题目来源:https://vjudge.net/problem/HDU-5546
【题意】
9*9的图中,o是白旗,x是黑棋,按照围棋的思想,问下一步如果轮到黑棋走,能不能封死至少一个白棋,输出相应语句。
【思路】
利用数组模拟寻找”o”点附近的”.”,如果只有一个,那么就是可行。
但是会存在多个”o”连在一起的情况,所以利用深搜,找到下一个o点,继续寻找它周边的“.”。
【代码】
#include<map>
#include<stack>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
int d[4][2]= {{0,-1},{0,1},{-1,0},{1,0}};
char s[10][10];
bool vo[10][10];
bool vd[10][10];
int cnt;
void dfs(int x,int y)
{
for(int i=0; i<4; i++)//当前o点
{
int fx=x+d[i][0];
int fy=y+d[i][1];
if(fx<9&&fx>=0&&fy<9&&fy>=0&&s[fx][fy]=='.'&&!vd[fx][fy])
{
vd[fx][fy]=1;
cnt++;
}
}
for(int i=0; i<4; i++)//多个o
{
int fx=x+d[i][0];
int fy=y+d[i][1];
if(fx<9&&fx>=0&&fy<9&&fy>=0&&s[fx][fy]=='o'&&!vo[fx][fy])
{
vo[fx][fy]=1;
dfs(fx,fy);
}
}
}
int main()
{
int T,t=0;
scanf("%d%*c",&T);
while(T--)
{
mem(vo,0);
int flag=0;
for(int i=0; i<9; i++)
scanf("%s",s[i]);
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(s[i][j]=='o'&&!vo[i][j])
{
mem(vd,0);
cnt=0;
dfs(i,j);
if(cnt==1)
{
flag=1;
break;
}
}
}
if(flag)
break;
}
if(flag)
printf("Case #%d: Can kill in one move!!!\n",++t);
else
printf("Case #%d: Can not kill in one move!!!\n",++t);
}
}