题意:输入一个n*m棋盘(n,m<10),某些格子有标记。用最少的皇后守卫(即占据或者攻击)所有带标记的格子。

分析:因为不知道放几个皇后可以守卫所有带标记的格子,即回溯法求解时解答树的深度没有明显的上限,所以使用迭代加深搜索。

将棋盘的每个格子标记为0~n*m-1,依次枚举守卫的皇后个数,枚举当前守卫的皇后个数下所有的放置情况,看是否能全部守卫。(枚举方式i:1~n,j:i+1~n……)

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
char a[][];
int vis[][];
int mark[][];
int kase;
int n, m;
bool judge(){
for(int i = ; i < n; ++i){//判断所有被标记的正方形是否被保护
for(int j = ; j < m; ++j){
if(mark[i][j] && !vis[][i] && !vis[][j] && !vis[][j + i] && !vis[][j - i + n])
return false;
}
}
return true;
}
bool dfs(int cur, int pos, int tot){
if(cur == tot){//放置tot个皇后是否可全保护
if(judge()){
printf("Case %d: %d\n", kase, tot);
return true;
}
return false;
}
else{
for(int i = pos; i < n * m; ++i){//所有点被标记成0~n*m-1
int x = i / m;//当前位置的横坐标
int y = i % m;
int tmp1 = vis[][x];
int tmp2 = vis[][y];
int tmp3 = vis[][x + y];
int tmp4 = vis[][y - x + n];
vis[][x] = vis[][y] = vis[][x + y] = vis[][y - x + n] = ;
if(dfs(cur + , i + , tot)) return true;//此处优化,i + 1下次枚举是当前位置再加1,避免情况重复
vis[][x] = tmp1;
vis[][y] = tmp2;
vis[][x + y] = tmp3;
vis[][y - x + n] = tmp4;
}
}
return false;//枚举当前所有情况不满足
}
int main(){
while(scanf("%d", &n) == ){
if(!n) return ;
++kase;
scanf("%d", &m);
memset(a, , sizeof a);
memset(mark, , sizeof mark);
for(int i = ; i < n; ++i){
scanf("%s", a[i]);
}
for(int i = ; i < n; ++i){
for(int j = ; j < m; ++j){
if(a[i][j] == 'X'){
mark[i][j] = ;
}
}
}
for(int i = ; ; ++i){
memset(vis, , sizeof vis);
if(dfs(, , i)) break;
}
}
return ;
}

UVA - 11214 Guarding the Chessboard(守卫棋盘)(迭代加深搜索)的更多相关文章

  1. UVA 11214 Guarding the Chessboard 守卫棋盘(迭代加深+剪枝)

    暴力,和八皇后很像,用表示i+j和i-j标记主对角线,但是还是要加一些的剪枝的. 1.最裸的暴搜 6.420s,差点超时 2.之前位置放过的就没必要在放了,每次从上一次放的位置开始放 0.400s # ...

  2. UVA - 11214 Guarding the Chessboard(迭代加深搜索)

    题目: 输入一个n*m的棋盘(n,m<10),某些格子有标记,用最少的皇后守卫(即占据或攻击)所有的标记的格子.输出皇后的个数. 思路: 一开始没有想到用迭代加深搜索,直接dfs结果还没写完就发 ...

  3. UVA 11214 Guarding the Chessboard

    题意: 皇后防御的范围是他所在横.竖.对角线,地图上的#为可以放旗子的地方.问最少放几个皇后能防守所有#. 分析: vis数组开4维,对应行.列.主对角线.副对角线 代码: #include < ...

  4. UVA - 11214 Guarding the Chessboard (可重复覆盖,DLX+IDA*)

    题目链接 正解是IDA*+四个方向判重,但由于是个裸的可重复覆盖问题,可以用DLX水过~ 每个格子与放上皇后能干掉的标记连边,跑可重复覆盖DLX.注意要用IDA*来优化,否则会超时. #include ...

  5. UVA-11214 Guarding the Chessboard (迭代加深搜索)

    题目大意:在一个国际象棋盘上放置皇后,使得目标全部被占领,求最少的皇后个数. 题目分析:迭代加深搜索,否则超时. 小技巧:用vis[0][r].vis[1][c].vis[2][r+c].vis[c- ...

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. UVA 10160 Servicing Stations(状态压缩+迭代加深)

    [题目链接] LInk [题目大意] 给出一些点和边,选择一个点就能把这个点和相邻的点都覆盖,求最小点覆盖 [题解] 我们压缩点被覆盖的状态,迭代加深搜索覆盖的最小点数, 当剩余的点全部选上时都无法完 ...

  8. Power Calculus UVA - 1374 迭代加深搜索

    迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...

  9. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

随机推荐

  1. Android 隐藏手机号中间四位和隐藏邮箱地址中间四位

    /** * 手机号用****号隐藏中间数字 * * @param phone * @return */public static String settingphone(String phone) { ...

  2. 「Luogu1402」酒店之王

    传送门 Luogu 解题思路 网络流板子题. 建图细节见代码,也可以参考这道差不多的题 细节注意事项 咕咕咕. 参考代码 #include <algorithm> #include < ...

  3. 「PA2014」Fiolki

    传送门 Bzoj 解题思路 构造法. 对于每一次的倾倒操作,连边 \(newnode\to u,newnode\to v\). 最后所有的反应都会在构造出来的树上的对应两点的 \(\text{LCA} ...

  4. Python 100 Days

    Day 1 python的缺点 执行效率稍低,因此计算密集型任务可以由C/C++编写. 在开发时可以选择的框架太多(如Web框架就有100多个),有选择的地方就有错误. python解释器 官方的Py ...

  5. Logback的AsyncAppender与RollingFileAppender流程解析

    近期工作中涉及到文件记录.文件翻转等操作,思考有没有成熟的代码以便参考. 因此,第一时间就联想到Logback的AsyncAppender以及RollingFileAppender. AsyncApp ...

  6. NPOI,导出Execl,压缩文件zip,发送Email

    private void SendEmail(string emailAddress, string companyName,string proxy, string officer, DataTab ...

  7. 【QSBOJ】组成三位数

    题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数构成1:2:3的比例,试求出所有满足条件的三个三位数. 例如:三个三位数192,384,576满足以上条件. 输出 输出 ...

  8. Ethernet IP TCP UDP 协议头部格式

    The Ethernet header structure is shown in the illustration below: 以太网头部14 bytes Destination Source L ...

  9. 新闻网大数据实时分析可视化系统项目——21、大数据Web可视化分析系统开发

    1.基于业务需求的WEB系统设计 2.下载Tomcat并创建Web工程并配置相关服务 下载tomcat,解压并启动tomcat服务. 1)新建web app项目 创建好之后的效果 2)对tomcat进 ...

  10. R语言 方差稳定化变换与线性变换 《回归分析与线性统计模型》page96

    > rm(list = ls()) > A=read.csv("data96.csv") > A Y N 1 11 0.0950 2 7 0.1920 3 7 0 ...