Codeforces Beta Round #3

来源:互联网 发布:矩阵力学的主要创立者 编辑:程序博客网 时间:2024/06/05 18:28


A - Tic-tac-toe
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 3C
Appoint description: 

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: firstsecondillegalthe first player wonthe 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();}


0 0
原创粉丝点击