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 ...
随机推荐
- iOS触摸事件深度解析-备用
概述 本文主要解析从我们的手指触摸苹果设备到最终响应事件的整个处理机制.本质上讲,整个过程可以分为两个步骤: 步骤1:找目标.在iOS视图层次结构中找到触摸事件的最终接受者: 步骤2:事件响应.基于i ...
- ImageNet && 医学图像的识别
医学图像识别的问题 如果将CNN应用于医学图像,首要面对的问题是训练数据的缺乏.因为CNN的训练数据都需要有类别标号,这通常需要专家来手工标记.要是标记像ImageNet这样大规模的上百万张的训练图像 ...
- 进程外组件通信之免注册com通信【原创】
最近在搞进程外组件通信的东西,写了个demo,免注册的,一直没调通,其实就是两个问题卡了好几天,也没找到有用的资料,试了好几天终于才解决,现简单记录下来,免得大家跟我走一样的弯路.下面com端程序名称 ...
- form 和 ngModel
参考 https://docs.angularjs.org/api/ng/type/ngModel.NgModelController https://docs.angularjs.org/api/n ...
- keil Ax51中条件编译指令IF与$IF的区别
keil A51中条件编译指令IF与$IF的区别:1.IF和$IF是不等价的,不要混淆了;2.带前缀$的条件编译$IF用法:(汇编器指示命令Assembler Directive)只能用来测试由$SE ...
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 414 Solved: ...
- 【转】VC中TRACE
原文网址:http://blog.csdn.net/yangsen2016/article/details/1636996 TRACE宏对于VC下程序调试来说是很有用的东西,有着类似printf的功能 ...
- 2015第23周四HTML特殊字符显示问题
1.项目中常采用EL表达式来输出后台内容,但测试发现它并不能完美处理要输出内容包含有<>或&等HTML特殊字符问题.先直接给出此问题JSTL的解决方案: 1.引入标签:<%@ ...
- Delphi流的操作 转
一.流的概念 流简单说是建立在面向对象基础上的一种抽象的处理数据的工具,它定义了一些处理数据的基本操作,如读取数据,写入数据等,程序员只需掌握对流进行操作,而不用关心流的另一头数据的真正流向.其实,流 ...
- hdu1824-Let's go home:图论2-SAT
关键在于找出一定矛盾的条件,设一队的3个人为(a,b,c),a为队长,那么(a不留下,b不留下)矛盾,(a不留下,c不留下)矛盾; 对于每一对队员,(a留下,b留下)矛盾. 把模型建好,剩下的就是套模 ...