poj4049 chess金华邀请赛题

Problem F. Chess
Description
There is a special chess game. In the game, two people play with go pieces (“x” 
and “o”) on a 4×4 go board. The “x” piece plays first, and players alternate in placing 
their pieces on an empty cell. The winner is the first player to get an unbroken row of 
four pieces horizontally, vertically, or diagonally, as shown below:
In Figure 1, player with piece ‘x’ win the game.
In Figure 2, player with piece ‘o’ win the game.
In Figure 3, there is a tie.
XXX often plays that game with his girlfriend. Sometimes he wants to win the 
game in order to prove his cleverness. Sometimes he wants to lose the game to let his 
girlfriend happy. Sometimes he wants to end in a tie (which means that no one wins 
when there is no empty  cell on the board), so that the relationship between his 
girlfriend and him can be improved.
Here is the question: given the status of middle stage of the board, judge whether 
XXX can get the result he expected whatever how his girlfriend places her pieces later 
on.
Input
The input begins with a line containing an integer, indicating the number of test 
cases. There are no more than 100 test cases.
For each case, the first line is either “WIN”, “LOSE” or “TIE”, indicating the 
expectation result of XXX. The next four lines give a 4*4 matrix indicating the status 
of middle stage of the board. The matrix is formed with the character “x” (“x” piece), 
“o” (“o” piece) and“.” (empty  cell). There are 6 to 10 pieces on the board. The 
number of “x” pieces is equals to or one more than the number of “o” pieces. It is 
XXX’s turn to place the piece. It is promised that no one has won the game in the 
given board. 
Output
For each test case, output a string “YES” or “NO” in a line to tell XXX if he can 
x o o x
o x o x
x o o o
o x x x
Figure 3
x
x x
o o o o
x
Figure 2
x
x o
o x o
x
Figure 1achieve his expectation.
Sample Input
3
LOSE
.o.x
.o.o
x...
.x..
WIN
.o..
xxoo
x..o
x..x
TIE
...o
xo.x
x.xo
o...
Sample Output
NO
NO
YES

竟然因为开的str数组为str[4][4],害我调试了一个小时,天呐~~~~

#include<iostream>
#include<cstdlib>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
char str[6][6];
char ss[10];
int change(int x,int y)
{
    return (x-1)*4+y;
}
bool istrue(int x,int temp)
{
    return (x&(1<<temp));
}
bool iswin(int x)
{
    bool flag;
    for(int i=1;i<=4;i++)
    {
        flag=true;
        for(int j=1;j<=4;j++)
        {
          int temp=change(i,j);
          if(!istrue(x,temp))
          {flag=false;break;}
        }
        if(flag) return true;
    }
    for(int j=1;j<=4;j++)
    {
        flag=true;
        for(int i=1;i<=4;i++)
        {
            int temp=change(i,j);
            if(!istrue(x,temp)){flag=false;break;}
        }
        if(flag) return true;
    }
    flag=true;
    for(int i=1;i<=4;i++)
    {
        int temp=change(i,i);
        if(!istrue(x,temp)){flag=false;break;}
    }
    if(flag) return true;
    flag=true;
    for(int i=1;i<=4;i++)
    {
        int temp=change(5-i,i);
        if(!istrue(x,temp)) {flag=false;break;}
    }
    if(flag) return true;
    return false;
}

bool isWIN(int step,int x,int y,bool who)
{
    if(iswin(x)) return true;
    if(iswin(y)) return false;
    if(step>=17) return false;
    if(who)
    {
        for(int i=1;i<=16;i++)
        {
            if(!istrue(x,i)&&!istrue(y,i))
            {
                if(isWIN(step+1,x+(1<<i),y,false))
                return true;
            }
        }
        return false;
    }
    else
    {
        for(int i=1;i<=16;i++)
        {
            if(!istrue(x,i)&&!istrue(y,i))
            {
                if(!isWIN(step+1,x,y+(1<<i),true))
                return false;///
            }
        }
        return true;
    }
}
bool islose(int step,int x,int y,bool who)
{
    if(iswin(x)) return false;
    if(iswin(y)) return true;
    if(step>=17) return false;
    if(who)
    {
        for(int i=1;i<=16;i++)
        {
            if(!istrue(x,i)&&!istrue(y,i))
            {
               // x|=(1<<i);
                if(islose(step+1,x+(1<<i),y,false))
                return true;
            }
        }
        return false;
    }
    else
    {
        for(int i=1;i<=16;i++)
        {
            if(!istrue(x,i)&&!istrue(y,i))
            {
                //y|=(1<<i);
                if(!islose(step+1,x,y+(1<<i),true))
                return false;///
            }
        }
        return true;
    }
}
bool istie(int step,int x,int y,bool who)
{
    if(iswin(x)) return false;
    if(iswin(y)) return false;
    if(step>=17) return true;
    if(who)
    {
        for(int i=1;i<=16;i++)
        {
            if(!istrue(x,i)&&!istrue(y,i))
            {
               // x|=(1<<i);
                if(istie(step+1,x+(1<<i),y,false))
                return true;
            }
        }
        return false;
    }
    else
    {
        for(int i=1;i<=16;i++)
        {
            if(!istrue(x,i)&&!istrue(y,i))
            {
               // y|=(1<<i);
                if(!istie(step+1,x,y+(1<<i),true))///
                return false;
            }
        }
        return true;
    }
}
bool solve(int sx,int sy)
{
    int x=0;
    int y=0;
    if(sx==sy)
    {
        for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
        {
            int temp=change(i,j);
            if(str[i][j]=='x')
            x|=1<<temp;
            else if(str[i][j]=='o')
            y|=1<<temp;
        }
    }
    else
    {
        for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
        {
            int temp=change(i,j);
            if(str[i][j]=='o') x|=1<<temp;
            else if(str[i][j]=='x') y|=1<<temp;
        }
    }
    int step = sx+sy +1;
    if(!strcmp(ss,"LOSE"))
    {
        return islose(step,x,y,true);
    }
    else if(!strcmp(ss,"WIN"))
    {
        return isWIN(step,x,y,true);
    }
    else if(!strcmp(ss,"TIE"))
    {
        return istie(step,x,y,true);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cin>>ss;
        int sx=0;
        int sy=0;
        for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
        {
            cin>>str[i][j];
            if(str[i][j]=='x') sx++;
            else if(str[i][j]=='o') sy++;
        }
        if(solve(sx,sy))
        puts("YES");
        else
        puts("NO");
    }
}


 

你可能感兴趣的:(poj4049 chess金华邀请赛题)