C. Tic-tac-toe
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard output
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.
Examples
input
X0X
.0.
.X.
output
second
题目大意:九宫格,大家小时候都玩过,规则简单就是判断横,竖,斜对角有没有三个连在一起同样的就获胜。重点想好不合格的状态。先手的获胜要比后手多一步,后手获胜的话要和先手步数一样等等。一个简单模拟题。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<stack>
#include<iostream>
#include<algorithm>
const int maxn=1e5+10;
const int INF=0x3f3f3f3f;
using namespace std;
char str[5][5];
int x,y;
bool check_connect(char c){
int i,j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(str[i][j]!=c) break;
}
if(j==3) return true;
for(j=0;j<3;j++){
if(str[j][i]!=c) break;
}
if(j==3) return true;
}
for(i=0;i<3;i++){
if(str[i][i]!=c) break;
}
if(i==3) return true;
for(i=2;i>=0;i--){
if(str[i][2-i]!=c) break;
}
if(i==-1) return true;
return false;
}
int main(){
for(int i=0;i<3;i++){
scanf("%s",str[i]);
int len = strlen(str[i]);
for(int j=0;j<len;j++){
if(str[i][j] == 'X'){
x++;
}
else if(str[i][j]=='0'){
y++;
}
}
}
if(x-y>1||x<y||(check_connect('X')&&check_connect('0'))||(check_connect('X')&&x==y)||(check_connect('0')&&x>y)){
printf("illegal");
}
else if(check_connect('X')&&x-1==y){
printf("the first player won");
}
else if(check_connect('0')&&x==y){
printf("the second player won");
}
else if(x==y&&x!=5){
printf("first");
}
else if(x-y==1&&y!=4){
printf("second");
}
else{
printf("draw");
}
}