本文主要是介绍uva 387 A Puzzling Problem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目地址:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=323
题目描述:
A Puzzling Problem
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 3442No solution possible1133 1153 2223 2444题意:
在二维平面上放木块拼图。
题解:
水题,DFS就行了。感觉是uva 197 cube的简单版。直接dfs就行。这里我用的是2维4*4的数组来存储输入的碎片木块,再联立成一个三维数组。直接挨顺序取木块,平移,放入,到取完最后一个木块时 即表示能组成一个满4*4的矩型。
代码:
#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 pieceint i=0,j=0;//i,j is increase of the coordinateint t=0,r=0;//destination two dimension coordinateint x=0,y=0;//source two dimension coordinatefor(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 pieceint destflag=0;//break is true flag = 1, then the dest is invalidfor(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{//translatet=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 controlt++;if(t>1){printf("\n");}//initmemset(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 controlif(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 itDFS(0);if(flag){OutPuzzle();}else{printf("No solution possible\n");}}return(0);
}
int main(int argc, char const *argv[])
{/* code */MainProc();return 0;
}
这篇关于uva 387 A Puzzling Problem的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!