深夜切題——ly與lyon的終極巔峰對決
阿新 • • 發佈:2019-02-13
ly與lyon的終極巔峰對決
時間限制:1000MS 記憶體限制:65535K
Description
從前有一天,ly與lyon在討論人工智慧裡面的博弈問題,恰好,他們提到了五子棋。 當然,這裡說的五子棋是指無禁手(不知道什麼是禁手的也不用管了,跟這題沒關係)的五子棋: 黑先下,黑白輪流下,最先在橫豎斜任一方向上形成連續的5個子(或以上)就算贏。 對此,ly和lyon都有自己的一套判斷局勢的演算法,並且根據自己的想法各寫了一個判斷局況的程式。然而,他們都覺得自己的程式要比對方的優秀,所以, 他們稍作改良,做成了自動決策的對局程式,並拿出來互相pk。目前需要一個自動判斷勝負的程式,即最先出現5連子的判勝。
輸入格式
第1行輸入兩個數n和m,用空格分開,n為棋盤橫縱座標的最大值,m為步數:
1<=n<=1000,0<m<=n*n
第2行到第m+1行為第一步到第m步的座標,每行兩個數,用空格分開:
x和y,1<=x,y<=n
輸入保證不存在重複下子。
(出題人LRC)
輸出格式
輸出首次分出勝負那一步的序號(第一步為1),如果走完了都沒有分出勝負,輸出“baga”。
輸入樣例
5 11
3 3
2 3
2 4
4 3
4 2
3 4
1 5
3 2
5 1
1 1
1 2
輸出樣例
9
一看題,憑我的水平就暴力掃吧。。。打完機腦子裡沒有其他方法(蠢。。。)(run)
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <cmath> //#include <cctype> #include <queue> #include <vector> #include <string> #include <stack> #include <set> #include <map> //#include <sstream> #include <iostream> #include <bitset> #include <algorithm> using namespace std; #define MP make_pair #define PB push_back #define mst(a,b) memset((a),(b),sizeof(a)) #define TEST cout<<"*************************"<<endl #define rep(s,n,up) for(int i = (s); i < (n); i+=(up)) #define per(n,e,down) for(int i = (n); i >= (e); i-=(down)) #define rep1(s,n,up) for(int j = (s); j < (n); j+=(up)) #define per1(n,e,down) for(int j = (n); j >= (e); j-=(down)) typedef long long LL; typedef unsigned long long uLL; typedef pair<int, int> Pii; typedef vector<int> Vi; typedef vector<Pii> Vii; const int inf = 0x3f3f3f3f; const LL INF = (1uLL << 63) - 1; const double Pi = acos(-1.0); const int maxn = (1 << 16) + 7; const uLL Hashmod = 29050993; const double esp=1e-6; #define local int chessboard[1001][1001],n,m,flag=0; void checkrow(int i,int j,int step) { int c=0,mark; if(step%2)mark=1;else mark=2; int y=j; while(y-1>=0&&chessboard[i][--y]==mark)++c; y=j; while(y+1<=n&&chessboard[i][++y]==mark)++c; if(c>=4)flag=1; } void checkcolumn(int i,int j,int step) { int c=0,mark; if(step%2)mark=1;else mark=2; int x=i; while(x-1>=0&&chessboard[--x][j]==mark)++c; x=i; while(x+1<=n&&chessboard[++x][j]==mark)++c; if(c>=4)flag=1; } void checkxright(int i,int j,int step) { int c=0,mark; if(step%2)mark=1;else mark=2; int x=i,y=j; while(x-1>=0&&y-1>=0&&chessboard[--x][--y]==mark)++c; x=i,y=j; while(x+1<=n&&y+1<=n&&chessboard[++x][++y]==mark)++c; if(c>=4)flag=1; } void checkxleft(int i,int j,int step) { int c=0,mark; if(step%2)mark=1;else mark=2; int x=i,y=j; while(x-1>=0&&y+1<=n&&chessboard[--x][++y]==mark)++c; x=i,y=j; while(x+1<=n&&y-1>=0&&chessboard[++x][--y]==mark)++c; if(c>=4)flag=1; } int main() { #ifdef local freopen("input.txt", "r", stdin); //freopen("output.txt","w",stdout); #endif //ios::sync_with_stdio(0); //cin.tie(); mst(chessboard,0); int i,j,k; scanf("%d%d",&n,&m); int step=0; while(step++<m){ scanf("%d%d",&i,&j); if(!flag){ if(step%2)chessboard[i][j]=1; else chessboard[i][j]=2; if(!flag) {checkrow(i,j,step);k=step;} if(!flag) {checkcolumn(i,j,step);k=step;} if(!flag) {checkxright(i,j,step);k=step;} if(!flag) {checkxleft(i,j,step);k=step;} } } /*rep(0,n+1,1) {rep1(0,n+1,1) printf("%d ",chessboard[i][j]); printf("\n");}*/ if(flag)printf("%d\n",k); else printf("baga\n"); return 0; }