Educational Codeforces Round 25 B. Five-In-a-Row

Topic website: Educational Codeforces Round 25 B. Five-In-a-Row

Topic analysis:

The title is equivalent to playing Gobang to determine whether the current player of'X'can win the next move. That is, whether there is a replacement of one of the'X', so that there are five consecutive'X' (horizontal or vertical or diagonal).

After exhausting each'.', that is,'. 'replaced by'X', judge whether it is five consecutive'X' (including three directions, write for loop directly)?

Code:

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int SIZE = 20;
char mp[SIZE][SIZE];

int main(int argc, char const *argv[])
{
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 10; ++j)
        {
            scanf("%c", &mp[i][j]);
        }
        getchar();
    }

    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 10; ++j)
        {
            if (mp[i][j] == '.')
            {
                int cnt;

                // Towards the left
                cnt = 1;
                for (int k = j - 1; k >= 0; --k)
                {
                    if (mp[i][k] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // Towards the right
                for (int k = j + 1; k < 10; ++k)
                {
                    if (mp[i][k] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // Upward
                cnt = 1;
                for (int k = i - 1; k >= 0; --k)
                {
                    if (mp[k][j] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // down
                for (int k = i + 1; k < 10; ++k)
                {
                    if (mp[k][j] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // From right down to left up
                cnt = 1;
                for (int k = i - 1, q = j - 1; k >= 0 && q >= 0; --k, --q)
                {
                    if (mp[k][q] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // From top left to bottom right
                for (int k = i + 1, q = j + 1; k < 10 && q < 10; ++k, ++q)
                {
                    if (mp[k][q] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // From top right to bottom left
                cnt = 1;
                for (int k = i + 1, q = j - 1; k < 10 && q >= 0; ++k, --q)
                {
                    if (mp[k][q] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }

                // From bottom left to top right
                for (int k = i - 1, q = j + 1; k >= 0 && q < 10; --k, ++q)
                {
                    if (mp[k][q] == 'X')
                    {
                        ++cnt;
                        if (5 == cnt)
                        {
                            printf("YES\n");
                            return 0;
                        }
                    }
                    else break;
                }
            }
        }
    }

    printf("NO\n");
    return 0;
}

Posted by jbingman on Sun, 10 Feb 2019 11:00:18 -0800