URAL 1970 J - 皇后像廣場 dfs
J - 皇后像廣場
题目连接:
http://acm.hust.edu.cn/vjudge/contest/123332#problem/J
Description
Vova was walking along the Statue Square (皇后像廣場) in Hong Kong, when he noticed that the square was paved with multi-colored square tiles. Vova took a careful look at the tiles and realized that they formed some picture. The tiles were large and he couldn't look at all of them at once. Probably one should look at the tile picture from above.
Vova decided to take a photo of a (10 × 10)-tile part of a picture, but he still couldn't cover them with one shot. Then Vova took nine photos, each of them covered a (4 × 4)-tile area of the picture (see the picture below). If we arrange the nine photos correctly, then we can restore the original 10 × 10 picture.
Problem illustration
Unfortunately, soon after printing out the photos Vova forgot not only the arranging order of them, but the correct rotation as well. He can rotate the image on the photo by an arbitrary angle multiple of 90 degrees. Help Vova use the nine photos to restore the original 10 × 10 pattern.
Input
The input contains the nine photos Vova made. The photos are described by 4 × 4 size matrices containing integers from 0 to 99, representing the colors of the corresponding tiles. The numbers on a line are separated by spaces. Each matrix is separated from the next one by an empty line.
Output
Print the original pattern as a 10 × 10 matrix. The matrix elements in the line should be separated by spaces. If there are multiple solutions, you may print any of them. It is guaranteed that at least one solution exists.
Sample Input
1 1 9 9
1 9 1 1
9 1 1 1
9 1 1 9
9 1 1 9
9 1 1 1
9 1 1 1
9 1 1 9
9 1 1 9
9 1 1 1
1 9 1 1
1 1 9 9
9 1 1 9
1 1 1 1
1 1 1 1
9 1 1 9
9 1 1 9
1 1 1 9
1 1 9 1
9 9 1 1
9 1 1 9
1 1 1 9
1 1 1 9
9 1 1 9
9 9 1 1
1 1 9 1
1 1 1 9
9 1 1 9
9 9 9 9
1 1 1 1
1 1 1 1
9 1 1 9
9 1 1 9
1 9 9 1
1 1 1 1
9 9 9 9
Sample Output
1 1 9 9 9 9 9 9 1 1
1 9 1 1 1 1 1 1 9 1
9 1 1 1 1 1 1 1 1 9
9 1 1 9 1 1 9 1 1 9
9 1 1 1 1 1 1 1 1 9
9 1 1 1 1 1 1 1 1 9
9 1 1 9 1 1 9 1 1 9
9 1 1 1 9 9 1 1 1 9
1 9 1 1 1 1 1 1 9 1
1 1 9 9 9 9 9 9 1 1
Hint
题意
给你9个44的矩阵,然后问你能不能用这9个矩阵拼成一个1010的大矩阵的图案。
可以旋转。
题解:
预处理旋转之后的样子,然后直接dfs就好了
不需要剪枝,我猜状态不会很多。。
代码
#include<bits/stdc++.h>
using namespace std;
struct node
{
int a[4][4];
}p[9][4];
int flag = 0;
int ans[10][10];
int vis[10];
int xx[9]={0,0,0,3,3,3,6,6,6};
int yy[9]={0,3,6,0,3,6,0,3,6};
void dfs(int x){
if(flag)return;
if(x==9){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j==0)printf("%d",ans[i][j]);
else printf(" %d",ans[i][j]);
}
printf("\n");
}
flag = 1;
return;
}
for(int i=0;i<9;i++){
if(vis[i])continue;
for(int j=0;j<4;j++){
int fff = 0;
for(int t=0;t<4;t++){
for(int k=0;k<4;k++){
if(ans[xx[x]+t][yy[x]+k]==-1)continue;
if(ans[xx[x]+t][yy[x]+k]!=p[i][j].a[t][k]){
fff = 1;
break;
}
}
if(fff)break;
}
if(fff==0){
for(int t=0;t<4;t++){
for(int k=0;k<4;k++){
ans[xx[x]+t][yy[x]+k]=p[i][j].a[t][k];
}
}
vis[i]=1;
dfs(x+1);
if(flag)return;
vis[i]=0;
for(int t=0;t<4;t++){
if(x/3!=0&&t==0)continue;
for(int k=0;k<4;k++){
if(x%3!=0&&k==0)continue;
ans[xx[x]+t][yy[x]+k]=-1;
}
}
}
}
}
}
int main(){
//freopen("1.in","r",stdin);
memset(ans,-1,sizeof(ans));
for(int i=0;i<9;i++){
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
scanf("%d",&p[i][0].a[j][k]);
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
p[i][1].a[j][k]=p[i][0].a[k][4-j-1];
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
p[i][2].a[j][k]=p[i][1].a[k][4-j-1];
for(int j=0;j<4;j++)
for(int k=0;k<4;k++)
p[i][3].a[j][k]=p[i][2].a[k][4-j-1];
}
dfs(0);
}
URAL 1970 J - 皇后像廣場 dfs的更多相关文章
- 蓝桥杯之 2n皇后问题(双层dfs,暴力)
Description 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后 和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两 个白皇后都不在同 ...
- HDU 2553 N皇后问题(深搜DFS)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 八皇后问题解题报告(dfs
这里是代码传送门 所谓八皇后问题,一开始接触,上学期舍友提及的,但是因为各种原因,水平不够,并没有关心,偶然之间,再次遇见,便进行的尝试(棋盘是0-7的,不是1-8的...开始打弄错了) 所谓八皇后问 ...
- N皇后问题hdu2553(dfs)
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...
- HDU-6341 Problem J. Let Sudoku Rotate(dfs 剪枝)
题目:有一个4*4*4*4的数独,每一横每一竖每一个小方块中都无重复的字母,即都为0-9,A-F..有一个已经填好的数独,若干个4*4的方块被逆时针拧转了若干次,问拧转回来至少需要多少次. 分析:很明 ...
- URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)
题目链接: URAL 1890 . Money out of Thin Air 题目描述: 给出一个公司里面上司和下级的附属关系,还有每一个人的工资,然后有两种询问: 1:employee x y z ...
- hdu6341 Problem J. Let Sudoku Rotate (dfs)
题目传送门 题意: 给你16个16宫格的数独,里面是0~F,你可以逆时针旋转里面的每个16宫格 问你它是从标准数独逆时针旋转多少次得到? 思路: 可以知道每个16宫已经是标准的了,接下来只要考虑每行. ...
- 洛谷 1219:八皇后 (位运算 & DFS)
题目链接: https://www.luogu.org/problem/show?pid=1219#sub row:受上面的皇后通过列控制的位置 ld:受上面的皇后通过从右至左的斜对角线控制的位置 r ...
随机推荐
- JS面向对象编程之对象(简化版)
上次网上看了一篇这个文章,然后乱七八糟晕头转向把我晕的够呛.看了半天没找到错的地方但是浏览器Hello world就是没有定义...我也是醉了,最后发现我认为是废话的话一句话竟然有用!!!所以我还是简 ...
- [Alg::DP] 袋鼠过河
一道简单的动态规划问题. 题目来源:牛客网 链接:https://www.nowcoder.com/questionTerminal/74acf832651e45bd9e059c59bc6e1cbf ...
- lucene修改索引——(六)
原理: 修改的原理是先删除,后增加一个,这也是常用的一种修改的方式. 删除的文档的id不会被新增加的文档占用,类似于mysql的自增,当删除一个id=2时,以后id=2就是空着的,不会上来一个把2给占 ...
- if语句引起的bug
最近维护高手留下的api项目,客户端反馈一个bug过来,然后查找到可能出错的代码位置,是一个if语句,乍一看好像没什么问题,代码如下: if (company.UserId != userId || ...
- CEO、COO、CFO、CTO、CXO
CEO:Chief Executive Officer 首席执行官——类似总经理.总裁,是企业的法人代表 COO:Chief Operating Officer 首席营运官——类似常务总经理 CFO: ...
- orm 缺点
背景 提起orm,在我开发这几年可是阴魂不散,因为我的开发没人带,全是自己琢磨,好处是很多东西都懂,都理解的透彻,缺点是见得少,接触少.而我一直没用orm,但是又到处听说orm,但我总想不明白有啥用处 ...
- centos7更改网卡名
虚拟机中安装centos7,分配两张网卡,安装完成后,使用ip addr 命令查看网卡,发现网卡名称为ens33 和 ens34,不符合平时的使用习惯,想把网卡名改为eth0和eth1,具体操作步骤如 ...
- poj2054
题意:给定一棵树,每个节点有一个权值,现要求给这些节点进行排列,设排列后的节点顺序为v1~vn,它们的权值是w1~wn,那么我们要求一种排列使得w1*1+w2*2+...+wn*n最小.还有一个限制就 ...
- Python黑魔法
1. 赋值 In [1]: x = 1 ...: y = 21 ...: print x, y ...: ...: x, y = y, x ...: print x, y 1 21 21 1 2. 列 ...
- 在android 5.0以上,如何判断当前应用是在前台还是后台
今天在做项目时遇到这个问题,本来很简单的,但是在Android 5.1 的系统上运行却出错了,在网上查找答案后发现原来是在android 5.0 之后getRunningTask()方法被废弃,失效了 ...