UVA 387 A Puzzling Problem(dfs + 模拟)
2013-08-10 20:49
856 查看
A Puzzling Problem |
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 ofrows 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
题意:输入一个t表示有t块拼图。以下t行输入n,m表示n*m的拼图。然后以下n*m行输入该拼图的形状。要判断这些拼图能不能拼出一个4 * 4的正方形。如果行输出拼的方法(一种即可)如果不行输出No solution possible。注意每块拼图都要用上哦亲。。
思路:这题数据量不是很大。4 * 4。我是直接深搜没有剪枝就过了。。大概就是模拟一个个拼图去放置。放置到一种可以情况就可以结束了。
#include <stdio.h>
#include <string.h>
int t;
int n, m;
char map[10][10];
int judge;
int vis[20];
struct Block {
char set[10][10];
int num;
int n, m;
} b[20];
int ju(int ii, int x, int y) {
int i, j;
for (i = x; i < x + b[ii].n; i ++)
for (j = y; j < y + b[ii].m; j ++)
if (map[i][j] != '0' && b[ii].set[i -x][j - y] != '0')
return 0;
return 1;
}
void dfs(int x, int y, int tt, int num) {
int i, j, k;
if (tt == t) {
if (num == 16)
judge = 1;
return;
}
if (y > 4) {
if (x < 4)
dfs(x + 1, 1, tt, num);
return;
}
for (i = 1; i <= t; i ++) {
if (!vis[i] && ju(i, x, y)) {
vis[i] = 1;
for (j = x; j < x + b[i].n; j ++)
for (k = y; k < y + b[i].m; k ++)
if (b[i].set[j - x][k - y] != '0')
map[j][k] = b[i].set[j - x][k - y] + i - 1;
dfs(x, y + 1, tt + 1, num + b[i].num);
if (judge) return;
vis[i] = 0;
for (j = x; j < x + b[i].n; j ++)
for (k = y; k < y + b[i].m; k ++)
if (b[i].set[j - x][k - y] != '0')
map[j][k] = '0';
}
}
dfs(x, y + 1, tt, num);
}
int main() {
int i, j, k;
int bo = 0;
while (scanf("%d", &t) != EOF && t) {
if (bo == 0)
bo = 1;
else
printf("\n");
judge = 0;
memset(b, 0, sizeof(b));
memset(vis, 0, sizeof(vis));
memset(map, '1', sizeof(map));
for (i = 1; i <= 4; i ++)
for (j = 1; j <= 4; j ++)
map[i][j] = '0';
for (i = 1; i <= t ; i ++) {
scanf("%d%d%*c", &n, &m);
b[i].n = n; b[i].m = m;
for (j = 0; j < n; j ++) {
gets(b[i].set[j]);
int len = strlen(b[i].set[j]);
for (k = 0; k < len; k ++)
if (b[i].set[j][k] != '0')
b[i].num ++;
}
}
dfs(1, 1, 0, 0);
if (judge) {
for (i = 1; i <= 4; i ++) {
for (j = 1; j <= 4; j ++)
printf("%c", map[i][j]);
printf("\n");
}
}
else
printf("No solution possible\n");
}
return 0;
}
相关文章推荐
- UVA - 387 A Puzzling Problem
- UVA 387 A Puzzling Problem
- UVa 387 - A Puzzling Problem
- UVa 387 - A Puzzling Problem 解题报告(暴力)
- uva 387 A Puzzling Problem (回溯)
- uva387 - A Puzzling Problem
- UVa 387 - A Puzzling Problem
- uva 387 A Puzzling Problem (回溯)
- uva 387 A Puzzling Problem
- UVA 387 - A Puzzling Problem
- 【UVa】387 A Puzzling Problem
- uva 387 - A Puzzling Problem
- uva387 A Puzzling Problem
- UVA - 387 A Puzzling Problem(回溯)
- UVA - 524 Prime Ring Problem(dfs回溯法) 解题心得
- UVA 1640 The Counting Problem(统计题,找规律 + dfs)
- UVA101 The Blocks Problem(模拟)
- Solve this interesting problem(线段树逆二分模拟的DFS递归操作)
- hdu多校第三场 Solve this interesting problem (5323 线段树模拟 + DFS)
- The Blocks Problem UVA 101 模拟