POJ-2361 井字棋合法判断
难点在于如何想到不合法的条件和判断输赢的算法
不知道为什么用范围for就莫名其妙的避免了那些个多出来的“\n”占用位置,有待解答。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool champion(char c[3][3],int a,int b);
int main()
{
int n;
for(scanf("%d" , &n);
n; n--){
char s[3][3];
for(auto &row1 : s)
for(auto &col1 : row1)
cin>>col1;
int cntx = 0,cnto = 0;
for(const auto &row2 : s){
for(auto col2 : row2){
if(col2 == 'O')
cnto++;
else if(col2 == 'X')
cntx++;
}
}
if(cnto > cntx || cntx - cnto > 1){
cout<<"no";
continue;
}
else{
if(champion(s,cntx,cnto))
cout<<"yes";
else
cout<<"no";
}
}
return 0;
}
bool champion(char c[3][3],int a,int b){
bool flag1 = false,flag2 = false;
for(int j = 0;j != 3;j++){
if(c[j][0] != '.'&&c[j][0] == c[j][1] &&c[j][1] == c[j][2]){
if(c[j][0] == 'X'){
if(a == b)
return false;
else
flag1 = true;
}
else if(c[j][0] == 'O'){
if(a != b)
return false;
else
flag2 = true;
}
}
}
for(int i = 0;i != 3;i++ ){
if(c[0][i] != '.' && c[0][i] == c[1][i] == c[2][i]){
if(c[0][i] == 'X'){
if(a == b)
return false;
else
flag1 = true;
}
else if(c[0][i] == 'O'){
if(a != b)
return false;
else
flag2 = true;
}
}
}
if(c[0][0] != '.' && c[1][1] == c[2][2] == c[0][0]){
if(c[0][0] == 'X'){
if(a != b)
flag1 = true;
else
return false;
}
else if(c[0][0] == 'O'){
if(a == b)
flag2 = true;
else
return false;
}
}
if(c[0][2] != '.' && c[1][1] == c[0][2] == c[2][0]){
if(c[0][2] == 'X'){
if(a != b)
flag1 = true;
else
return false;
}
else if(c[0][2] == 'O'){
if(a == b)
flag2 = true;
else
return false;
}
}
if(flag1 == flag2 && flag1)
return false;
else
return true;
}