[ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008

题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是上右下左,每个三角形里面标记了数字,问你能否通过移动这n*n个格子,使得相邻两个三角形具有相同的数字?

dfs暴搜,dfs(x,y)代表当前要放(x,y)这个位置。

然后枚举给定的每个格子。

如果说可以放,就dfs下一个格子。

这一道题需要把相同的格子压缩起来,也就是说为了节省时间,可以记录相同的格子出现的次数。

然后对于这不同种的格子去dfs。

如果不这样做会超时。

这算是一种优化方法吧。涨姿势了。

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <string>
 4 #include <iostream>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <cctype>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <iterator>
12 #include <functional>
13 #include <cmath>
14 #include <numeric>
15 #include <ctime>
16 using namespace std;
17 typedef long long LL;
18 typedef pair<int,int> PII;
19 typedef vector<int> VI;
20 #define PB push_back
21 #define MP make_pair
22 #define SZ size()
23 #define CL clear()
24 #define AA first
25 #define BB second
26 #define EPS 1e-8
27 #define ZERO(x) memset((x),0,sizeof(x))
28 const int INF = ~0U>>1;
29 const double PI = acos(-1.0);
30
31 int n;
32 int now[10][10];
33 bool vis[100];
34 int ptr;
35 bool flag;
36
37 struct Node{
38     int top,right,bottom,left;
39     int num;
40     bool operator==(const Node& b) const{
41         return top==b.top&&right==b.right&&bottom==b.bottom&&left==b.left;
42     }
43     Node(int t=0,int r=0,int b=0,int l=0):top(t),right(r),bottom(b),left(l){}
44 };
45 Node mp[100];
46
47 void dfs(int x,int y){
48     if(flag) return;
49     if( x==n || y==n ){
50         flag = true;
51         return;
52     }
53     for(int i=0;i<ptr;i++){
54         if( mp[i].num&&
55            (x==0||mp[now[x-1][y]].bottom==mp[i].top)
56            &&(y==0||mp[now[x][y-1]].right==mp[i].left) ){
57                 now[x][y] = i;
58                 mp[i].num--;
59                 if( y+1<n ) dfs(x,y+1);
60                 else dfs(x+1,0);
61                 mp[i].num++;
62            }
63     }
64 }
65
66 int main(){
67     int kase = 1;
68     while(scanf("%d",&n),n){
69         ptr = 0;
70         for(int i=0;i<n*n;i++){
71             int t,r,b,l;
72             scanf("%d%d%d%d",&t,&r,&b,&l);
73             bool appear = 0;
74             for(int j=0;j<ptr;j++){
75                 if(Node(t,r,b,l)==mp[j]){
76                     mp[j].num++;
77                     appear = 1;
78                 }
79             }
80             if(!appear){
81                 mp[ptr] = Node(t,r,b,l);
82                 mp[ptr++].num = 1;
83             }
84         }
85
86         flag = false;
87         dfs(0,0);
88
89         if(kase!=1) puts("");
90         printf("Game %d: ",kase++);
91         puts(flag?"Possible":"Impossible");
92     }
93     return 0;
94 }
时间: 2024-08-07 00:46:44

[ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)的相关文章

zoj 1008 Gnome Tetravex (dfs+枚举)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles m

ZOJ 1008 Gnome Tetravex(DFS)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles m

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

ZOJ Problem Set - 1008 Gnome Tetravex (DFS+剪枝)

ZOJ Problem Set - 1008 Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divide

zoj 1008 Gnome Tetravex

开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <st

ZOJ1008 Gnome Tetravex(DFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题意: 哈特近来一直在玩有趣的 Gnome Tetravex 游戏.在游戏开始时,玩家会得到 n×n(n≤5) 个正方形.每个正方形都被分成 4个标有数字的三角形(数字的范围是 0到9).这四个三角形分 别被称为"左三角形"."右三角形"."上三角形"和"下三角形".例如,图 2.12(

洛谷OJ 1074 靶型sudoku dfs(搜索顺序优化)

https://www.luogu.org/problem/show?pid=1074 题意:靶型sudoku 问填完的最大得分?//优化:从可能性小的开始搜索,把地图分数设置成常量数组,接着暴力即可 #include <bits/stdc++.h> using namespace std; const int N=2e2+20; int row[N][N],col[N][N],vis[N][N];//row[i][1~9] 第i列是否能选j int a[N][N]; int ans,n=9,

1008 Gnome Tetravex

练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. 1 #include <stdio.h> 2 3 int m,n,f,sub[25][4],note[25],ans[25]; 4 5 void ini(){ 6 int i,j,top,right,bottom,left; 7 for(i=0;i<25;i++){ 8 for(j=0;j<4;j++) 9 sub[i][j]=0; 10 note[i]=0; 11 ans[i]=0; 12 } 13

组队赛#1 解题总结 ZOJ 3803 YY&#39;s Minions (DFS搜索+模拟)

YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W