Ancient Go

题目

给出多个古代围棋棋盘(9*9),求问当前执x子的玩家能否在下一步吃掉对方至少一个子。

I/O

//Sample input
2

.......xo
.........
.........
..x......
.xox....x
.o.o...xo
..o......
.....xxxo
....xooo.

......ox.
.......o.
...o.....
..o.o....
...o.....
.........
.......o.
...x.....
........o
//Sample Output
Case #1: Can kill in one move!!!
Case #2: Can not kill in one move!!!

算法

对区域判断类连通图。
1. 判断o子的连通块,检查周围;
2. 递归得到o子周围的空位数,刚好等于1时有解。
3. 复杂度:n^2之内,搜索棋盘记录状态即可。

优化
1. 棋盘初始化为11*11,将原棋盘外一圈置为x,免去循环时的边界判断。
2. 传递连通区域的空位数,并判重。用isMark和is数组记录状态。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

char chess[11][11];
bool isMark[11][11];
int dir[2][4] = {-1, 0, 0, 1,  0, 1, -1, 0};

int check(int i, int j, bool is[11][11])
{
    int unput = 0;
    for(int l = 0; l<4; l++)
    {   char c = chess[i+dir[0][l]][j+dir[1][l]];
        if(c == '.' && is[i+dir[0][l]][j+dir[1][l]] == 0)
        {   is[i+dir[0][l]][j+dir[1][l]] = 1;
            unput++;
        }
        if(c == 'o' && isMark[i+dir[0][l]][j+dir[1][l]] == 0)
        {   isMark[i+dir[0][l]][j+dir[1][l]] = 1;
            unput+=check(i+dir[0][l], j+dir[1][l], is);
        }
    }
    return unput;
}

int main()
{   int t;
    scanf("%d",&t);
    int kase = 1;
    while(t--)
    {   memset(chess, 0, sizeof(chess));
        memset(isMark, 0, sizeof(isMark));
        int flag = 0;

        for(int i = 1; i<=9; i++) for(int j =1; j<=9; j++ )
            cin >> chess[i][j];
        for(int i = 0; i<=11; i++)
        {   chess[0][i] = 'x';
            chess[10][i] = 'x';
            chess[i][0] = 'x';
            chess[i][10] = 'x';
        }

        int unset;
        bool is[11][11];

        for(int i = 1; i<=9; i++) for(int j = 1; j<=9; j++)
        {
            if(chess[i][j] == 'o' && isMark[i][j] == 0)
            {   memset(is, 0, sizeof(is));
                unset = 0;
                isMark[i][j] = 1;
                unset = check(i, j, is);
                if(unset == 1){flag = 1; break;}
            }
        }
        if(flag) printf("Case #%d: Can kill in one move!!!\n", kase++);
        else printf("Case #%d: Can not kill in one move!!!\n", kase++);
    }
    return 0;
}   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值