HDU 1045 (DFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045
题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个O。
解题思路:
题目规模比较小(4*4),可以DFS解决。
对于一个点,要么放,要么不放。
放的话条件必须是上下左右四个方向扫到边界且不首先碰到X。
可以只对放的点进行标记,而对于不放的点不进行标记,这样当dep>n*n的时候及时return就行了。
注意每次dfs只需要按顺序考虑一个点,而不要同时对整个棋盘所有非X点同时dfs,否则就爆了。
原因是,每次只关联一个点,其它点都是不关联的,如果dfs等于做了大量重复计算。
#include "cstdio"
#include "string"
#include "cstring"
#include "iostream"
using namespace std;
int n,vis[][],ans;
char map[][];
struct status
{
int x,y;
char type;
status(int x,int y,char type):x(x),y(y),type(type) {}
status() {}
}P[];
bool judge(int X,int Y)
{
if(map[X][Y]=='X') return false;
for(int i=X-; i>=&&map[i][Y]!='X'; i--) if(vis[i][Y]) return false;
for(int i=X+; i<=n&&map[i][Y]!='X'; i++) if(vis[i][Y]) return false;
for(int i=Y-; i>=&&map[X][i]!='X'; i--) if(vis[X][i]) return false;
for(int i=Y+; i<=n&&map[X][i]!='X'; i++) if(vis[X][i]) return false;
return true;
}
void dfs(int p,int s)
{
if(p>n*n) {ans=max(ans,s);return;}
dfs(p+,s);
vis[P[p].x][P[p].y]=true;
if(judge(P[p].x,P[p].y)) dfs(p+,s+);
vis[P[p].x][P[p].y]=false; }
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n&&n)
{
memset(vis,,sizeof(vis));
int cnt=;ans=;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
P[++cnt]=status(i,j+,tt[j]);
map[i][j+]=tt[j];
}
}
dfs(,);
cout<<ans<<endl;
}
}
| 11909497 | 2014-10-19 11:59:35 | Accepted | 1045 | 0MS | 292K | 1335 B | C++ | Physcal |
HDU 1045 (DFS搜索)的更多相关文章
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- HDU 1010 (DFS搜索+奇偶剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...
- hdu 1010 dfs搜索
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1045 dfs + 回溯
题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...
- HDU 1045 dfs
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDOJ(HDU).1045 Fire Net (DFS)
HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- hdu 1312:Red and Black(DFS搜索,入门题)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
随机推荐
- 在服务器上远程链接另一台服务器的数据库的方法how to connet the database from the other host
iwangzheng.com 16:57 [root@a02.cmsapi]$ mysql -u<username> -p<password> -h10.103.xx.xx W ...
- 服务器部署之 cap deploy:setup
文章是从我的个人博客上粘贴过来的, 大家也可以访问 www.iwangzheng.com $ cap deploy:setup 执行到这一步的时候会时间较长,可以直接中断 * executing &q ...
- bellman ford优先队列优化简介模板
#include<iostream>#include<cstdio>#include<utility>#include<queue>#include&l ...
- Linux jstack命令详解
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息. 如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack ...
- systemd在各个linux发行版的普及
后面我要说下自己的意见: 原则如果阻碍了进步,那还算个屁,不客气地说,UNIX 原则已经过时了. 移植性问题:我除了 Mac 外不用任何 BSD 系统,当然 Mac 上一般只做开发不做运维(但就算如此 ...
- auto_ptr浅析(转载)
转载自http://www.cnblogs.com/qytan36/archive/2010/06/28/1766555.html auto_ptr是C++标准库中(<utility>)为 ...
- 【wireshark】打开后显示There are no interfaces on which a capture can be done
解决方式:用管理员方式打开wireshark即可
- July 16th, Week 29th Saturday, 2016
A long road tests a horse's strength and a long task proves a man's heart. 路遥知马力,日久见人心. Do you have ...
- .NET生成word文档服务器配置常见问题
注意:安装office2003的时候一定要选择 "完全安装" 而不是 "典型安装" 错误:System.Runtime.InteropServices.COME ...
- 【Chrome】手动下载和安装Adblock Plus的方法
由于强大的GFW,导致很多Google的站点没法访问,也就没法直接安装chrome的插件了,网页都打不卡, 那么几乎是必备的Adblock Plus如何下载安装呢? 1.访问官方网站: https:/ ...