Uva--387(记忆化回溯,弱剪枝)

2014-07-20 14:55:02

 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

思路:这题看起来有点烦,其实并不难。关键点有几个:
(1)推荐结构体保存每个块的形状(可用一个二维数组保存图),并记录所占长、宽(row,col),要注意图的方向,也就是从最顶行开始读入图,最后output也注意下顺序
(2)逐行逐列开始放,放过一个块就标记这个块已经用过,要注意:一个点可能放多个块!什么意思呢,就以第一个样例为例:
1112
1412
3422
3442
先放块3,再放块4。在放完4后位置在(0,2)如果此时跳到(0,3)的话那么块2 永远不能被放入,而实际上
块2 可以放在(0,2)这个位置。
 (这里的说法有些别扭,因为我对块的存图方式是从(0,0)开始,所以对于块2的图,(0,0)点是0)
所以对一个位置要遍历所有可能放的块。
(3)在检查一个位置能否放某个块的时候要注意越界和是否重叠。

  1 /*************************************************************************
  2     > File Name: Uva387.cpp
  3     > Author: Nature
  4     > Mail: 564374850@qq.com
  5     > Created Time: Sun 20 Jul 2014 12:26:06 AM CST
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<cmath>
 10 #include<algorithm>
 11 #include<cstring>
 12 #include<cstdio>
 13 #include<fstream>
 14 using namespace std;
 15 
 16 int n,num;
 17 int used[20],G[5][5];
 18 
 19 struct pie{
 20     int r,c;
 21     int vol;
 22     int g[5][5];
 23 };
 24 pie p[20];
 25 
 26 bool Check(int row,int col,int k){ // only used to check and update the graph
 27     int tG[5][5];
 28     memcpy(tG,G,sizeof(G));
 29     for(int i = 0; i < p[k].r; ++i)
 30         for(int j = 0; j < p[k].c; ++j)
 31             if(p[k].g[i][j] == 1){
 32                 if(row + i < 4 && col + j < 4 && tG[row + i][col + j] == -1)
 33                     tG[row + i][col + j] = k;
 34                 else
 35                     return false;
 36             }
 37     memcpy(G,tG,sizeof(tG));
 38     return true;
 39 }
 40 
 41 void Back_Graph(int row,int col,int k){
 42     for(int i = 0; i < p[k].r; ++i)
 43         for(int j = 0; j < p[k].c; ++j)
 44             if(p[k].g[i][j] == 1 && row + i < 4 && col + j < 4)
 45                 G[row + i][col + j] = -1;
 46 }
 47 
 48 bool Dfs(int row,int col,int cnt){
 49     if(cnt == 16)
 50         return true;
 51     if(row >= 4 && col >= 4)
 52         return false;
 53     int next_row = row,next_col = col;
 54     if(col == 3){
 55         next_col = 0;
 56         ++next_row;
 57     }
 58     else ++next_col;
 59     int flag = 1;
 60     for(int i = 0; i < n; ++i){
 61         if(used[i]) continue;
 62         if(Check(row,col,i)){
 63             used[i] = 1;
 64             if(Dfs(row,col,cnt + p[i].vol))
 65                 return true;
 66             //if(Dfs(next_row,next_col,cnt + p[i].vol))
 67             //    return true;
 68             used[i] = 0;
 69             Back_Graph(row,col,i);
 70         }
 71     }
 72     if(G[row][col] != -1 && Dfs(next_row,next_col,cnt))
 73         return true;
 74     return  false;
 75 }
 76 
 77 void Init(){
 78     num = 0;
 79     memset(p,0,sizeof(p));
 80     memset(G,-1,sizeof(G));
 81     memset(used,0,sizeof(used));
 82 }
 83 
 84 int main(){
 85      int tr,tc,head = 1;
 86     char s[5];
 87       while(scanf("%d",&n) == 1 && n){
 88         Init();
 89         for(int i = 0; i < n; ++i){
 90             scanf("%d%d",&tr,&tc);
 91             p[i].r = tr;
 92             p[i].c = tc;
 93             for(int j = tr - 1; j >= 0; --j){
 94                 scanf("%s",s);
 95                 for(int k = 0; k < tc; ++k){
 96                     p[i].g[j][k] = s[k] - '0';
 97                     if(s[k] - '0') ++p[i].vol;
 98                 }
 99             }
100             num += p[i].vol;
101         }
102         if(!head) printf("\n");
103         head = 0;
104         if(num == 16 && Dfs(0,0,0)){
105             for(int i = 3; i >= 0; --i){
106                 for(int j = 0; j < 4; ++j)
107                     printf("%d",G[i][j] + 1);
108                 puts("");
109             }
110         }
111         else
112             printf("No solution possible\n");
113     }
114     return 0;
115 }

 





posted @ 2014-07-20 15:21  Naturain  阅读(183)  评论(0编辑  收藏  举报