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.)

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代码:
 #include<iostream>
#include<algorithm>
#include<iomanip> using namespace std;
char Qipan[][];
int NumB,NumW;
void Initializer();
void solve(char str[],char);
void SolveQ();
void SolveM(char,int,int);
void SolveL(char);
int CheckPoint(char,int,int);
void ActPoint(char,int,int);
void CheckNum();
int exchange;
int main()
{
int a;
char c;
char str[];
int flag=;
cin >> a;
while(a--)
{
Initializer();
cin >> c;
if(flag++)
cout << endl;
while(cin >> str)
{
solve(str,c);
if(exchange)
{
c='W'+'B'-c;
exchange=;
}
if(str[]=='Q')break;
}
}
return ;
}
void Initializer()
{
NumB=NumW=;
exchange=;
for(int i=; i<; i++)
cin >> Qipan[i];
}
void solve(char str[],char c)
{
if(str[]=='L') SolveL(c);
if(str[]=='M') SolveM(c,str[]-'',str[]-'');
if(str[]=='Q') SolveQ();
}
void SolveL(char c)
{ int Flag=;
for(int i=; i<; i++)
for(int j=; j<; j++)
if(Qipan[i][j]=='-'&&CheckPoint(c,i,j))
{
if(Flag++)cout << " ";
cout << "("<<i+<<","<<j+<<")";
}
if(!Flag) cout << "No legal move.";
cout << endl;
}
void SolveM(char c,int a,int b)
{
if(CheckPoint(c,a,b))
{
ActPoint(c,a,b);
exchange++;
}
else
ActPoint('W'+'B'-c,a,b);
CheckNum();
cout << "Black -"<<setw() <<NumB <<" White -"<< setw()<<NumW <<endl;
}
void CheckNum()
{
NumB=NumW=;
for(int i=; i<; i++)
for(int j=; j<; j++)
{
if(Qipan[i][j]=='W')NumW++;
if(Qipan[i][j]=='B')NumB++;
}
}
void SolveQ()
{
for(int i=; i<; i++)
cout << Qipan[i]<< endl;
}
int CheckPoint(char c,int a,int b)
{
if(a+<&&Qipan[a+][b]=='W'+'B'-c)
{
int i=;
while(a+i<&&Qipan[a+i][b]=='W'+'B'-c) i++;
if(a+i<&&Qipan[a+i][b]==c)return ;
}
if(a->=&&Qipan[a-][b]=='W'+'B'-c)
{
int i=;
while(a-i>=&&Qipan[a-i][b]=='W'+'B'-c)i++;
if(a-i>=&&Qipan[a-i][b]==c)return ;
}
if(b+<&&Qipan[a][b+]=='W'+'B'-c)
{
int i=;
while(b+i<&&Qipan[a][b+i]=='W'+'B'-c)i++;
if(b+i<&&Qipan[a][b+i]==c)return ;
}
if(b->=&&Qipan[a][b-]=='W'+'B'-c)
{
int i=;
while(b-i>=&&Qipan[a][b-i]=='W'+'B'-c)i++;
if(b-i>=&&Qipan[a][b-i]==c)return ;
}
if(a+<&&b+<&&Qipan[a+][b+]=='W'+'B'-c)
{
int i=;
while(a+i<&&b+i<&&Qipan[a+i][b+i]=='W'+'B'-c)i++;
if(a+i<&&b+i<&&Qipan[a+i][b+i]==c)return ;
}
if(a->=&&b+<&&Qipan[a-][b+]=='W'+'B'-c)
{
int i=;
while(a-i>=&&b+i<&&Qipan[a-i][b+i]=='W'+'B'-c)i++;
if(a-i>=&&b+i<&&Qipan[a-i][b+i]==c)return ;
}
if(a+<&&b->=&&Qipan[a+][b-]=='W'+'B'-c)
{
int i=;
while(a+i<&&b-i>=&&Qipan[a+i][b-i]=='W'+'B'-c)i++;
if(a+i<&&b-i>=&&Qipan[a+i][b-i]==c)return ;
}
if(a->=&&b->=&&Qipan[a-][b-]=='W'+'B'-c)
{
int i=;
while(a-i>=&&b-i>=&&Qipan[a-i][b-i]=='W'+'B'-c)i++;
if(a-i>=&&b-i>=&&Qipan[a-i][b-i]==c)return ;
}
return ;
}
void ActPoint(char c,int a,int b)
{
if(a+<&&Qipan[a+][b]=='W'+'B'-c)
{
int i=;
while(a+i<&&Qipan[a+i][b]=='W'+'B'-c)i++;
if(a+i<&&Qipan[a+i][b]==c)
for(int j=; j<i; j++)
Qipan[a+j][b]=c;
}
if(a->=&&Qipan[a-][b]=='W'+'B'-c)
{
int i=;
while(a-i>=&&Qipan[a-i][b]=='W'+'B'-c)i++;
if(a-i>=&&Qipan[a-i][b]==c)
for(int j=; j<i; j++)
Qipan[a-j][b]=c;
}
if(b+<&&Qipan[a][b+]=='W'+'B'-c)
{
int i=;
while(b+i<&&Qipan[a][b+i]=='W'+'B'-c)i++;
if(b+i<&&Qipan[a][b+i]==c)
for(int j=; j<i; j++)
Qipan[a][b+j]=c;
}
if(b->=&&Qipan[a][b-]=='W'+'B'-c)
{
int i=;
while(b-i>=&&Qipan[a][b-i]=='W'+'B'-c)i++;
if(b-i>=&&Qipan[a][b-i]==c)
for(int j=; j<i; j++)
Qipan[a][b-j]=c;
}
if(a+<&&b+<&&Qipan[a+][b+]=='W'+'B'-c)
{
int i=;
while(a+i<&&b+i<&&Qipan[a+i][b+i]=='W'+'B'-c)i++;
if(a+i<&&b+i<&&Qipan[a+i][b+i]==c)
for(int j=; j<i; j++)
Qipan[a+j][b+j]=c;
}
if(a->=&&b+<&&Qipan[a-][b+]=='W'+'B'-c)
{
int i=;
while(a-i>=&&b+i<&&Qipan[a-i][b+i]=='W'+'B'-c)i++;
if(a-i>=&&b+i<&&Qipan[a-i][b+i]==c)
for(int j=; j<i; j++)
Qipan[a-j][b+j]=c;
}
if(a+<&&b->=&&Qipan[a+][b-]=='W'+'B'-c)
{
int i=;
while(a+i<&&b-i>=&&Qipan[a+i][b-i]=='W'+'B'-c)i++;
if(a+i<&&b-i>=&&Qipan[a+i][b-i]==c)
for(int j=; j<i; j++)
Qipan[a+j][b-j]=c;
}
if(a->=&&b->=&&Qipan[a-][b-]=='W'+'B'-c)
{
int i=;
while(a-i>=&&b-i>=&&Qipan[a-i][b-i]=='W'+'B'-c)i++;
if(a-i>=&&b-i>=&&Qipan[a-i][b-i]==c)
for(int j=; j<i; j++)
Qipan[a-j][b-j]=c;
}
Qipan[a][b]=c;
}

