1. 程式人生 > >2017 計蒜之道 初賽 第一場 A. 阿里的新遊戲

2017 計蒜之道 初賽 第一場 A. 阿里的新遊戲

這裡寫圖片描述
一道列舉題目, 列舉所有的組合,然後根據這些組合判斷棋子在不在同一條線上

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
    int x, y;
};

node ss[100], tt[4];
int jj;
int is_node(node q)//返回vis陣列最小下標
{
    for(int i = 0; i<= 100; i++)
        if(q.x
== ss[i].x && q.y == ss[i].y) return i; return 0; } bool cmpx(const node &a, const node &b){return a.x < b.x;} bool cmpy(const node &a, const node &b){return a.y < b.y;} int is_okx(node tt[4])//對於ok==0時有特殊遺漏的判斷 { node qq[4]; for(int i = 1; i <= 3; i++) qq[i]
= tt[i]; sort(qq+1, qq+4, cmpx); if(qq[1].x+qq[3].x == -1 || qq[1].x+qq[3].x == 1) return 1; return 0; } int is_oky(node tt[4]) { node qq[4]; for(int i = 1; i <= 3; i++) qq[i] = tt[i]; sort(qq+1, qq+4, cmpy); if(qq[1].y+qq[3].y == -1 || qq[1].y+qq[3].y == 1) return 1; return
0; } int n, m; void dfs(int cur) { if(cur > 3) { if(tt[1].x == tt[2].x && tt[1].x == tt[3].x && tt[3].x == tt[2].x) { int ok = tt[1].y + tt[2].y + tt[3].y; if(ok == 0 || ok == -6 || ok == 6) { if(is_oky(tt)) return; jj++; } return; } if(tt[1].y == tt[2].y && tt[1].y == tt[3].y && tt[3].y == tt[2].y) { int ok = tt[1].x + tt[2].x + tt[3].x; if(ok == 0 || ok == -6 || ok == 6) { if(is_okx(tt)) return; jj++; } return; } return; } int s = (cur == 1 ? 1 : is_node(tt[cur-1])+1); //保證最小序列舉組合 for(int i = s; i <= n; i++) { tt[cur] = ss[i]; dfs(cur+1); } } int main() { while(cin >> n >> m && n >= 3 && m >= 3 && n <= 9 && m <= 9) { memset(tt, 0, sizeof(tt)); memset(ss, 0, sizeof(ss)); jj = 0; for(int i = 1; i <= n; i++) cin >> ss[i].x >> ss[i].y; int a, b; while(m--) cin >> a >> b; dfs(1); cout << jj << endl; } return 0; }