A Puzzling Problem 

The goal of this problem is to write a program which will take from 1 to 5 puzzle pieces such as those shown below and arrange them, if possible, to form a square. An example set of pieces is shown here.

The pieces cannot be rotated or flipped from their original orientation in an attempt to form a square from the set. All of the pieces must be used to form the square. There may be more than one possible solution for a set of pieces, and not every arrangement will work even with a set for which a solution can be found. Examples using the above set of pieces are shown here.

Input

The input file for this program contains several puzzles (i.e. sets of puzzle pieces) to be solved. The first line of the file is the number of pieces in the first puzzle. Each piece is then specified by listing a single line with two integers, the number of rows and columns in the piece, followed by one or more lines which specify the shape of the piece. The shape specification consists of `0' and `1' characters, with the `1' characters indicating the solid shape of the puzzle (the `0' characters are merely placeholders). For example, piece `A' above would be specified as follows:

2 3
111
101

The pieces should be numbered by the order they are encountered in the puzzle. That is, the first piece in a puzzle is piece #1, the next is piece #2, etc. All pieces may be assumed to be valid and no larger than 4 rows by 4 columns.

The line following the final line of the last piece contains the number of pieces in the next puzzle, again followed by the puzzle pieces and so on. The end of the input file is indicated by a zero in place of the number of puzzle pieces.

Output

Your program should report a solution, if one is possible, in the format shown by the examples below. A 4-row by 4-column square should be created, with each piece occupying its location in the solution. The solid portions of piece #1 should be replaced with `1' characters, of piece #2 with `2' characters, etc. The solutions for each puzzle should be separated by a single blank line.

If there are multiple solutions, any of them is acceptable. For puzzles which have no possible solution simply report ``No solution possible''.

Sample Input

4
2 3
111
101
4 2
01
01
11
01
2 1
1
1
3 2
10
10
11
4
1 4
1111
1 4
1111
1 4
1111
2 3
111
001
5
2 2
11
11
2 3
111
100
3 2
11
01
01
1 3
111
1 1
1
0

Sample Output

1112
1412
3422
3442 No solution possible 1133
1153
2223
2444

题目大意:给出一些积木,要求将积木全部使用后拼成一个4*4的正方形。

解题思路:这题思路很简单,将所有积木以数组的形式储存起来,然后在4*4的地图上逐一去判断。主要注意也就三点。

1:积木的表示方法,我是使用结构体去存的,只要记录下面的所有木块与第一个木块的关系就可以了。

2:放入木块前先判断放入木块是否会越界重叠的问题后再放入。

3:如果当前位置没有合适的积木可以放的话可以直接回溯了,因为题目要求是不能旋转(这点最关键,需要空间想象力好的才能理解)

一不小心跑进uva rank 10,唉,小开心一下。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 20
const int R = 4; int n, vis[R][R], rec[N];
struct sque{
int dir[N][2];
int cnt;
}b[N]; void get_block(int cur){
int r, c, t, ok = 0, p, q;
char str[N];
scanf("%d%d", &r, &c);
for (int i = 0; i < r; i++){
scanf("%s", str);
for (int j = 0; j < c; j++){
if (str[j] - '0'){
if (ok){
b[cur].dir[b[cur].cnt][0] = i - p;
b[cur].dir[b[cur].cnt][1] = j - q;
b[cur].cnt++;
}
else{
b[cur].dir[b[cur].cnt][0] = 0;
b[cur].dir[b[cur].cnt][1] = 0;
p = i;
q = j;
b[cur].cnt++;
ok = 1;
}
}
}
}
} bool judge(int x, int y, int cur){
for (int i = 0; i < b[cur].cnt; i++){
if (x + b[cur].dir[i][0] < 0 || x + b[cur].dir[i][0] >= 4)
return false;
if (y + b[cur].dir[i][1] < 0 || y + b[cur].dir[i][1] >= 4)
return false;
if (vis[x + b[cur].dir[i][0]][y + b[cur].dir[i][1]])
return false;
}
return true;
} bool dfs(int x, int y, int sum, int k){
if (sum == 16){
if (k == n)
return true;
else
return false;
} if (x == 4)
return false;
if (vis[x][y]){
if (y == 3){
if (dfs(x + 1, 0, sum, k))
return true;
}
else{
if (dfs(x, y + 1, sum, k))
return true;
}
return false;
} for (int i = 1; i <= n; i++){
if (rec[i]) continue;
if (judge(x, y, i)){
rec[i] = 1;
for (int j = 0; j < b[i].cnt; j++)
vis[x + b[i].dir[j][0]][y + b[i].dir[j][1]] = i; if (y == 3){
if (dfs(x + 1, 0, sum + b[i].cnt, k + 1))
return true;
}
else{
if (dfs(x, y + 1, sum + b[i].cnt, k + 1))
return true;
} rec[i] = 0;
for (int j = 0; j < b[i].cnt; j++)
vis[x + b[i].dir[j][0]][y + b[i].dir[j][1]] = 0;
}
}
return false;
} int main(){
int text = 0;
while (scanf("%d", &n), n){
if (text++)
printf("\n");
// Init;
memset(b, 0, sizeof(b));
memset(vis, 0, sizeof(vis));
memset(rec, 0, sizeof(rec)); // Read;
for (int i = 1; i <= n; i++)
get_block(i); if(dfs(0, 0, 0, 0)){
for (int i = 0; i < R; i++){
for (int j = 0; j < R; j++)
printf("%d", vis[i][j]);
printf("\n");
}
}
else
printf("No solution possible\n");
}
return 0;
}

uva 387 A Puzzling Problem (回溯)的更多相关文章

  1. UVA - 387 A Puzzling Problem

    题目链接: https://vjudge.net/problem/UVA-387 思路: 非常有意思的拼图,深搜+回溯, 输出硬伤:除了第一次之外,每次先输空格,再输出结果, 以及可能给的数据拼不成4 ...

  2. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  3. UVa 101 The Blocks Problem Vector基本操作

    UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...

  4. uva387 - A Puzzling Problem

    A Puzzling Problem The goal of this problem is to write a program which will take from 1 to 5 puzzle ...

  5. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  6. uva 10837 - A Research Problem(欧拉功能+暴力)

    题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin.要求一个最小的n.欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p−1; ...

  7. UVA 810 - A Dicey Problem(BFS)

    UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...

  8. UVA 10026 Shoemaker's Problem 鞋匠的难题 贪心+排序

    题意:鞋匠一口气接到了不少生意,但是做鞋需要时间,鞋匠只能一双一双地做,根据协议每笔生意如果拖延了要罚钱. 给出每笔生意需要的天数和每天的罚钱数,求出最小罚钱的排列顺序. 只要按罚款/天数去从大到小排 ...

  9. UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。

    /** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...

随机推荐

  1. 将cocos2dx项目从Visual Studio 迁移到 xcode

    因为Visual Studio和XCode的巨大差异性,一开始选择任何一个IDE,都会有一个迁移的过程,XCode的迁移到Visual Studio相对非常简单,不用再介绍.将项目从Visual St ...

  2. bjfu1262 优先队列

    比较典型的应用优先队列的题.题目是在一个长为n的数组中,依次问m个数中的最小值.那么把值和下标做成一个结构体,放进优先队列里,每次移动窗口就把该T的T掉,剩下的最小值就是答案,复杂度nlogn,轻松a ...

  3. duilib让不同的容器使用不同的滚动条样式

    装载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42240569 以前在给一个容器设置横纵向的滚动条时,一直是通过设置xml ...

  4. MicroSD卡(TF卡)SPI模式实现方法

    现在我们手机的内存卡多为Micro SD卡,又叫TF卡,所以Micro SD卡比SD卡常见.自己曾经也想写写SD卡的读取程序,但又不想特地再去买个SD卡,这时想起手机内存卡不是和SD卡很像吗?在网上查 ...

  5. Mac下用命令行直接批量转换文本编码到UTF8

    由于近期在Mac下写Android程序,下载的一些Demo由于编码问题源码里的汉字出现乱码,文件比较多,所以想批量解决下文件的编码问题. Mac下有以下两种方式可以解决: A. 文件名的编码:Mac的 ...

  6. Maven安装与全局profile配置

    Maven 3.2 需要 JDK 1.6, Maven 3.0/3.1 需要 JDK 1.5 · 解压. · 环境变量 M2_HOME · M2 = %M2_HOME%\bin 同时也添加到PATH ...

  7. Python 批量创建同文件名的特定后缀文件

    看了很多批量创建文件和文件批量格式转换的code,感觉杀鸡焉用牛刀,自己写了几行轻量级的拿来给大家参考: 在out_dir目录下批量创建与in_dir目录下同文件名但后缀不同的文件. in_dir = ...

  8. NServiceBus-性能测试

    NServiceBus: 有效地处理一个消息 处理大量并发 尺度大小不同的服务器 尺度低规格的设备 的最终平衡速度和安全. 基准 许多参数会影响测量性能.最明显的是硬件服务器和CPU核的数量,大小的内 ...

  9. gem 相关命令

    gem #查看gem源 gem sources # 删除默认的gem源 gem sources --remove http://rubygems.org/ # 增加taobao作为gem源 gem s ...

  10. html5 canvas 钟表

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...