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.)
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 ( 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 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的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 4-3/UVa220 - Othello
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa 220 - Othello #include<iostream ...
- [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 ...
- python 翻转棋(othello)
利用上一篇的框架,再写了个翻转棋的程序,为了调试minimax算法,花了两天的时间. 几点改进说明: 拆分成四个文件:board.py,player.py,ai.py,othello.py.使得整个结 ...
- 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 ...
- UVA 220 Othello
题意:输入n,代表次数,每次输入8*8的棋盘,处理3种命令:①L:打印所有合法操作,②M:放棋子,③Q:打印棋盘然后退出. 思路:①用字符数组存棋盘,整型数组存合法位置. ②查找的方法:当前玩家为cu ...
- 【习题 4-3 UVA - 220】Othello
[链接] 我是链接,点我呀:) [题意] [题解] legal被我打成leagal... 然后注意输出坐标的时候,格式是%2d.. 然后就没啥难的了.. [代码] #include <bits/ ...
- 算法习题---4-3黑白棋(UVa220)
一:题目 系统提示当前旗手W/B(白/黑)下子,例如W下子,那么W下的位置必须是夹住黑色棋子的位置才可以. 夹住方式:横向.竖向.斜向 注意落子后将夹住的黑棋吞噬变为白棋 (一)题目详解 .棋盘以数组 ...
- C++的简单“五子棋”游戏,只是核心代码,资源代码未添加
ChessBoard.h #ifndef __CHESS_BOARD_H__ #define __CHESS_BOARD_H__ #include "DataStruct.h" # ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- [工具]web开发时自动刷新网页:liveReload
传统网页开发流程:用sublime text写好代码,运行,发现问题,再回到sublime text修改,运行…如此往复,十分繁琐.今天看到有人(<LiveReload>讓Sublime ...
- Gradle Android客户端程序打包(基于gradle 2.10版本验证通过)
一.前言 目前正在准备从eclipse开发环境向AndroidStudio迁移,提前过去探探路,不出所料,原来gradle脚本果然报错,无法运行,想想索性把本地的gradle一起升级到最新版本,毕竟1 ...
- C#中使用SendMessage在进程间传递数据的实例
原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...
- bzoj 1196
http://www.lydsy.com/JudgeOnline/problem.php?id=1196 二分+并查集 一共有2*M条路径,我们首先将这2*M条路径按费用排序. 然后二分最大费用的公路 ...
- 黑马程序员_Java面向对象1_封装
3.面向对象_封装 3.1面向对象概念 3.1.1理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程:强调的是功能行为(执行者) 面向对象:将功能封装进对象,强调具备了 ...
- hdu 4869 Turn the pokers(组合数+费马小定理)
Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...
- (转)iPhone 判断UITableView 滚动到底部
UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate. ...
- Live555 实战之框架简单介绍
作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 上一篇文章简要介绍了怎样以共享库的方式交叉编译Live555,今天再来介绍live源代码框架. 先 ...
- DataBindings 与 INotifyPropertyChanged 实现自动刷新 WinForm 界面
--首发于博客园, 转载请保留此链接 博客原文地址 业务逻辑与界面的分离对于维护与迁移是非常重要的,在界面上给某属性赋值,后台要检测到其已经发生变化 问题: 输入某物品 单价 Price, 数量Am ...
- shell常用命令的用法
1. 如何把 /etc/passwd 中用户uid 大于500 的行给打印出来?awk -F ':' '$3>500' /etc/passwd 2. awk中 NR,NF两个变量表示什么含义?N ...