UVa220 ACM/ICPC 1992黑白棋

Othello is a game played by two people on an 8 x 8 board, using disks that are white on one side andblack on the other. One player places disks with the white side up and the other player places diskswith the black side up. The players alternate placing one disk on an unoccupied space on the board.In placing a disk, the player must bracket at least one of the other color disks. Disks are bracketedif they are in a straight line horizontally, vertically, or diagonally, with a disk of the current player’scolor at each end of the line. When a move is made, all the disks that were bracketed are changed tothe color of the player making the move. (It is possible that disks will be bracketed across more thanone line in a single move.)



Input

The first line of the input is the number of games to be processed. Each game consists of a boardconfiguration followed by a list of commands. The board configuration consists of 9 lines. The first 8specify the current state of the board. Each of these 8 lines contains 8 characters, and each of thesecharacters will be one of the following:

‘-’ indicating an unoccupied square

‘B’ indicating a square occupied by a black disk

‘W’ indicating a square occupied by a white disk

The ninth line is either a ‘B’ or a ‘W’ to indicate which is the current player. You may assume thatthe data is legally formatted.

Then a set of commands follows. The commands are to list all possible moves for the current player,make a move, or quit the current game. There is one command per line with no blanks in the input.

Output

The commands and the corresponding outputs are formatted as follows:

List all possible moves for the current player. The command is an ‘L’ in the first column of theline. The program should go through the board and print all legal moves for the current playerin the format (x, y) where x represents the row of the legal move and y represents its column.These moves should be printed in row major order which means:

1) all legal moves in row number i will be printed before any legal move in row number j if jis greater than iand 

2) if there is more than one legal move in row number i, the moves will be printed in ascendingorder based on column number.

All legal moves should be put on one line. If there is no legal move because it is impossible for thecurrent player to bracket any pieces, the program should print the message ‘No legal move.’

Make a move. The command is an ‘M’ in the first column of the line, followed by 2 digits in thesecond and third column of the line. The digits are the row and the column of the space to placethe piece of the current player’s color, unless the current player has no legal move. If the currentplayer has no legal move, the current player is first changed to the other player and the movewill be the move of the new current player. You may assume that the move is then legal. Youshould record the changes to the board, including adding the new piece and changing the colorof all bracketed pieces. At the end of the move, print the number of pieces of each color on theboard in the format ‘Black - xx White - yy’ where xx is the number of black pieces on theboard and yy is the number of white pieces on the board. After a move, the current player willbe changed to the player that did not move.

Quit the current game. The command will be a ‘Q’ in the first column of the line. At this point,print the final board configuration using the same format as was used in the input. This terminatesinput for the current game.

You may assume that the commands will be syntactically correct. Put one blank line betweenoutput from separate games and no blank lines anywhere else in the output.



Sample Input

2

--------

--------

--------

---WB---

---BW---

--------

--------

--------

W

L

M35

L

Q

WWWWB---

WWWB----

WWB-----

WB------

--------

--------

--------

--------

B

L

M25

L

Q



Sample Output

(3,5) (4,6) (5,3) (6,4)

Black - 1 White - 4

(3,4) (3,6) (5,6)

--------

--------

----W---

---WW---

---BW---

--------

--------

--------


No legal move.

Black - 3 White - 12

(3,5)

WWWWB---

WWWWW---

WWB-----

WB------

--------

--------

--------

--------



