习题7-10 UVA 11214 Guarding the Chessboard (迭代加深搜索)
来源:互联网 发布:java applet插件下载 编辑:程序博客网 时间:2024/04/26 13:32
大体题意:
给你n × m 的棋盘 X代表敌人,问最少放几个皇后吃掉所有敌人。
思路:
类似八皇后问题,直接枚举放的皇后个数进行迭代加深搜索。
dfs(x,y,cur)x代表 行数,y代表列数,cur 代表当前第几个皇后,直到满足所有X都可以被吃即可!
注意 :
放皇后时要注意皇后可以不放在X上!
最多5个皇后,枚举到4即可!
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 10 + 5;bool vis[4][3*maxn];char mp[maxn][maxn];int n,m;int maxd,cnt;bool dfs(int x,int y,int cur){if (cur == maxd){for (int i = 0; i < n; ++i){for (int j = 0; j < m; ++j)if (mp[i][j] == 'X' && !vis[0][i] && !vis[1][j] && !vis[2][i+j] && !vis[3][i-j+11])return false;}return true;}while(x < n){while(y < m){bool t1 = vis[0][x], t2 = vis[1][y], t3 = vis[2][x+y], t4 = vis[3][x-y+11];vis[0][x] = vis[1][y] = vis[2][x+y] = vis[3][x-y+11] = true;if(dfs(x+1,0,cur+1))return true;vis[0][x] = t1, vis[1][y] = t2, vis[2][x+y] = t3, vis[3][x-y+11] = t4;++y;}y = 0;x++;}return false;}int main(){while(scanf("%d",&n) == 1 && n){scanf("%d",&m);for (int i = 0; i < n; ++i)scanf("%s",mp[i]);for (maxd = 0; maxd < 5 ; ++maxd){memset(vis,0,sizeof(vis));if(dfs(0,0,0))break;}printf("Case %d: %d\n",++cnt,maxd);}return 0;}
0 0
- 习题7-10 UVA 11214 Guarding the Chessboard (迭代加深搜索)
- UVa 11214 - Guarding the Chessboard(迭代加深搜索)
- UVa #11214 Guarding the Chessboard (习题7-10)
- UVa 11214:Guarding the Chessboard(DFS)
- 11214 - Guarding the Chessboard(暴力搜索)
- UVa 11214 Guarding the Chessboard
- UVa 11214 - Guarding the Chessboard
- UVA 11214 Guarding the Chessboard
- UVa 11214 - Guarding the Chessboard
- Guarding the Chessboard UVA
- Guarding the Chessboard UVA
- Guarding the Chessboard UVA
- Uva11214 Guarding the Chessboard【dfs回溯】【习题7-10】
- UVA 11214(p217)----Guarding the Chessboard
- UVA 11214 Guarding the Chessboard(回溯)
- 11214 - Guarding the Chessboard
- 习题7-10:守卫棋盘(dfs迭代加深)
- UVa 1533:Moving Pegs(迭代加深搜索)
- BeanUtils工具类
- 倒排索引
- 关于数字图像处理中不同的低通滤波器的区别以及cut-off frequency 对于图像的影响
- 理解Twisted与非阻塞编程
- PLS-00103: Encountered the symbol "CREATE"
- 习题7-10 UVA 11214 Guarding the Chessboard (迭代加深搜索)
- 记忆+语录 by ArcGIS工程师
- ZOJ 1195
- ExcelUtils 工具类
- 9patch背景导致控件内容异常
- Android——多线程
- 折半插入排序
- 南京理工大学第八届程序设计大赛(校外镜像) F sequence
- Hdu 5661 Claris and XOR