模拟&贪心 D CodeForces-825B Five In a Row

题意很简单:模拟五子棋中X子是否是只差一步就赢了。

用了很强的涛爷的很强的思路:依次模拟每个空位填上‘X’是否会有连成5子的存在,而不是十分麻烦地寻找是否有填上‘X’就会赢的空位存在。

一开始没过因为又又又又又又又没注意到scanf读字符%c时会读上回车,还是曹老师帮忙看出来的错误。可以说是很愚蠢了。此处读字符串%s更简捷。

上代码:

#include <cstdio>
#include <cstdbool>

bool check(int x,int y,char chess[10][10]);

int main()
{
    char chess[10][10];
    int i,j;

    for(i=0;i<10;i++)
        scanf("%s",&chess[i]);    //没错,就是这个愚蠢的地方。
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            if(chess[i][j]=='.'&&check(i,j,chess)){
                printf("YES");
                return 0;
            }
    printf("NO");

    return 0;
}

bool check(int x,int y,char chess[10][10])    //写一个函数简直妙
{
    int i,j,h,num;

    chess[x][y]='X';    //模拟填子
    num=0;    //每个判断前都要记得清空
    for(i=x,j=0;j<10;j++)    //判断行
        if(chess[i][j]=='X'){
            num++;
            if(num==5)
                return true;
        }else num=0;
    num=0;
    for(j=y,i=0;i<10;i++)    //判断列
        if(chess[i][j]=='X'){
            num++;
            if(num==5)
                return true;
        }else num=0;
    num=0;
    for(i=x,j=y;i>0&&j>0;i--,j--)    //判断左上右下的斜,从边界开始遍历
        ;
    for(;i<10&&j<10;i++,j++)
        if(chess[i][j]=='X'){
            num++;
            if(num==5)
                return true;
        }else num=0;
    num=0;
    for(i=x,j=y;i<10-1&&j>0;i++,j--)    //判断右上左下的斜,也是从边界开始遍历(其实这样有点儿蠢)
        ;
    for(;i>=0&&j<10;i--,j++)
        if(chess[i][j]=='X'){
            num++;
            if(num==5)
                return true;
        }else num=0;
    chess[x][y]='.';    //一定记得改回来!!!!!
    return false;
}


猜你喜欢

转载自blog.csdn.net/daddy_hong/article/details/79209944