Uva220 Othello的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 4-3/UVa220 - Othello

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa 220 - Othello #include<iostream ...

  2. [CareerCup] 8.8 Othello Game 黑白棋游戏

    8.8 Othello is played as follows: Each Othello piece is white on one side and black on the other. Wh ...

  3. python 翻转棋(othello)

    利用上一篇的框架,再写了个翻转棋的程序,为了调试minimax算法,花了两天的时间. 几点改进说明: 拆分成四个文件:board.py,player.py,ai.py,othello.py.使得整个结 ...

  4. BFS:UVa220 ACM/ICPC 1992-Othello(黑白棋)

    Othello Othello is a game played by two people on an 8 x 8 board, using disks that are white on one ...

  5. UVA 220 Othello

    题意:输入n,代表次数,每次输入8*8的棋盘,处理3种命令:①L:打印所有合法操作,②M:放棋子,③Q:打印棋盘然后退出. 思路:①用字符数组存棋盘,整型数组存合法位置. ②查找的方法:当前玩家为cu ...

  6. 【习题 4-3 UVA - 220】Othello

    [链接] 我是链接,点我呀:) [题意] [题解] legal被我打成leagal... 然后注意输出坐标的时候,格式是%2d.. 然后就没啥难的了.. [代码] #include <bits/ ...

  7. 算法习题---4-3黑白棋(UVa220)

    一:题目 系统提示当前旗手W/B(白/黑)下子,例如W下子,那么W下的位置必须是夹住黑色棋子的位置才可以. 夹住方式:横向.竖向.斜向 注意落子后将夹住的黑棋吞噬变为白棋 (一)题目详解 .棋盘以数组 ...

  8. C++的简单“五子棋”游戏,只是核心代码,资源代码未添加

    ChessBoard.h #ifndef __CHESS_BOARD_H__ #define __CHESS_BOARD_H__ #include "DataStruct.h" # ...

  9. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. 一直想测试的NGINX变量输出,最于有办法了。

    参考URL: http://blog.ailms.me/2013/08/04/nginx-server_name-and-host-difference.html 要是可能在正则测试及REWRITE就 ...

  2. HDU 4116 Fruit Ninja

    http://acm.hdu.edu.cn/showproblem.php?pid=4116 题意:给N个圆,求一条直线最多能经过几个圆?(相切也算) 思路:枚举中心圆,将其他圆的切线按照极角排序,并 ...

  3. 绝美Sysinternals

    啥也不说了,自己看吧: https://technet.microsoft.com/en-us/sysinternals/bb545046 新地址: https://technet.microsoft ...

  4. Qt编程之在QGraphics scene中使用图片

    http://stackoverflow.com/questions/5960074/qimage-in-a-qgraphics-scene http://stackoverflow.com/ques ...

  5. bzoj4033

    http://www.lydsy.com/JudgeOnline/problem.php?id=4033 树形DP. 我们发现,每条边都是一条桥,若我们知道这条边其中一侧有多少个黑点,我们就可以知道这 ...

  6. bzoj1624 [Usaco2008 Open] Clear And Present Danger 寻宝之路

    Description     农夫约翰正驾驶一条小艇在牛勒比海上航行.     海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛.一 张藏宝图上说,如果他的路程上 ...

  7. Polymorphism & Overloading & Overriding

    In Java, a method signature is the method name and the number and type of its parameters. Return typ ...

  8. 面试题32.从1到n整数中1出现的次数

    题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从 1到12这些整数中包含1的数字中1,10,11和12,1一共出现了5次 本题可以直接变量1到n的n个数然后分别计 ...

  9. nyoj201 作业题

    作业题 时间限制: 3000 ms  |  内存限制: 65535 KB 难度: 3   描述 小白同学这学期有一门课程叫做<数值计算方法>,这是一门有效使用数字计算机求数学问题近似解的方 ...

  10. js中window.print()去除页眉页脚

    //jsp打印时去除页眉页页脚 打印前加入下面代码即可 var HKEY_Root,HKEY_Path,HKEY_Key; HKEY_Root="HKEY_CURRENT_USER" ...