UVA

来源:互联网 发布:91vpn代理软件 编辑:程序博客网 时间:2024/05/27 00:54

题目:


Othello is a game played by two people on an 8 x 8 board, using disks that are white on one side and black 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’s color at each end of the line. When a move is made, all the disks that were bracketed are changed to the color of the player making the move. (It is possible that disks will be bracketed across more than one 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 board configuration followed by a list of commands. The board configuration consists of 9 lines. The first 8 pecify the current state of the board. Each of these 8 lines contains 8 characters, and each of these characters 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 that the 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 the line. The program should go through the board and print all legal moves for the current player in 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 j is greater than i and 2) if there is more than one legal move in row number i, the moves will be printed in ascending order based on column number.
All legal moves should be put on one line. If there is no legal move because it is impossible for the current 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 the second and third column of the line. The digits are the row and the column of the space to place the piece of the current player’s color, unless the current player has no legal move. If the current player has no legal move, the current player is first changed to the other player and the move will be the move of the new current player. You may assume that the move is then legal. You should record the changes to the board, including adding the new piece and changing the color of all bracketed pieces. At the end of the move, print the number of pieces of each color on the board in the format ‘Black - xx White - yy’ where xx is the number of black pieces on the board and yy is the number of white pieces on the board. After a more, the current player will be 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 terminates input 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------
--------
--------
--------

--------




题意:


模拟黑白棋走法,规则与黑白棋一致。

第一行W和B表示下的人的棋子

L输出可以走的点,没有可以走输出No legal move.

Q是退出输出改变后的棋盘

M之后输入下的人要下的子(如果之前L没有可以下的则下的人变更),如果下错,继续让下的人下


分析:


直接模拟,注意输出格式。

方向是8个方向,最后才反应过来不是4个。

如果wa一次的话,还是重打吧,调试会出人命的(除了PE一次过真幸运,话说那个竟然是 %2d,为什么复制原来输出时只有一个空格)。

代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;char a[10][10];int d[64][2];int sum;char changeplayer(char c)//交换用户 {if(c=='B')return 'W';return 'B';}//搜索x,y点往各个方向是否存在可以改换棋子的 int upright(int x,int y,char c)//右上 {int i,d=0;for(i=1;x-i>=1&&y+i<=8;i++){if(a[x-i][y+i]==c){if(d)return i;else return 0;}else if(a[x-i][y+i]=='-')return 0;d++;}return 0;}int upleft(int x,int y,char c)//左上 {int i,d=0;for(i=1;x-i>=1&&y-i>=1;i++){if(a[x-i][y-i]==c){if(d)return i;else return 0;}else if(a[x-i][y-i]=='-')return 0;d++;}return 0;}int downleft(int x,int y,char c)//左下 {int i,d=0;for(i=1;x+i<=8&&y-i>=1;i++){if(a[x+i][y-i]==c){if(d)return i;else return 0;}else if(a[x+i][y-i]=='-')return 0;d++;}return 0;}int downright(int x,int y,char c)//右下 {int i,d=0;for(i=1;x+i<=8&&y+i<=8;i++){if(a[x+i][y+i]==c){if(d)return i;else return 0;}else if(a[x+i][y+i]=='-')return 0;d++;}return 0;}int up(int x,int y,char c)//上 {int i,d=0;for(i=x-1;i>=1;i--){if(a[i][y]==c){if(d)return i;else return 0;}else if(a[i][y]=='-')return 0;d++;}return 0;}int down(int x,int y,char c)//下 {int i,d=0;for(i=x+1;i<=8;i++){if(a[i][y]==c){if(d)return i;else return 0;}else if(a[i][y]=='-')return 0;d++;}return 0;}int left(int x,int y,char c)//左 {int i,d=0;for(i=y-1;i>=1;i--){if(a[x][i]==c){if(d)return i;else return 0;}else if(a[x][i]=='-')return 0;d++;}return 0;}int right(int x,int y,char c)//右 {int i,d=0;for(i=y+1;i<=8;i++){if(a[x][i]==c){if(d)return i;else return 0;}else if(a[x][i]=='-')return 0;d++;}return 0;}void change(int x,int y,char c)//改变棋子 {int up1,down1,left1,right1,upleft1,upright1,downleft1,downright1,i;up1=up(x,y,c);if(up1){for(i=x;i>=up1;i--){a[i][y]=c;}}down1=down(x,y,c);if(down1){for(i=x;i<=down1;i++){a[i][y]=c;}}left1=left(x,y,c);if(left1){for(i=y;i>=left1;i--){a[x][i]=c;}}right1=right(x,y,c);if(right1){for(i=y;i<=right1;i++){a[x][i]=c;}}upleft1=upleft(x,y,c);if(upleft1){for(i=0;i<=upleft1;i++){a[x-i][y-i]=c;}}upright1=upright(x,y,c);if(upright1){for(i=0;i<=upright1;i++){a[x-i][y+i]=c;}}downleft1=downleft(x,y,c);if(downleft1){for(i=0;i<=downleft1;i++){a[x+i][y-i]=c;}}downright1=downright(x,y,c);if(downright1){for(i=0;i<=downright1;i++){a[x+i][y+i]=c;}}}void out()//输出可以下的棋子的位置 {for(int i=0;i<sum;i++){if(i)printf(" ");printf("(%d,%d)",d[i][0],d[i][1]);}puts("");}void out2()//输出每人的棋子个数 {int i,j,s1,s2;s1=s2=0;for(i=1;i<=8;i++){for(j=1;j<=8;j++){if(a[i][j]=='B')s1++;else if(a[i][j]=='W')s2++;}}printf("Black - %2d White - %2d\n",s1,s2);}void find(char x)//寻找是否可下 {int i,j;for(i=1;i<=8;i++){for(j=1;j<=8;j++){if(a[i][j]=='-'){if(left(i,j,x)||right(i,j,x)||up(i,j,x)||down(i,j,x)||upright(i,j,x)||upleft(i,j,x)||downright(i,j,x)||downleft(i,j,x)){d[sum][0]=i;d[sum][1]=j;sum++;}}}}}int main(){char k[5];int t,i,j,ll=0;char player;scanf("%d",&t);while(t--){if(ll)puts("");ll=1;for(i=1;i<=8;i++){scanf("%s",a[i]+1);}scanf("%s",k);sum=0;player=k[0];find(player);//寻找可下点 while(scanf("%s",k)){if(k[0]=='Q'){for(i=1;i<=8;i++){for(j=1;j<=8;j++){printf("%c",a[i][j]);}puts("");}break;}//sum==0,没有可以下的位置 if(k[0]=='L'){if(sum==0)cout<<"No legal move."<<endl;else out();}else{if(sum==0){player=changeplayer(player);sum=0;find(player);}int flag=0;//寻找是否下的位置可下 for(i=0;i<sum;i++){if(d[i][0]==k[1]-'0'&&d[i][1]==k[2]-'0'){flag=1;break;}}if(flag){change(k[1]-'0',k[2]-'0',player);player=changeplayer(player);out2();sum=0;find(player);}}}}}


0 0
原创粉丝点击