HDU5546 Ancient Go DFS
题意:给定一个9*9的棋盘,问黑子能否在下一步将白子围住(四面)。
由于数据不大,可以直接将'.'换成'x',用DFS搜索。
#include<cstdio>
#include<cstring>
using namespace std;
char chess[11][11];
bool visit[11][11];
int turnx[4]={1,-1,0,0};
int turny[4]={0,0,1,-1};
int flag;
bool in(int x,int y)
{
if(x<0||y<0||x>=9||y>=9)
return 0;
return 1;
}
void dfs(int x,int y)
{
if(chess[x][y]=='.')//如果有出路,则标记为0,说明在该点下子无法获胜
{
flag=0;
return;
}
for(int k=0;k<4;k++)
{
int nx=x+turnx[k];
int ny=y+turny[k];
if(in(nx,ny)&&!visit[nx][ny]&&chess[nx][ny]!='x')
{
visit[nx][ny]=1;
dfs(nx,ny);
}
}
}
int solve()
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(chess[i][j]=='.')
{
chess[i][j]='x';
for(int k=0;k<4;k++)
{
int nx=i+turnx[k];
int ny=j+turny[k];
if(in(nx,ny)&&chess[nx][ny]=='o')
{
memset(visit,0,sizeof(visit));
visit[nx][ny]=1;
flag=1;
dfs(nx,ny);
if(flag)
return 1;//直接返回,跳出循环
}
}
chess[i][j]='.';//还原
}
}
}
return 0;
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
for(int i=0;i<9;i++)
scanf("%s",chess[i]);
flag=1;
memset(visit,0,sizeof(visit));
int ans=solve();
if(ans)
printf("Case #%d: Can kill in one move!!!\n",cas++);
else
printf("Case #%d: Can not kill in one move!!!\n",cas++);
}
return 0;
}
HDU5546 Ancient Go DFS的更多相关文章
- K - Ancient Messages(dfs求联通块)
K - Ancient Messages Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- 2015南阳CCPC G - Ancient Go dfs
G - Ancient Go Description Yu Zhou likes to play Go with Su Lu. From the historical research, we fou ...
- HDU 3839 Ancient Messages(DFS)
In order to understand early civilizations, archaeologists often study texts written in ancient lang ...
- hdu 3839 Ancient Messages (dfs )
题目大意:给出一幅画,找出里面的象形文字. 要你翻译这幅画,把象形文字按字典序输出. 思路:象形文字有一些特点,分别有0个圈.1个圈.2个圈...5个圈.然后dfs或者bfs,就像油井问题一样,找出在 ...
- Ancient Go---hdu5546(dfs爆搜CCPC题目)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可 ...
- Ancient Go(简单DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 AC代码: #include<iostream> #include<cstdi ...
- Ancient Printer[HDU3460]
Ancient Printer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Tot ...
- HDU1010 DFS+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- BOM组件物料重复检查
好吧,今天同事让做个BOM组件物料重复检查 网上有很多例子都是在保存的时候检查的,用的是BADI :BOM_UPDATE 自己也试了一下,麻烦....很麻烦...尤其是在重复检查的时候: METHOD ...
- SQL Server数据库(SQL Sever语言 存储过程及触发器)
存储过程:就像函数一样的会保存在数据库中-->可编程性-->存储过程 创建存储过程: 保存在数据库表,可编程性,存储过程create proc jiafa --需要的参数@a int,@b ...
- arrow css
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 手动实现ArrayList
public interface List { public void insert(int i,Object obj)throws Exception; public void delete(int ...
- outlook 用宏发邮件
经常发面试邮件,通常只是修改一下收件人邮箱地址,和收件人姓名,其他全部一致,有木有发现每次都用用outlook写邮件很麻烦? 使用宏发邮件,就会不麻烦了,直接修改下称呼,修改下收件人地址,按下F5,就 ...
- True bar
真彩bar /***========================================================================= ==== ==== ==== D ...
- 利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理
Nginx的好处相信我不必多说了,它作为一个相当轻量级的开源Web 服务器以及反向代理服务器而深受欢迎.越来越多的公司已经对它产生兴趣,包括我们公司的许多部门,利用它进行负载均衡和资源管理,之前写过一 ...
- bzoj 1951: [Sdoi2010]古代猪文
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #defin ...
- POJ 1050 To the Max 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
- Linux下的多线程编程
1 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的 Unix也支持线程的概念,但是在一个进程(proces ...