【习题 7-10 Uva11214】Guarding the Chessboard
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
迭代加深搜索。
可以想见最后深度不会很深吧。。
然后皇后之间互相攻击到是允许的。。
就这样
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
/*
一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;
const int O = 50;
const int N = 100,M = 10;
int n,m;
int visx[N],visy[N],visc[M+10],visr[M+10];
char s[M+10][M+10];
int maxnum;
bool ok(){
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (s[i][j]=='X' && !(visr[i] || visc[j]||visx[O+i+j] || visy[O+i-j]))
return false;
return true;
}
bool dfs(int num,int r,int c){
if (num==maxnum){
if (ok()) return true;
return false;
}
if (c==m+1) return dfs(num,r+1,1);
if (r>n) return false;
if (dfs(num,r,c+1)) return true;
visc[c]++;visr[r]++;
visx[r+c+O]++; visy[r-c+O]++;
if (dfs(num+1,r,c+1)) return true;
visc[c]--;visr[r]--;
visx[r+c+O]--; visy[r-c+O]--;
return false;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int kase = 0;
while (cin >>n >> m && n &&m){
memset(visx,0,sizeof visx);
memset(visy,0,sizeof visy);
memset(visr,0,sizeof visr);
memset(visc,0,sizeof visc);
for (int i = 1;i <= n;i++)
cin >> (s[i]+1);
for (maxnum = 0; ;maxnum++){
if (dfs(0,1,1)) break;
}
cout <<"Case "<<++kase<<": "<<maxnum<<endl;
}
return 0;
}
【习题 7-10 Uva11214】Guarding the Chessboard的更多相关文章
- UVA-11214 Guarding the Chessboard (迭代加深搜索)
题目大意:在一个国际象棋盘上放置皇后,使得目标全部被占领,求最少的皇后个数. 题目分析:迭代加深搜索,否则超时. 小技巧:用vis[0][r].vis[1][c].vis[2][r+c].vis[c- ...
- Java50道经典习题-程序10 自由落体
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? import java.util.Scanner; public cl ...
- UVA - 11214 Guarding the Chessboard(迭代加深搜索)
题目: 输入一个n*m的棋盘(n,m<10),某些格子有标记,用最少的皇后守卫(即占据或攻击)所有的标记的格子.输出皇后的个数. 思路: 一开始没有想到用迭代加深搜索,直接dfs结果还没写完就发 ...
- UVA - 11214 Guarding the Chessboard(守卫棋盘)(迭代加深搜索)
题意:输入一个n*m棋盘(n,m<10),某些格子有标记.用最少的皇后守卫(即占据或者攻击)所有带标记的格子. 分析:因为不知道放几个皇后可以守卫所有带标记的格子,即回溯法求解时解答树的深度没有 ...
- 武汉科技大学ACM :1007: 华科版C语言程序设计教程(第二版)习题7.10
Problem Description 输入n(n<100)个字符串,每个字符串长度不超过1000,将他们按字典顺序输出. Input 多组测试样例. 每组第一行有一个整数n表示有n个字符串. ...
- UVA 11214 Guarding the Chessboard
题意: 皇后防御的范围是他所在横.竖.对角线,地图上的#为可以放旗子的地方.问最少放几个皇后能防守所有#. 分析: vis数组开4维,对应行.列.主对角线.副对角线 代码: #include < ...
- 习题3.10 约瑟夫环 josephus问题
/* assume a header */ /* 双向循环链表 */ struct Node; typedef struct Node * PtrToNode; typedef PtrToNode L ...
- Guarding the Chessboard(UVa 11214)
本题题意: 输入一个n*m的棋盘,某些格子有标记,用最少的皇后占据或者攻击所以带标记的格子.皇后的攻击范围为同行同列和同对角线. 可以使用IDA*算法,即从样例可以发现只需要最多5个棋子就可以对棋盘上 ...
- shell习题第10题:打印每个单词的字数
[题目要求] 用shell打印下面这句话中字母数小于6的单词. Bash also interprets a number of multi-character options. [核心要点] for ...
随机推荐
- Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
最近在网上看到一些人讨论到Java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些题目,很多人感到很迷惑,为什么,在jav ...
- FPGA主要应用
FPGA主要应用: 1.逻辑控制(逻辑接口领域) 传统方向,主要用于通信设备的高速接口电路设计,用FPGA处理高速接口的协议,并完成高速的数据收发和交换.FPGA最初以及到目前最广的应用就是在通信领域 ...
- HMACSHA256 Class
https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx Comp ...
- lightoj--1116--Ekka Dokka(水题)
Ekka Dokka Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submit Stat ...
- cf 864 F. Cities Excursions
F. Cities Excursions There are n cities in Berland. Some pairs of them are connected with m directed ...
- React开发实时聊天招聘工具 -第六章 登陆注册(1)
1.基于cookie的用户认证 express 依赖 cookie-parser 2.axios语法: axios.get('/data').then(res=>{ if(res.status= ...
- Python之路:画空心矩形
1.if语句中当要判断多个语句成立才能执行语句时候,可以用or(或,只要其中一个成立就可以执行下一条语句)或者and(只要有其中一个不成立就不会执行下一条语句) 2.在Python中range函数会产 ...
- 二:2.1 字符串与循环中的 while
字符串:字符串是以单引号或双引号括起来的任意文本 创建字符串: str1 = "sunck is a good man!" str3 = "sunckis a nice ...
- CodeForcesGym 100502G Outing
Outing Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Origi ...
- Activiti工作流框架学习(二)——使用Activiti提供的API完成流程操作
可以在项目中加入log4j,将logj4.properties文件拷入到src目录下,这样框架执行的sql就可以输出到到控制台,log4j提供的日志级别有以下几种: Fatal error war ...