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个方格称为相邻方 ...
随机推荐
- [BZOJ2251/BJWC2010]外星联络
Description 小 P 在看过电影<超时空接触>(Contact)之后被深深的打动,决心致力于寻找外星人的事业.于是,他每天晚上都爬在屋顶上试图用自己的收音机收听外星人发来的信息. ...
- UVa12304(计算几何中圆的基本操作)
断断续续写了250多行的模拟,其间被其他事情打扰,总共花了一天才AC吧~ 这道题目再次让我明白,有些事情看起来很难,实际上并没有我们想象中的那么难.当然了我主要指的不是这个题的难度…… 也是初学计算几 ...
- python入门之排序,文件操作
排序 li.sort() 对li列表从小到大排序,直接更新li列表 sorted(li) 返回一个li排序后的列表,并非直接对li作更新 列表元素必须是同一种数据类型 文件操作 打开文件: f = o ...
- JS脚本不能执行
这段时间在做前端的动态页面,出了很多问题,因为js平时用的很少,所以花了不少无用功. 其中有两点一定要注意: 1.当js中有语法错误时,js脚本会无法执行. 2.当js脚本中有未定义的变量时,后边的语 ...
- Linux 增加虚拟内存
1. 用 df -h 命令找一个比较大的磁盘空间 2.建立swap文件 大小为2G count= 3.启用虚拟内存 1. 将swap文件设置为swap分区文件 mkswap swapfile #(由于 ...
- 我喜欢的两个js类实现方式 现在再加上一个 极简主义法
闭包实现 变量是不会变的:) var myApplication = function(){ var name = 'Yuri'; var age = '34'; var status = 'sing ...
- 【进度总结】第一个web应用程序(未完成)
web程序快速导航 使用Eclipse for Java EE Web Development,并配置Tomcat,这部分内容在众多教程中都描述的十分详细.我直接从代码部分开始记录流程: 这张图是We ...
- siege4压测脚本示例
agent="Siege 1.0"rcconfig="/opt/siege4.0/etc/siegerc"concurrent=$1repet=$2url=&q ...
- 将sql 查询结果导出到excel
在平时工作中经常会遇到,sql 查询数据之后需要发送给业务人员,每次都手工执行脚本然后拷贝数据到excel中,比较耗时耗力,可以考虑自动执行查询并将结果邮件发送出来. 分两步实现: 1.执行查询将结果 ...
- X和面试随笔
第一次参加了面试,面试官很好,我写的笔试和回答的都很差劲,虽然技术方面的回答我想抽自己,但是人家还是要了,给了我一个机会,很感谢. 第一道题:设计一个进销存系统的表结构设计 1:老板每天要知道卖出的货 ...