模拟题,暴力判断。
感觉会很麻烦、实际一条条写下来也还好啦~
//Tic-tac-toe 2013.7.21 #include <iostream> char win(char t[3][3]) { int r1=0,r2=0,r3=0,c1=0,c2=0,c3=0,x1=0,x2=0; if(t[0][0]!='.'&&(t[0][0]==t[0][1]&&t[0][0]==t[0][2])) r1=1; if(t[1][1]!='.'&&(t[1][0]==t[1][1]&&t[1][0]==t[1][2])) r2=1; if(t[2][2]!='.'&&(t[2][0]==t[2][1]&&t[2][0]==t[2][2])) r3=1; if(t[0][0]!='.'&&(t[0][0]==t[1][0]&&t[0][0]==t[2][0])) c1=1; if(t[1][1]!='.'&&(t[0][1]==t[1][1]&&t[0][1]==t[2][1])) c2=1; if(t[2][2]!='.'&&(t[0][2]==t[1][2]&&t[0][2]==t[2][2])) c3=1; if(t[1][1]!='.'&&(t[0][0]==t[1][1]&&t[0][0]==t[2][2])) x1=1; if(t[1][1]!='.'&&(t[2][0]==t[1][1]&&t[1][1]==t[0][2])) x2=1; if(r1+r2+r3+c1+c2+c3+x1+x2>1) { if(r1+c1==2||r1+x1==2||c1+x1==2) return t[0][0]; if(r1+c2==2) return t[0][1]; if(r1+c3==2||r1+x2==2||x2+c3==2) return t[0][2]; if(r2+c1==2) return t[1][0]; if(r2+c3==2) return t[1][2]; if(r2+c2==2||r2+x1==2||r2+x2==2||c2+x1==2||c2+x2==2||x1+x2==2) return t[1][1]; if(r3+c1==2||r3+x2==2||c1+x2==2) return t[2][0]; if(r3+c2==2) return t[2][1]; if(r3+c3==2||r3+x1==2||x1+c3==2) return t[2][2]; return 'i'; } if(r2||c2||x1||x2) return t[1][1]; if(r1||c1) return t[0][0]; if(r3||c3) return t[2][2]; return '.';//no win } using namespace std; const int maxn = 9; int main() { char tb[3][3]; cin>>tb[0][0]>>tb[0][1]>>tb[0][2]; cin>>tb[1][0]>>tb[1][1]>>tb[1][2]; cin>>tb[2][0]>>tb[2][1]>>tb[2][2]; int cX = 0,c0 = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(tb[i][j]=='X') cX++; else if(tb[i][j]=='0') c0++; } } if(c0>4 || cX<c0 || cX>c0+1) { cout<<"illegal"<<endl; return 0; } char c = win(tb); if('X'== c ) { if(cX==c0) cout<<"illegal"<<endl; else cout<<"the first player won"<<endl; } else if('0'==c) { if(cX==c0+1) cout<<"illegal"<<endl; else cout<<"the second player won"<<endl; } else if('i'==c) cout<<"illegal"<<endl; else if((cX+c0==9)&&('.'== c )) cout<<"draw"<<endl; else if(cX+c0<9 && ('.'== c )) { if(cX==c0) cout<<"first"<<endl; else if(cX==c0+1) cout<<"second"<<endl; // else cout<<"unexpected fault!1"<<endl; } //else // cout<<"unexpected fault!2"<<endl; return 0; }