题目链接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搜索)的更多相关文章

  1. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  2. HDU 1010 (DFS搜索+奇偶剪枝)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意:给定起点和终点,问刚好在t步时能否到达终点. 解题思路: 4个剪枝. ①dep&g ...

  3. hdu 1010 dfs搜索

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. HDU 1045 dfs + 回溯

    题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...

  5. HDU 1045 dfs

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. HDOJ(HDU).1045 Fire Net (DFS)

    HDOJ(HDU).1045 Fire Net [从零开始DFS(7)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HD ...

  7. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  8. hdu 1312:Red and Black(DFS搜索,入门题)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. hdu 1045 Fire Net(最小覆盖点+构图(缩点))

    http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB   ...

随机推荐

  1. function foo(){}、(function(){})、(function(){}())等函数区别分析

    前面一段时间,看到(function(){}),(function(){}())这些函数就犯晕,不知道它到底是什么意思,为什么函数外要加小括号,函数后要加小括号,加和不加到底有什么区别……一直犯迷糊, ...

  2. HDU 1864最大报销额 01背包问题

    B - 最大报销额 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  3. django-cms 代码研究(六)plugin的深入分析

    示例代码: https://github.com/divio/djangocms-picture 以上一个图片的插件,安装后可在页面中添加图片,效果如下图: 以此为切入点,分析plugin的逻辑: 分 ...

  4. Merge Two Sorted Arrays

    Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...

  5. 16.O(logn)求Fibonacci数列[Fibonacci]

    [题目] log(n)时间Fib(n),本质log(n)求a^n. [代码]  C++ Code  12345678910111213141516171819202122232425262728293 ...

  6. Linux下挂载NTFS格式的U盘或硬盘

    我们知道在Linux下挂载fat32的U盘非常容易,使用mount /dev/drive_name /mnt/指定目录这样就可以挂载了,但是如果U盘或者硬盘的格式是NTFS的话,那么Linux是不能识 ...

  7. CodeForces - 416A (判断大于小于等于 模拟题)

    Guess a number! Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  8. 使用webstorm调试node程序

    前言 相信大家接触过不少node代码了,如果你应用的比较初级或者针对你的项目不需要接触过深的node代码,也许你仅仅需要简单的console.log('your variable')就完全满足你的需要 ...

  9. 说说localStorage

    HTML5的本地存储是大势所趋,如果仅存储在内存中,则是sessionStorage,他们的语法都是一样,仅仅是一个存储在本地文件系统中,另一个存储在内存中(随着浏览器的关闭而消失),其语句如下: l ...

  10. C语言实现大数据除法

    本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...