Five-In-a-Row

B. Five-In-a-Row

这个题的数据范围不大只有 10*10 的棋盘,就是下五子棋,直接暴力求解即可。枚举每一个可能落子的位置,然后把棋盘扫一遍,如果出现五子连线即输出 YES,在扫描棋盘的时候可以枚举每一个点的八个方向,看是否存在有五个“X”连在一起,这样代码量会少一些。

// Created by CAD on 2019/8/6.
#include <bits/stdc++.h>

#define ll long long
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f
#define PII pair<int,int>
#define PIII pair<pair<int,int>,int>
#define mst(name, value) memset(name,value,sizeof(name))
#define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)
#define test(n) cout<<n<<endl
using namespace std;

int dir_x[] = {0, 1, 1, 1, 0, 0, -1, -1, -1};
int dir_y[] = {0, 0, 1, -1, 1, -1, 1, 0, -1};
char g[11][11];

bool check(int a,int b)
{
    for (int i = 1; i <= 10; ++i)
        for (int j = 1; j <= 10; ++j)
            for (int k = 1; k <= 8; ++k)
            {
                bool flag = true;
                for (int t = 0; t <= 4; t++)
                {
                    int x = i + dir_x[k] * t;
                    int y = j + dir_y[k] * t;
                    if (x >= 1 && x <= 10 && y >= 1 && y <= 10 && g[x][y] == 'X'){}
                    else
                        flag = false;
                }
                if (flag) return true;
            }
    return false;
}

int main()
{
    for (int i = 1; i <= 10; i++)
        scanf("%s", g[i]+1);
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= 10; j++)
        {
            if (g[i][j] == '.')
            {
                g[i][j] = 'X';
                if (check(i,j))
                    return puts("YES");
                g[i][j] = '.';
            }
        }
    }
    cout << "NO" << endl;
}
posted @ 2019-08-07 10:02  caoanda  阅读(211)  评论(0编辑  收藏  举报