CodeForces - 825 Five-In-a-Row

Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.

In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.

Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.

Input
You are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.

It is guaranteed that in the current arrangement nobody has still won.

Output
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.

Example
Input
XX.XX.....
.....OOOO.
..........
..........
..........
..........
..........
..........
..........
..........
Output
YES
Input
XXOXX.....
OO.O......
..........
..........
..........
..........
..........
..........
..........
..........
Output

NO

搜索
本题讲的是一个下五子棋问题,再走一步棋,是否能赢,如果能赢,输出YES,否,输出NO。直接用暴力找会比较方便。
从.开始找,
1 如果用这个点出发它的那一横行连续有四个以上的'X',标记一下
2 如果从这个 . 出发,如果这一竖行有4个以上的'X',则标记一下  ;
3 从这个 . 出发,如果左上和右下连续有4个以上的点,则标记一下
4  从这个 . 出发,如果右上和左下连续有4个以上的点,则标记一下

#include<stdio.h>
#include<string.h>
char str[20][20];
int main()
{
    while(~scanf("%s",str[0]))
    {
        int i,j,k,r;
        for(i = 1; i < 10; i++)
            scanf("%s",str[i]);
        int f = 0,sum;
        for(i = 0; i < 10 && !f; i++)
            for(j = 0; j < 10 && !f; j++)
            {
                if(str[i][j] == '.')
                {
                    sum = 0;
                    for(k = j+1; str[i][k] == 'X' && k < 10; k++)    //横
                        sum++;
                    for(k = j-1; str[i][k] == 'X' && k >= 0; k--)
                        sum++;
                    if(sum >= 4)
                    {
                        f = 1;
                        break;
                    }

                    sum = 0;
                    for(k = i+1; str[k][j] == 'X' && k < 10; k++)   //竖
                        sum++;
                    for(k = i-1; str[k][j] == 'X' && k >= 0; k--)
                        sum++;
                    if(sum >= 4)
                    {
                        f = 1;
                        break;
                    }

                    sum = 0;
                    for(r = i+1, k = j+1; str[r][k] == 'X'&& r < 10&&k < 10 ; r++, k++)  //右下
                        sum++;
                    for(r = i-1, k = j-1; str[r][k] == 'X'&& r >= 0&&k >= 0 ; r--, k--)   //左上
                        sum++;
                    if(sum >= 4)
                    {
                        f = 1;
                        break;
                    }

                    sum = 0;
                    for(r = i+1, k = j-1; str[r][k] == 'X'&& r < 10 && k>= 0 ; r++, k--)  //左下
                        sum++;
                    for(r = i-1, k = j+1; str[r][k] == 'X'&&r >= 0  && k < 10 ; r--, k++)   //右上
                        sum++;
                    if(sum >= 4)
                    {
                        f = 1;
                        break;
                    }
                }
            }
        if(f)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/Qin7_Victory/article/details/75674513
今日推荐