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 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.)
Input
The first line of the input is the number of games to be processed. Each game consists of a boardconfiguration followed by a list of commands. The board configuration consists of 9 lines. The first 8specify the current state of the board. Each of these 8 lines contains 8 characters, and each of thesecharacters 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 thatthe data is legally formatted.
Then a set of commands follows. 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.
Output
The commands and the corresponding outputs are formatted as follows:
List all possible moves for the current player. The command is an ‘L’ in the first column of theline. The program should Go through the board and print all legal moves for the current playerin 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 jis greater than iand
2) if there is more than one legal move in row number i, the moves will be printed in ascendingorder based on column number.
All legal moves should be put on one line. If there is no legal move because it is impossible for thecurrent 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 thesecond and third column of the line. The digits are the row and the column of the space to placethe piece of the current player’s color, unless the current player has no legal move. If the currentplayer has no legal move, the current player is first changed to the other player and the movewill be the move of the new current player. You may assume that the move is then legal. Youshould record the changes to the board, including adding the new piece and changing the colorof all bracketed pieces. At the end of the move, print the number of pieces of each color on theboard in the format ‘Black - xx White - yy’ where xx is the number of black pieces on theboard and yy is the number of white pieces on the board. After a move, the current player willbe 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 terminatesinput for the current game.
You may assume that the commands will be syntactically correct. Put one blank line betweenoutput from separate games and no blank lines anywhere else in the output.
Sample Input
Sample Output
解题心得:
- 这个题在网上看见大神写的代码贼恐怖,超过了四百行,其实不用这样的,代码简洁一点100+行就可以过了,但是注意不要写BUG。
- 这个题目的描述要注意,要下的地方必须能够吃掉对方的棋子。注意要吃掉对方的棋子必须连续。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
char maps[maxn][maxn];
char odr[maxn];
int dir[8][2] = {1,0,1,-1,1,1,-1,1,-1,0,-1,-1,0,1,0,-1};//可以找的八个方向
//初始化地图
void pre_maps()
{
for(int i=1; i<=8; i++)
scanf("%s",maps[i]+1);
}
//检查是否走出地图
bool check2(int x,int y)
{
if(x<1 || y<1 || x>8 || y>8)
return false;
return true;
}
//检查x,y位置是否是合法位置
bool check(int x,int y,char qi)
{
char el_qi;
if(qi == 'B')
el_qi = 'W';
else
el_qi = 'B';
for(int i=0; i<8; i++)
{
if(check2(x+dir[i][0],y+dir[i][1]) && maps[dir[i][0]+x][dir[i][1]+y] == el_qi)
{
int x1 = x,y1 = y;
x1 += dir[i][0];
y1 += dir[i][1];
while(check2(x1,y1))
{
if(maps[x1][y1] == '-')
break;
if(maps[x1][y1] == qi)
return true;
x1 += dir[i][0];
y1 += dir[i][1];
}
}
}
return false;
}
//在找到合法的地点放下棋子之后更新地图
void updata_maps(int x,int y,char qi)
{
char el_qi;
if(qi == 'B')
el_qi = 'W';
else
el_qi = 'B';
for(int i=0; i<8; i++)
{
if(check2(x+dir[i][0],y+dir[i][1]) && maps[dir[i][0]+x][dir[i][1]+y]== el_qi)
{
int x1 = x,y1 = y;
x1 += dir[i][0];
y1 += dir[i][1];
while(check2(x1,y1) && maps[x+dir[i][0]][y+dir[i][1]]==el_qi && maps[x1][y1] != '-')
{
if(maps[x1][y1] == qi)
{
int x2=x,y2=y;
while(x2!=x1 || y2!=y1)
{
if(maps[x2][y2] == el_qi)
maps[x2][y2] = qi;
x2 += dir[i][0];
y2 += dir[i][1];
}
break;
}
x1 += dir[i][0];
y1 += dir[i][1];
}
}
}
}
bool check_M(int x,int y,bool flag)
{
char qi;
if(flag)
qi = 'B';
else
qi = 'W';
if(check(x,y,qi) && maps[x][y] == '-')
{
maps[x][y] = qi;
updata_maps(x,y,qi);
}
else
{
flag = !flag;
if(flag)
qi = 'B';
else
qi = 'W';
maps[x][y] = qi;
updata_maps(x,y,qi);
}
int num_B,num_W;
num_B = num_W = 0;
for(int i=1; i<=8; i++)
for(int j=1; j<=8; j++)
{
if(maps[i][j] == 'B')
num_B++;
else if(maps[i][j] == 'W')
num_W++;
}
printf("Black - %2d White - %2d\n",num_B,num_W);
return flag;
}
void check_L(bool flag)
{
bool prin_flag = false,find_flag = false;
char qi;
if(flag)
qi = 'B';
else
qi = 'W';
for(int i=1; i<=8; i++)
for(int j=1; j<=8; j++)
{
if(maps[i][j] != '-')
continue;
if(check(i,j,qi) && prin_flag)
{
printf(" (%d,%d)",i,j);
find_flag = true;
}
else if(check(i,j,qi) && !prin_flag)
{
printf("(%d,%d)",i,j);
find_flag = true;
prin_flag = true;
}
}
if(!find_flag)//查看是否找到合法的位置
printf("No legal move.\n");
else
printf("\n");
}
void check_Q()
{
for(int i=1; i<=8; i++)
{
for(int j=1; j<=8; j++)
printf("%c",maps[i][j]);
printf("\n");
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(maps,0,sizeof(maps));
pre_maps();
bool flag = false;
while(scanf("%s",odr))
{
//找到第一个走的棋手
if(odr[0] == 'W')
flag = false;
else if(odr[0] == 'B')
flag = true;
if(odr[0] == 'M')
{
flag = check_M(odr[1]-'0',odr[2]-'0',flag);
flag = !flag;
}
else if(odr[0] == 'L')
check_L(flag);
else if(odr[0] == 'Q')
{
check_Q();
break;
}
}
if(t!=0)
printf("\n");
}
return 0;
}
BFS:UVa220 ACM/ICPC 1992-Othello(黑白棋)的更多相关文章
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是 ...
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
- [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 ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...
- hduoj 4707 Pet 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4707 Pet Time Limit: 4000/2000 MS (Java/Others) Memory ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- 黑白棋游戏 (codevs 2743)题解
[问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...
随机推荐
- Codeforces Round #532(Div. 2) C.NN and the Optical IIIusion
链接:https://codeforces.com/contest/1100/problem/C 题意: 一个圆球外面由其他圆球包裹,两两相连. 给出n,r. n为外面圆球数量,r为内部圆球半径. 求 ...
- 1-27TreeSet简介
使用TreeSet存储Integer对象 TreeSet的特点是可以对存放进去的元素进行排序. package com.monkey1024.set; import java.util.TreeSet ...
- python __new__ __init__ 区别
参数 __new__的第一个占位参数是class对象 __init__的第一个占位参数是class的实例对象 其他的参数应一致 作用 __new__ 用来创建实例,在返回的实例上执行__init__, ...
- cucumber 文件目录结构和执行顺序
引用链接:http://www.cnblogs.com/timsheng/archive/2012/12/10/2812164.html Cucumber是Ruby世界的BDD框架,开发人员主要与两类 ...
- hasNextInt()方法
hasNextInt()方法是判断控制台接收是否为数字,当你在控制台输入一个字符的时候,hasNextInt()判断你输入这个字符是不是数字,而不是接收值,当if判断通过之后执行接收,也就是你输入的那 ...
- C#调用C库的注意事项
作者:朱金灿 来源:http://blog.csdn.net/clever101 注意事项一: 从C#的exe进入C库的源码进行调试,需要先"启用非托管代码调试",如下图: 注意事 ...
- iOS --runtime理解
iOS~runtime理解 Runtime是想要做好iOS开发,或者说是真正的深刻的掌握OC这门语言所必需理解的东西.最近在学习Runtime,有自己的一些心得,整理如下,一为 查阅方便二为 或许能给 ...
- com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /192.168.1.13:20881, cause: Failed to bind to: /0.0.0.0:20881
抛出的异常如上,解决方案是:根据异常信息确定是端口被占用,排查项目是否启动之后没有关闭,在windows命令行中运行如下命令:netstat -ano 检查端口占用的情况,根据pid在任务管理器中杀死 ...
- Unity之脚本编译顺序
根据官方的解释,它们的编译顺序如下: (1)所有在Standard Assets.Pro Standard Assets或者Plugins文件夹中的脚本会产生一个Assembly-CSharp-fil ...
- UVA 753 A Plug for UNIX (最大流)
关键在建图,转换器连一条容量无限的边表示可以转化无数次,设备的插头连源点,插座连汇点. dinic手敲已熟练,输出格式又被坑,总结一下,输出空行多case的,一个换行是必要的,最后一个不加空行,有Te ...