uva 387 A Puzzling Problem

2024-01-20 02:30
文章标签 problem 387 uva puzzling

本文主要是介绍uva 387 A Puzzling Problem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=323

题目描述:

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/624531

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

uva 10055 uva 10071 uva 10300(水题两三道)

情歌两三首,水题两三道。 好久没敲代码了为暑假大作战热热身。 uva 10055 Hashmat the Brave Warrior 求俩数相减。 两个debug的地方,一个是longlong,一个是输入顺序。 代码: #include<stdio.h>int main(){long long a, b;//debugwhile(scanf("%lld%lld", &

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

uva 10387 Billiard(简单几何)

题意是一个球从矩形的中点出发,告诉你小球与矩形两条边的碰撞次数与小球回到原点的时间,求小球出发时的角度和小球的速度。 简单的几何问题,小球每与竖边碰撞一次,向右扩展一个相同的矩形;每与横边碰撞一次,向上扩展一个相同的矩形。 可以发现,扩展矩形的路径和在当前矩形中的每一段路径相同,当小球回到出发点时,一条直线的路径刚好经过最后一个扩展矩形的中心点。 最后扩展的路径和横边竖边恰好组成一个直

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

uva 568 Just the Facts(n!打表递推)

题意是求n!的末尾第一个不为0的数字。 不用大数,特别的处理。 代码: #include <stdio.h>const int maxn = 10000 + 1;int f[maxn];int main(){#ifdef LOCALfreopen("in.txt", "r", stdin);#endif // LOCALf[0] = 1;for (int i = 1; i <=

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10916 Factstone Benchmark(打表)

题意是求 k ! <= 2 ^ n ,的最小k。 由于n比较大,大到 2 ^ 20 次方,所以 2 ^ 2 ^ 20比较难算,所以做一些基础的数学变换。 对不等式两边同时取log2,得: log2(k ! ) <=  log2(2 ^ n)= n,即:log2(1) + log2(2) + log2 (3) + log2(4) + ... + log2(k) <= n ,其中 n 为 2 ^