Uva220 Othello

 Othello 

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 disks with 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 bracketed if 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.)

tex2html_wrap73tex2html_wrap75

Write a program to read a series of Othello games. 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 specify 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.

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. Commands 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 ( xy) 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 move, 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 between output 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------
--------
--------
--------
--------
这个嘛,,,,很长很暴力。
代码写得我都不愿意看了。。。。
AC代码:
  1 #include<iostream>
  2 #include<algorithm>
  3 #include<iomanip>
  4 
  5 using namespace std;
  6 char Qipan[10][10];
  7 int NumB,NumW;
  8 void Initializer();
  9 void solve(char str[],char);
 10 void SolveQ();
 11 void SolveM(char,int,int);
 12 void SolveL(char);
 13 int CheckPoint(char,int,int);
 14 void ActPoint(char,int,int);
 15 void CheckNum();
 16 int exchange;
 17 int main()
 18 {
 19     int a;
 20     char c;
 21     char str[10];
 22     int flag=0;
 23     cin >> a;
 24     while(a--)
 25     {
 26         Initializer();
 27         cin >> c;
 28         if(flag++)
 29             cout << endl;
 30         while(cin >> str)
 31         {
 32             solve(str,c);
 33             if(exchange)
 34             {
 35                 c='W'+'B'-c;
 36                 exchange=0;
 37             }
 38             if(str[0]=='Q')break;
 39         }
 40     }
 41     return 0;
 42 }
 43 void Initializer()
 44 {
 45     NumB=NumW=0;
 46     exchange=0;
 47     for(int i=0; i<8; i++)
 48         cin >> Qipan[i];
 49 }
 50 void solve(char str[],char c)
 51 {
 52     if(str[0]=='L') SolveL(c);
 53     if(str[0]=='M') SolveM(c,str[1]-'1',str[2]-'1');
 54     if(str[0]=='Q') SolveQ();
 55 }
 56 void SolveL(char c)
 57 {
 58 
 59     int Flag=0;
 60     for(int i=0; i<8; i++)
 61         for(int j=0; j<8; j++)
 62             if(Qipan[i][j]=='-'&&CheckPoint(c,i,j))
 63             {
 64                 if(Flag++)cout << " ";
 65                 cout << "("<<i+1<<","<<j+1<<")";
 66             }
 67     if(!Flag) cout << "No legal move.";
 68     cout << endl;
 69 }
 70 void SolveM(char c,int a,int b)
 71 {
 72     if(CheckPoint(c,a,b))
 73     {
 74         ActPoint(c,a,b);
 75         exchange++;
 76     }
 77     else
 78         ActPoint('W'+'B'-c,a,b);
 79     CheckNum();
 80     cout << "Black -"<<setw(3) <<NumB <<" White -"<< setw(3)<<NumW <<endl;
 81 }
 82 void CheckNum()
 83 {
 84     NumB=NumW=0;
 85     for(int i=0; i<8; i++)
 86         for(int j=0; j<8; j++)
 87         {
 88             if(Qipan[i][j]=='W')NumW++;
 89             if(Qipan[i][j]=='B')NumB++;
 90         }
 91 }
 92 void SolveQ()
 93 {
 94     for(int i=0; i<8; i++)
 95         cout << Qipan[i]<< endl;
 96 }
 97 int CheckPoint(char c,int a,int b)
 98 {
 99     if(a+1<8&&Qipan[a+1][b]=='W'+'B'-c)
100     {
101         int i=2;
102         while(a+i<8&&Qipan[a+i][b]=='W'+'B'-c) i++;
103         if(a+i<8&&Qipan[a+i][b]==c)return 1;
104     }
105     if(a-1>=0&&Qipan[a-1][b]=='W'+'B'-c)
106     {
107         int i=2;
108         while(a-i>=0&&Qipan[a-i][b]=='W'+'B'-c)i++;
109         if(a-i>=0&&Qipan[a-i][b]==c)return 1;
110     }
111     if(b+1<8&&Qipan[a][b+1]=='W'+'B'-c)
112     {
113         int i=2;
114         while(b+i<8&&Qipan[a][b+i]=='W'+'B'-c)i++;
115         if(b+i<8&&Qipan[a][b+i]==c)return 1;
116     }
117     if(b-1>=0&&Qipan[a][b-1]=='W'+'B'-c)
118     {
119         int i=2;
120         while(b-i>=0&&Qipan[a][b-i]=='W'+'B'-c)i++;
121         if(b-i>=0&&Qipan[a][b-i]==c)return 1;
122     }
123     if(a+1<8&&b+1<8&&Qipan[a+1][b+1]=='W'+'B'-c)
124     {
125         int i=2;
126         while(a+i<8&&b+i<8&&Qipan[a+i][b+i]=='W'+'B'-c)i++;
127         if(a+i<8&&b+i<8&&Qipan[a+i][b+i]==c)return 1;
128     }
129     if(a-1>=0&&b+1<8&&Qipan[a-1][b+1]=='W'+'B'-c)
130     {
131         int i=2;
132         while(a-i>=0&&b+i<8&&Qipan[a-i][b+i]=='W'+'B'-c)i++;
133         if(a-i>=0&&b+i<8&&Qipan[a-i][b+i]==c)return 1;
134     }
135     if(a+1<8&&b-1>=0&&Qipan[a+1][b-1]=='W'+'B'-c)
136     {
137         int i=2;
138         while(a+i<8&&b-i>=0&&Qipan[a+i][b-i]=='W'+'B'-c)i++;
139         if(a+i<8&&b-i>=0&&Qipan[a+i][b-i]==c)return 1;
140     }
141     if(a-1>=0&&b-1>=0&&Qipan[a-1][b-1]=='W'+'B'-c)
142     {
143         int i=2;
144         while(a-i>=0&&b-i>=0&&Qipan[a-i][b-i]=='W'+'B'-c)i++;
145         if(a-i>=0&&b-i>=0&&Qipan[a-i][b-i]==c)return 1;
146     }
147     return 0;
148 }
149 void ActPoint(char c,int a,int b)
150 {
151     if(a+1<8&&Qipan[a+1][b]=='W'+'B'-c)
152     {
153         int i=2;
154         while(a+i<8&&Qipan[a+i][b]=='W'+'B'-c)i++;
155         if(a+i<8&&Qipan[a+i][b]==c)
156             for(int j=1; j<i; j++)
157                 Qipan[a+j][b]=c;
158     }
159     if(a-1>=0&&Qipan[a-1][b]=='W'+'B'-c)
160     {
161         int i=2;
162         while(a-i>=0&&Qipan[a-i][b]=='W'+'B'-c)i++;
163         if(a-i>=0&&Qipan[a-i][b]==c)
164             for(int j=1; j<i; j++)
165                 Qipan[a-j][b]=c;
166     }
167     if(b+1<8&&Qipan[a][b+1]=='W'+'B'-c)
168     {
169         int i=2;
170         while(b+i<8&&Qipan[a][b+i]=='W'+'B'-c)i++;
171         if(b+i<8&&Qipan[a][b+i]==c)
172             for(int j=1; j<i; j++)
173                 Qipan[a][b+j]=c;
174     }
175     if(b-1>=0&&Qipan[a][b-1]=='W'+'B'-c)
176     {
177         int i=2;
178         while(b-i>=0&&Qipan[a][b-i]=='W'+'B'-c)i++;
179         if(b-i>=0&&Qipan[a][b-i]==c)
180             for(int j=1; j<i; j++)
181                 Qipan[a][b-j]=c;
182     }
183     if(a+1<8&&b+1<8&&Qipan[a+1][b+1]=='W'+'B'-c)
184     {
185         int i=2;
186         while(a+i<8&&b+i<8&&Qipan[a+i][b+i]=='W'+'B'-c)i++;
187         if(a+i<8&&b+i<8&&Qipan[a+i][b+i]==c)
188             for(int j=1; j<i; j++)
189                 Qipan[a+j][b+j]=c;
190     }
191     if(a-1>=0&&b+1<8&&Qipan[a-1][b+1]=='W'+'B'-c)
192     {
193         int i=2;
194         while(a-i>=0&&b+i<8&&Qipan[a-i][b+i]=='W'+'B'-c)i++;
195         if(a-i>=0&&b+i<8&&Qipan[a-i][b+i]==c)
196             for(int j=1; j<i; j++)
197                 Qipan[a-j][b+j]=c;
198     }
199     if(a+1<8&&b-1>=0&&Qipan[a+1][b-1]=='W'+'B'-c)
200     {
201         int i=2;
202         while(a+i<8&&b-i>=0&&Qipan[a+i][b-i]=='W'+'B'-c)i++;
203         if(a+i<8&&b-i>=0&&Qipan[a+i][b-i]==c)
204             for(int j=1; j<i; j++)
205                 Qipan[a+j][b-j]=c;
206     }
207     if(a-1>=0&&b-1>=0&&Qipan[a-1][b-1]=='W'+'B'-c)
208     {
209         int i=2;
210         while(a-i>=0&&b-i>=0&&Qipan[a-i][b-i]=='W'+'B'-c)i++;
211         if(a-i>=0&&b-i>=0&&Qipan[a-i][b-i]==c)
212             for(int j=1; j<i; j++)
213                 Qipan[a-j][b-j]=c;
214     }
215     Qipan[a][b]=c;
216 }
View Code
原文地址:https://www.cnblogs.com/I-love-HLD/p/4213463.html