2013
12-12

# 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.

The input for this program contains several puzzles (i.e. sets of puzzle pieces) to be solved. The first line of the input 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.

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 followed 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”.

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

1112
1412
3422
3442

No solution possible

1133
1153
2223
2444

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int pieset[16][4][4]={0};//all pieces set,note piece#1 place 1111 piece#2 place 2222 no the problem says place 1111,piece#i place iii
int n=0;//per cases pieces number
int piesize[16][2]={0};//all pieces size m*n
int piecnt[16]={0};//piecnt[i] is piece#(i+1) 's sum !0 count
int puzzle[4][4]={0};//place the all piece to this matirx
int flag=0;//is solution possible flag
/*judge the block !0 and !0 is invalid*/
bool IsFit(int dest[][4])
{
int i=0,j=0;
for(i=0;i<=4-1;i++)
{
for(j=0;j<=4-1;j++)
{
if(dest[i][j]!=0&&puzzle[i][j]!=0)
{
return(false);
}
}
}
return(true);
}
/*fit the piece to the puzzle*/
int FitPie(int dest[][4])
{
int i=0,j=0;
for(i=0;i<=4-1;i++)
{
for(j=0;j<=4-1;j++)
{
if(dest[i][j]!=0)
{
puzzle[i][j]=dest[i][j];
}
}
}
return(0);
}
/*unfit the piece from the puzzle*/
int UnFitPie(int dest[][4])
{
int i=0,j=0;
for(i=0;i<=4-1;i++)
{
for(j=0;j<=4-1;j++)
{
if(dest[i][j]!=0)
{
puzzle[i][j]=0;
}
}
}
return(0);
}
/*judge the matrix is filled with the piece*/
bool IsFilled()
{
int i=0,j=0;
for(i=0;i<=4-1;i++)
{
for(j=0;j<=4-1;j++)
{
if(puzzle[i][j]==0)
{
return(false);
}
}
}
return(true);
}
/*DFS the piece,the cur is piece number, or index of now will place the piece*/
int DFS(int cur)
{
if(flag)
{
return(0);
}
if(cur>=n)
{
if(IsFilled())//judge the matrix is filled with the piece
{
flag=1;
return(0);
}
}
else
{
//translate the piece
int i=0,j=0;//i,j is increase of the coordinate
int t=0,r=0;//destination two dimension coordinate
int x=0,y=0;//source two dimension coordinate
for(i=-3;i<=3;i++)//can translate to - direction
{
for(j=-3;j<=3;j++)
{
int dest[4][4]={0};//we can not destroyed the original data from the pieset,this is translate destination piece
int destflag=0;//break is true flag = 1, then the dest is invalid
for(x=0;x<=piesize[cur][0]-1;x++)
{
for(y=0;y<=piesize[cur][1]-1;y++)
{
if(pieset[cur][x][y]>0)//find the valid point
{
//translate
t=x+i;
r=y+j;
if(t>=4||r>=4||t<0||r<0)//invalid translation
{
destflag=1;
break;
}
dest[t][r]=pieset[cur][x][y];//translation
}
}
if(destflag)
{
break;//invalid translation
}
}
if(!destflag)//the translation is valid
{
if(IsFit(dest))//the piece[cur] is fit for now puzzle matrix
{
FitPie(dest);
DFS(cur+1);
if(flag)//prevent the answer puzzle matrix recover
{
return(0);
}
UnFitPie(dest);
}
}
}
}
}
return(0);
}
/*output the puzzle*/
int OutPuzzle()
{
int i=0,j=0;
for(i=0;i<=4-1;i++)
{
for(j=0;j<=4-1;j++)
{
printf("%d",puzzle[i][j] );
}
printf("\n");
}
return(0);
}
/*for test*/
int test()
{
return(0);
}
/*main process*/
int MainProc()
{
int t=0;
while(scanf("%d",&n)!=EOF&&n>0)
{
//fromat control
t++;
if(t>1)
{
printf("\n");
}
//init
memset(pieset,0,sizeof(pieset));
memset(piesize,0,sizeof(piesize));
memset(puzzle,0,sizeof(puzzle));
memset(piecnt,0,sizeof(piecnt));
flag=0;
int i=0,j=0,k=0;
int piececnt=0;
for(k=0;k<=n-1;k++)
{
scanf("%d%d",&piesize[k][0],&piesize[k][1]);
for(i=0;i<=piesize[k][0]-1;i++)
{
for(j=0;j<=piesize[k][1]-1;j++)
{
int num=0;
scanf("%1d",&num);//scanf format control
if(num>0)
{
piececnt++;
piecnt[k]++;
pieset[k][i][j]=k+1;//piece#i place i
}
}
}
}
if(piececnt==16)//prune,the puzzle 0' count != left pieces !0's count then prune it
DFS(0);
if(flag)
{
OutPuzzle();
}
else
{
printf("No solution possible\n");
}
}
return(0);
}
int main(int argc, char const *argv[])
{
/* code */
MainProc();
return 0;
}`

1. 我还有个问题想请教一下，就是感觉对于新手来说，递归理解起来有些困难，不知有没有什么好的方法或者什么好的建议？