Title description
This question is relatively simple. To determine whether the current chessboard status is valid or not, an
invalid chessboard will present at least one of the following five characteristics:
1. O has more points than X
2. O and X win at the same time
3. The points of X are 2 or more more than the points of O
4. X wins, but there are as many Xs as O
5. O wins, but X is one more than O
#include<iostream>
using namespace std;
char map[4][4];
bool win(char c){
for(int i=0;i<3;i++)
{
for(int j=0;j<3&&map[i][j]==c;j++)
if(j==2)
return true;
for(int j=0;j<3&&map[j][i]==c;j++)
if(j==2)
return true;
}
for(int i=0;i<3&&map[i][i]==c;i++)
if(i==2)
return true;
for(int i=0;i<3&&map[i][2-i]==c;i++)
if(i==2)
return true;
return false;
}
int main(){
int flag;
int n;
while(cin>>n){
while(n--){
int xnum=0;
int onum=0;
flag=1;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++){
cin>>map[i][j];
if(map[i][j]=='X')
xnum++;
else if(map[i][j]=='O')
onum++;}
if(win('X')&&win('O'))
flag=0;
if(win('X')&&xnum==onum)
flag=0;
if(win('O')&&xnum!=onum)
flag=0;
if(onum>xnum||xnum-onum>=2)
flag=0;
if(flag)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
}
}
After reading the code of the boss, it is really concise
//poj 2361
#include <iostream>
#include <algorithm>
using namespace std;
int way[8][3]={
{
0,1,2},
{
3,4,5},
{
6,7,8},
{
0,3,6},
{
1,4,7},
{
2,5,8},
{
0,4,8},
{
2,4,6}
};
char s[16];
bool check(char c)
{
for(int i=0;i<8;++i){
int j;
for(j=0;j<3;++j)
if(s[way[i][j]]!=c)
break;
if(j==3)
return true;
}
return false;
}
int main()
{
int x,o,cases;
scanf("%d",&cases);
while(cases--){
scanf("%s%s%s",s,&s[3],&s[6]);
x=count(s,s+9,'X');
o=count(s,s+9,'O');
if(x<o||o+1<x) puts("no");
else if(x>o&&check('X')&&!check('O')) puts("yes");
else if(o==x&&!check('X')&&check('O')) puts("yes");
else if(!check('X')&&!check('O')) puts("yes");
else puts("no");
}
return 0;
}