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


解题心得:

  1. 这个题在网上看见大神写的代码贼恐怖,超过了四百行,其实不用这样的,代码简洁一点100+行就可以过了,但是注意不要写BUG。
  2. 这个题目的描述要注意,要下的地方必须能够吃掉对方的棋子。注意要吃掉对方的棋子必须连续。

#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;
}

最新文章

  1. Python爬虫小白入门(一)写在前面
  2. AngularJS多模块开发
  3. ios开发错误之: Undefined symbols for architecture x86_64
  4. Hibernate 中出现 users is not mapped 问题 (转)
  5. php分页的实现
  6. [Tools] 使用XP远程登录Win8系统
  7. Js_Ajax_输入词提示
  8. Kmeans算法学习与SparkMlLib Kmeans算法尝试
  9. foreach中引用 的问题
  10. HTML5——localStorage
  11. 使用Multiplayer Networking做一个简单的多人游戏例子-1/3(Unity3D开发之二十五)
  12. restTemplate 发送http post请求带有文件流、参数
  13. shell实现自动部署两台tomcat项目Ⅱ
  14. Nginx ssl证书部署方法
  15. 【Java初探03】——流程控制语句
  16. js提取新浪邮箱的信用卡
  17. Win_Server_2008 安装 Oracle_11g EM时上载EM资料失败
  18. matlab GUI工作原理
  19. 在VC中创建DLL文件的方法步骤
  20. RHCE7 管理II-2 通过grep使用正则表达式

热门文章

  1. HDU6298(2018多校第一场)
  2. 语义分割丨DeepLab系列总结「v1、v2、v3、v3+」
  3. Top-Down和Bottom-Up位图的区别
  4. React 实践记录 02 Flux introduction
  5. nodejs中的异步回调机制
  6. SQLServer外键查询删除信息
  7. Android学习总结(三)——IntentService的用法
  8. mongo ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
  9. python在d盘,robotframework引入seleniumlibrary报错
  10. 利用python实现整数转换为任意进制字符串