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. 参数解析argparse模块

    argparse,python的一个命令行解析模块 import argparse #创建一个命令行解析器 parser = argparse.ArgumentParser() #增添参数 parse ...

  2. F#(1)

    如果你也会C#,那不妨了解下F#(1):F# 数据类型   简单介绍 F#(与C#一样,念作“F Sharp”)是一种基于.Net框架的强类型.静态类型的函数式编程语言.可以说C#是一门包含函数式编程 ...

  3. SQLServer 安装以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法

    http://wenku.baidu.com/view/6732fe09844769eae009ede2.html SQL Server 安装以前的某个程序安装已在安装计算机上创建挂起的文件操作 安装 ...

  4. bzoj3174 [Tjoi2013]拯救小矮人

    Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人, ...

  5. requirejs-define jquery 快速初学实例(一)

    原文地址:http://6yang.net/articles_view.php?id=1103 2011-10-18 13:12:01 by [6yang], 1029 visits, 收藏 | 返回 ...

  6. 【剑指offer】面试题44:扑克牌的顺子

    题目: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定 ...

  7. socket pro

    /etc/exports/tmp目录共享为任何人可以共享并可以进行读写操作 /tmp *(rw,no_root_squash) /home/test 192.168.1.*(rw) *(ro) /et ...

  8. linux shell在while中用read从键盘输入

    系统是ubuntu 14.04 64bit,之前曾想安装Stream来玩dota2,但最终没成功.由于Stream只有32bit,安装Stream时也安装了大量32bit的库.删除Stream后,这些 ...

  9. 《学习opencv》笔记——矩阵和图像操作——cvCalcCovarMatrix,cvCmp and cvCmpS

    矩阵和图像的操作 (1)cvCalcCovarMatrix函数 其结构 void cvCalcCovarMatrix(计算给定点的均值和协方差矩阵 const CvArr** vects,//给定向量 ...

  10. JavaScript 公有 私有 静态属性和方法

    1.公有属性和公有方法 这里的 name  age  都是参数传递进去 可以在外面直接实例化调用. 2.私有属性和方法 私有的只能在函数内部使用 作用域的原因 3.静态属性和静态方法 这里我首先 创建 ...