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个方格称为相邻方 ...
随机推荐
- CodeSmith Generator 7.0.2
[工具]CodeSmith Generator 7.0.2激活步骤 只看楼主 收藏 回复 M炎骫毒逆天T c#攻城狮 8 学过三层的人应该认识CodeSmith Generator吧, ...
- 从零开始利用vue-cli搭建简单音乐网站(五)
上一篇文章讲到的是如何利用mongoose从数据库读取数据然后更新页面,接下来要实现的就是用户注册登录功能,这个功能涉及到的东西太多了,今天只实现了登录功能,登陆之后更新导航条界面,最后效果如下: 登 ...
- 详解ASP.NET缓存机制
文中对ASP.NET的缓存机制进行了简述,ASP.NET中的缓存极大的简化了开发人员的使用,如果使用得当,程序性能会有客观的提升.缓存是在内存存储数据的一项技术,也是ASP.NET中提供的重要特性之一 ...
- https握手失败案例(一)
OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .read ...
- css水平垂直居中的几个方法和技巧/居中之美
水平居中设置-行内元素 我们在实际工作中常会遇到需要设置水平居中场景,今天我们就来看看怎么设置水平居中的. 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-ali ...
- 起学习iOS开发专用词汇
今天的单词分别是: l Asynchronous 形容词 异步的 n 副词形式: asynchronously 异步地 n 缩写:ASYNC n 反义词:synchronous 形容词同步 ...
- CocoaPods 安装使用
CocoaPods是一个负责管理iOS项目中第三方开源代码的工具.CocoaPods项目的源码在Github上管理.该项目开始于2011年8月 12日,经过一年多的发展,现在已经超过1000次提交,并 ...
- 原生js格式化json的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WPF知识点全攻略07- 数据绑定(Binding)
数据绑定是WPF不得不提,不得不会系列之一 数据绑定简言之,就是把数据源的数据绑定到目标对象的属性上.目标对象可以是承自DependencyProperty的任何可访问的属性或控件,目标属性必须为依赖 ...
- 浏览器输入一个url到整个页面显示出来经历了哪些过程?
https://cloud.tencent.com/developer/article/1396399 https://www.cnblogs.com/haonanZhang/p/6362233.ht ...