#include<stdio.h>
#include<string.h>
char chess[8][10];//棋盘 
int weizi[100][2];//存放可以下棋的位子 
int L(char t)
{
    int i,j,v,x,q,s=0;
    char tt;
    s=0;
    if(t=='W')//指出和现在的棋相反的棋 
        tt='B';
    else
        tt='W';
    for(i=0;i<8;i++)
        for(j=0;j<8;j++)
    {
        if(chess[i][j]!='-')//是空位置的时候跳过 
            continue;
        if(i-1>=0&&chess[i-1][j]==tt)//如果是向上的有不同 
        {
            x=0;
            for(v=i-1;v>=0;v--)
            {
                if(chess[v][j]==t)//另一端存在相同的棋 
                {
                    x=1;
                    break;
                }
                else if(chess[v][j]=='-')
                    break;
            }
            if(x==1)//可行记录位子 
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(i+1<8&&chess[i+1][j]==tt)//如果是向上的有不同 
        {
            x=0;
            for(v=i+1;v<8;v++)
            {
                if(chess[v][j]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[v][j]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(j-1>=0&&chess[i][j-1]==tt)//如果是向左的有不同 
        {
            x=0;
            for(v=j-1;v>=0;v--)
            {
                if(chess[i][v]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[i][v]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(j+1<8&&chess[i][j+1]==tt)//如果是向右的有不同
        {
            x=0;
            for(v=j+1;v<8;v++)
            {
                if(chess[i][v]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[i][v]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(i-1>=0&&j-1>=0&&chess[i-1][j-1]==tt)//如果是向左上的有不同
        {
            x=0;
            for(v=i-1,q=j-1;v>=0&&q>=0;v--,q--)
            {
                if(chess[v][q]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[v][q]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(i+1<8&&j-1>=0&&chess[i+1][j-1]==tt)//如果是向左下的有不同
        {
            x=0;
            for(v=i+1,q=j-1;v<8&&q>=0;v++,q--)
            {
                if(chess[v][q]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[v][q]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(i-1>=0&&j+1<8&&chess[i-1][j+1]==tt)//如果是向右上的有不同
        {
            x=0;
            for(v=i-1,q=j+1;v>=0&&q<8;v--,q++)
            {
                if(chess[v][q]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[v][q]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
        if(i+1<8&&j+1<8&&chess[i+1][j+1]==tt)//如果是向右下的有不同
        {
            x=0;
            for(v=i+1,q=j+1;v<8&&q<8;v++,q++)
            {
                if(chess[v][q]==t)
                {
                    x=1;
                    break;
                }
                else if(chess[v][q]=='-')
                    break;
            }
            if(x==1)
            {
                weizi[s][0]=i+1;
                weizi[s][1]=j+1;
                s++;
                continue;
            }
        }
    }
    return s;//返回可行位置的个数 
}
void H(int s)
{
    int i;
    if(s==0)
            printf("No legal move.\n");//没有可行的点 
    else
        for(i=0;i<s;i++)
        {
            if(i==s-1)//最后一个要换行 
                printf("(%d,%d)\n",weizi[i][0],weizi[i][1]);
            else
                printf("(%d,%d) ",weizi[i][0],weizi[i][1]);
        }
}
void M(char a[10],char t)
{
    int i,j,sw=0,sb=0,x,y,z;
    char tt;
    if(t=='W')
        tt='B';
    else
        tt='W';
    x=a[1]-'1';
    y=a[2]-'1';
    chess[x][y]=t;//先将所在的位子填好 
    if(x-1>=0&&chess[x-1][y]==tt)//如果是向上的有不同 
    {
        z=0;
        for(i=x-1;i>=0;i--)
        {
            if(chess[i][y]==t)
            {
                z=1;
                break;
            }
            else if(chess[i][y]=='-')
                break;
        }
        if(z==1)//如果另一端有子,那把其中的棋都改变 
        {
            for(i=x-1;;i--)
            {
                if(chess[i][y]==t)
                    break;
                else
                    chess[i][y]=t;
            }
        }
    }
    if(x+1<8&&chess[x+1][y]==tt)//如果是向下的有不同
    {
        z=0;
        for(i=x+1;i<8;i++)
        {
            if(chess[i][y]==t)
            {
                z=1;
                break;
            }
            else if(chess[i][y]=='-')
                break;
        }
        if(z==1)
        {
            for(i=x+1;;i++)
            {
                if(chess[i][y]==t)
                    break;
                else
                    chess[i][y]=t;
            }
        }
    }
    if(y+1<8&&chess[x][y+1]==tt)//如果是向右的有不同
    {
        z=0;
        for(i=y+1;i<8;i++)
        {
            if(chess[x][i]==t)
            {
                z=1;
                break;
            }
            else if(chess[x][i]=='-')
                break;
        }
        if(z==1)
        {
            for(i=y+1;;i++)
            {
                if(chess[x][i]==t)
                    break;
                else
                    chess[x][i]=t;
            }
        }
    }
    if(y-1>=0&&chess[x][y-1]==tt)//如果是向左的有不同
    {
        z=0;
        for(i=y-1;i>=0;i--)
        {
            if(chess[x][i]==t)
            {
                z=1;
                break;
            }
            else if(chess[x][i]=='-')
                break;
        }
        if(z==1)
        {
            for(i=y-1;;i--)
            {
                if(chess[x][i]==t)
                    break;
                else
                    chess[x][i]=t;
            }
        }
    }
    if(y-1>=0&&x-1>=0&&chess[x-1][y-1]==tt)//如果是向左上的有不同
    {
        z=0;
        for(i=x-1,j=y-1;i>=0,j>=0;i--,j--)
        {
            if(chess[i][j]==t)
            {
                z=1;
                break;
            }
            else if(chess[i][j]=='-')
                break;
        }
        if(z==1)
        {
            for(i=x-1,j=y-1;;i--,j--)
            {
                if(chess[i][j]==t)
                    break;
                else
                    chess[i][j]=t;
            }
        }
    }
    if(y+1<8&&x-1>=0&&chess[x-1][y+1]==tt)//如果是向右上的有不同
    {
        z=0;
        for(i=x-1,j=y+1;i>=0,j<8;i--,j++)
        {
            if(chess[i][j]==t)
            {
                z=1;
                break;
            }
            else if(chess[i][j]=='-')
                break;
        }
        if(z==1)
        {
            for(i=x-1,j=y+1;;i--,j++)
            {
                if(chess[i][j]==t)
                    break;
                else
                    chess[i][j]=t;
            }
        }
    }
    if(y-1>=0&&x+1<8&&chess[x+1][y-1]==tt)//如果是向左下的有不同
    {
        z=0;
        for(i=x+1,j=y-1;i<8,j>=0;i++,j--)
        {
            if(chess[i][j]==t)
            {
                z=1;
                break;
            }
            else if(chess[i][j]=='-')
                break;
        }
        if(z==1)
        {
            for(i=x+1,j=y-1;;i++,j--)
            {
                if(chess[i][j]==t)
                    break;
                else
                    chess[i][j]=t;
            }
        }
    }
    if(y+1<8&&x+1<8&&chess[x+1][y+1]==tt)//如果是向右下的有不同
    {
        z=0;
        for(i=x+1,j=y+1;i<8,j<8;i++,j++)
        {
            if(chess[i][j]==t)
            {
                z=1;
                break;
            }
            else if(chess[i][j]=='-')
                break;
        }
        if(z==1)
        {
            for(i=x+1,j=y+1;;i++,j++)
            {
                if(chess[i][j]==t)
                    break;
                else
                    chess[i][j]=t;
            }
        }
    }
    for(i=0;i<8;i++)
        for(j=0;j<8;j++)
    {
        if(chess[i][j]=='W')//白棋的数量 
            sw++;
        else if(chess[i][j]=='B')//黑棋的数量 
            sb++;
    }
    printf("Black -%3d White -%3d\n",sb,sw);
}
void Q()
{
    int i,j;
    for(i=0;i<8;i++)//打印棋盘 
        printf("%s\n",chess[i]);
}
int main(void)
{
    int N,i,j,first=1,s;
    char t,a[10];
    scanf("%d",&N);
    while(N--)
    {
    	if(first)//为了每两个数据之间换一下行 
    		first=0;
   		else
   			printf("\n");
        for(i=0;i<8;i++)
            scanf("%s",chess[i]);
        scanf(" %c",&t);
        while(1)
        {
            scanf("%s",a);
            if(a[0]=='L')
             {
                 s=L(t);
                 H(s);
             }
            else if(a[0]=='M')
            {
                s=L(t);
                if(s==0)//如果我方无棋可走,那么换对方下 
                {
                    if(t=='B')
                    M(a,'W');
                    else
                    M(a,'B');
                }
                else
                {
                    M(a,t);
                    if(t=='B')
                        t='W';
                    else
                        t='B';
                }
            }
            else
            {
                Q();
                break;
            }
        }
    }
    return 0;
}
//本题肯定有更好的写法,这题就连我都觉得写的繁琐了,主要就是对8个方向的判断,还好当时写的时候思路很清晰,所以用了一个上午 





  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值