ZOJ 1008 (DFS)
2013-09-27 15:41
274 查看
原地址: 点击打开链接
对DFS的恢复现场加深了理解~!
对DFS的恢复现场加深了理解~!
//zoj 1008 #include <iostream> #include <cstdio> #include <cmath> using namespace std; const int SIZE=6; int map[SIZE*SIZE][SIZE], cnt[SIZE*SIZE], tot; int n, m; int res[SIZE*SIZE]; int check(int top, int right, int bottom, int left) { for(int i=0;i<tot;i++) if(map[i][0]==top && map[i][1]==right && map[i][2]==bottom && map[i][3]==left) return i; return -1; } bool dfs(int u) { if(u==m) return true; for(int i=0;i<tot;i++) { if(cnt[i]==0) continue; if(u/n!=0 && map[i][0]!=map[res[u-n]][2]) continue; if(u%n!=0 && map[i][3]!=map[res[u-1]][1]) continue; res[u]=i; cnt[i]--; if(dfs(u+1)) return true; else cnt[i]++; } return false; } int main() { int top, right, bottom, left; int line=1; while(1) { cin>>n; if(n==0) break; m=n*n; tot=0; for(int i=0;i<m;i++) { cin>>top>>right>>bottom>>left; int xh=check(top,right,bottom,left); if(xh<0) { map[tot][0]=top; map[tot][1]=right; map[tot][2]=bottom; map[tot][3]=left; cnt[tot++]=1; } else cnt[xh]++; } if(line>1) printf("\n"); if(dfs(0)) printf("Game %d: Possible\n",line); else printf("Game %d: Impossible\n",line); line++; } return 0; }
相关文章推荐
- ZOJ 1008 Gnome Tetravex(DFS)
- ZOJ 1008 Gnome Tetravex(DFS)
- ZOJ--1008:Gnome Tetravex(剪枝dfs)
- zoj 1008 Gnome Tetravex (dfs+枚举)
- ZOJ 1008 Gnome Tetravex (使用状态进行DFS)
- zoj 1008 暴力枚举求解dfs+优化
- ZOJ 1008 Gnome Tetravex(DFS)
- zoj 1008 暴力枚举求解dfs+优化
- zoj 1008 dfs 一种巧妙的剪枝
- ZOJ 1008 Gnome Tetravex(DFS)
- zoj 1008 DFS
- [ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)
- zoj 1008 Gnome Tetravex (DFS + 剪枝)
- ACM-ZOJ 1008 DFS 一道好题
- (DFS)zoj1008-Gnome Tetravex
- ZOJ 1008 Gnome Tetravex(DFS)
- zoj2100 Seeding (DFS递归+回溯)
- ZOJ 2103 Marco Popo the Traveler(dfs+欧拉路)
- zoj 2100 Seeding 【DFS 判断是否能经过每个点一次遍历完整个图】
- 2017 Multi-University Training Contest - Team 1 1003(hdu6035Colorful Tree) 树 dfs 1008(6040 Hints)