您的位置:首页 > 其它

Codeforces Beta Round #3

2016-06-05 11:06 337 查看
A - Tic-tac-toe
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice CodeForces
3C

Appoint description:
System Crawler (2016-06-04)

Description

Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts).
The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is
announced.

You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:

illegal — if the given board layout can't appear during a valid game;
the first player won — if in the given board layout the first player has just won;
the second player won — if in the given board layout the second player has just won;
draw — if the given board layout has just let to a draw.

Input

The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).

Output

Print one of the six verdicts: first, second, illegal, the first player won, the
second player won or draw.

Sample Input

Input
X0X
.0.
.X.


Output
second


暴力求解:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;

char a[5][5];
int numx,numo;
bool row(char aim){
for(int i=0;i<3;i++){
int j;
for(j=0;j<3;j++)
if(a[i][j]!=aim)
break;
if(j==3)
return 1;
}
return 0;
}
bool col(char aim){
for(int j=0;j<3;j++){
int i;
for(i=0;i<3;i++)
if(a[i][j]!=aim)
break;
if(i==3)
return 1;
}
return 0;
}
bool xie(char aim){
if((a[1][1]==aim&&a[0][0]==a[1][1]&&a[1][1]==a[2][2])||(a[1][1]==aim&&a[0][2]==a[1][1]&&a[2][0]==a[1][1]))
return 1;
else
return 0;
}
void judge(){
if((numx-numo)==0||(numx-numo)==1){
int ans1=0;
ans1=col('X');
ans1+=row('X');
ans1+=xie('X');
int ans2=0;
ans2=col('0');
ans2+=row('0');
ans2+=xie('0');
if(ans1&&!ans2){
if(numx-numo==1)
cout<<"the first player won"<<endl;
else
cout<<"illegal"<<endl;
return ;
}else if(!ans1&&ans2){
if(numx==numo)
cout<<"the second player won"<<endl;
else
cout<<"illegal"<<endl;
return ;
}else if(ans1&&ans2){
cout<<"illegal"<<endl;
}else if(numx+numo==9){
cout<<"draw"<<endl;
}else{
if(abs(numx-numo)==1)
cout<<"second"<<endl;
else if(numx==numo)
cout<<"first"<<endl;
}
}
else
cout<<"illegal"<<endl;
}
int main()
{
numx=0;numo=0;
for(int i=0;i<3;i++)
{
gets(a[i]);
for(int j=0;j<3;j++)
if(a[i][j]=='X')
numx++;
else if(a[i][j]=='0')
numo++;
}
judge();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: