UVa 11214 - Guarding the Chessboard
来源:互联网 发布:mac双系统卸载win7 编辑:程序博客网 时间:2024/04/30 03:04
这是一道迭代加深搜索的水题,与八皇后问题的解决方法一样,用一个数组来储存行列对角线副对角线是否有棋子,用迭代加深搜索来完成。
//stand c++
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
//#pragma comment(linker, "/STACK:102400000,102400000")
//stand c
#include<cstdio>
#include <cstring>
using namespace std;
int n, m;
bool vis[4][30];
char web[15][15];
bool allcapture(){
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j){
if(web[i][j] == 'X' && !vis[0][i] && !vis[1][j] && !vis[2][i+j] && !vis[3][n+j-i])
return false;
}
return true;
}
bool dfs(int lstp, int cur){
if(lstp == 0){
if(allcapture())
return true;
return false;
}
for(int i = cur; i < n*m; ++i){
int x = i / m, y = i % m;
int t0 = vis[0][x], t1 = vis[1][y], t2 = vis[2][x+y], t3 = vis[3][n+y-x];
vis[0][x] = vis[1][y] = vis[2][x+y] = vis[3][n+y-x] = true;
if(dfs(lstp-1, i+1))return true;
vis[0][x] = t0, vis[1][y] = t1, vis[2][x+y] = t2, vis[3][n+y-x] = t3;
}
return false;
}
int main(){
int cs = 0;
while(~scanf("%d", &n) && n){
printf("Case %d: ", ++cs);
scanf("%d", &m);
for(int i = 0; i < n; ++i)
scanf("%s", web[i]);
int ans = 0;
for(int i = 1; i <= 10; ++i){
memset(vis, 0, sizeof(vis));
if(dfs(i, 0)){ans = i; break;}
}
printf("%d\n", ans);
}
return 0;
}
0 0
- 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
- UVa 11214:Guarding the Chessboard(DFS)
- UVA 11214(p217)----Guarding the Chessboard
- UVA 11214 Guarding the Chessboard(回溯)
- 11214 - Guarding the Chessboard
- UVa #11214 Guarding the Chessboard (习题7-10)
- UVa 11214 - Guarding the Chessboard(迭代加深搜索)
- 11214 - Guarding the Chessboard(暴力搜索)
- UVa11214 - Guarding the Chessboard
- 习题7-10 UVA 11214 Guarding the Chessboard (迭代加深搜索)
- Uva11214 Guarding the Chessboard【dfs回溯】【习题7-10】
- Guarding the Farm
- 8月21日 XE8移动开发入门(二)XE8的常用控件和AppTethering控件的使用(免费培训)
- Linux 下安装python软件包(pip、nose、virtualenv、distribute )
- xhprof 安装&&使用
- 树与二叉树的性质
- 理解什么是restful
- UVa 11214 - Guarding the Chessboard
- OutMan——Objective-C中成员变量的作用域、@property和@synthesize介绍和使用、构造方法和自定义构造方法
- Easyui Datebox设置可选时间范围
- System.Net.HttpWebRequest.GetRequestStream超时问题
- Android基础之Android的系统体系结构
- 使用Inotify实时同步文件夹
- 读取excel数据并保存为xml
- HDU 1039.Easier Done Than Said?【字符串处理】【8月24】
- 【笔试】20、单例模式