五子棋

https://www.acwing.com/problem/content/1326/

 

思路:

  枚举起点
正反方向
如果满足.输出

#include <bits/stdc++.h>
using namespace std;
int n;
int g[16][16];
int dx[4] = {-1,-1,0,1};
int dy[4] = {0,1,1,1};
bool sucessful;
int state,step;
int main(){
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        int x,y;
        cin >> x >> y;
        if(i % 2) g[x][y] = 1;
        else g[x][y] = 2;
        for(int j = 0; j < 4; j++) {
            int l = 0, r = 0;
            int a, b;
            while (true) {
                a = x + dx[j] * (l + 1);b = y + dy[j] * (l + 1);
                if(a < 1 || a > 15 || b < 1 || b > 15)
                    break;
                if(g[x][y] != g[a][b])
                    break;
                l++;
            }
            while (true) {
                a = x - dx[j] * (r + 1);b = y - dy[j] * (r + 1);
                if(a < 1 || a > 15 || b < 1 || b > 15)
                    break;
                if(g[x][y] != g[a][b])
                    break;
                r++;
            }
            if(l + r + 1>= 5){
                sucessful = true;
                break;
            }
        }
        if(sucessful){
            state = g[x][y];
            step = i;
            break;
        }
    }

    if(state == 2)
        cout << "B" << " " << step;
    else if(state == 1)
        cout << "A" << " " << step;
    else cout << "Tie";
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/xcfxcf/p/12358290.html