博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3683 Gomoku (模拟、搜索)
阅读量:6888 次
发布时间:2019-06-27

本文共 7810 字,大约阅读时间需要 26 分钟。

Gomoku

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1319    Accepted Submission(s): 328


Problem Description
You are probably not familiar with the title, “Gomoku”, but you must have played it a lot. Gomoku is an abstract strategy board game and is also called Five in a Row, or GoBang. It is traditionally played with go pieces (black and white stones) on a go board (19x19 intersections). Nowadays, standard chessboard of Gomoku has 15x15 intersections. Black plays first, and players alternate in placing a stone of their color on an empty intersection. The winner is the first player to get an unbroken row of five or more stones horizontally, vertically, or diagonally. 
For convenience, we coordinate the chessboard as illustrated above. The left-bottom intersection is (0,0). And the bottom horizontal edge is x-axis, while the left vertical line is y-axis. 
I am a fan of this game, actually. However, I have to admit that I don’t have a sharp mind. So I need a computer program to help me. What I want is quite simple. Given a chess layout, I want to know whether someone can win within 3 moves, assuming both players are clever enough. Take the picture above for example. There are 31 stones on it already, 16 black ones and 15 white ones. Then we know it is white turn. The white player must place a white stone at (5,8). Otherwise, the black player will win next turn. After that, however, the white player also gets a perfect situation that no matter how his opponent moves, he will win at the 3rd move. 
So I want a program to do similar things for me. Given the number of stones and positions of them, the program should tell me whose turn it is, and what will happen within 3 moves.
 

Input
The input contains no more than 20 cases.
Each case contains n+1 lines which are formatted as follows.
n
x
1 y
1 c
1
x
2 y
2 c
2
......
x
n y
n c
n
The first integer n indicates the number of all stones. n<=222 which means players have enough space to place stones. Then n lines follow. Each line contains three integers: x
i and y
i and c
i. x
i and y
i are coordinates of the stone, and c
i means the color of the stone. If c
i=0 the stone is white. If c
i=1 the stone is black. It is guaranteed that 0<=x
i,y
i<=14, and c
i=0 or 1. No two stones are placed at the same position. It is also guaranteed that there is no five in a row already, in the given cases.
The input is ended by n=0.
 

Output
For each test case:
First of all, the program should check whose turn next. Let’s call the player who will move next “Mr. Lucky”. Obviously, if the number of the black stone equals to the number of white, Mr. Lucky is the black player. If the number of the black stone equals to one plus the numbers of white, Mr. Lucky is the white player. If it is not the first situation or the second, print “Invalid.” 
A valid chess layout leads to four situations below:
1)Mr. Lucky wins at the 1
st move. In this situation, print :
Place TURN at (x,y) to win in 1 move.
“TURN” must be replaced by “black” or “white” according to the situation and (x,y) is the position of the move. If there are different moves to win, choose the one where x is the smallest. If there are still different moves, choose the one where y is the smallest.
2)Mr. Lucky’s opponent wins at the 2
nd move. In this situation, print:
Lose in 2 moves.
3)Mr. Lucky wins at the 3
rd move. If so, print:
Place TURN at (x,y) to win in 3 moves.
“TURN” should replaced by “black” or “white”, (x,y) is the position where the Mr. Lucky should place a stone at the 1
st move. After he place a stone at (x,y), no matter what his opponent does, Mr. Lucky will win at the 3[sup]rd[sup] step. If there are multiple choices, do the same thing as described in situation 1. 
4)Nobody wins within 3 moves. If so, print:
Cannot win in 3 moves.
 

Sample Input
 
31 3 3 1 3 4 0 3 5 0 3 6 0 4 4 1 4 5 1 4 7 0 5 3 0 5 4 0 5 5 1 5 6 1 5 7 1 5 9 1 6 4 1 6 5 1 6 6 0 6 7 1 6 8 0 6 9 0 7 5 1 7 6 0 7 7 1 7 8 1 7 9 0 8 5 0 8 6 1 8 7 0 8 8 1 8 9 0 9 7 1 10 8 0 1 7 7 1 1 7 7 0 0
 

Sample Output
 
Place white at (5,8) to win in 3 moves. Cannot win in 3 moves. Invalid.
 

思路:1、推断先手能否在一步走赢,即是否存在一空白格子使得先手的棋子有连续的5个。

2、推断对手是否存在两个空白格子使得他可以得到连续的5个棋子,由于这样,先手就不能堵住后手。

3、枚举任一空白格子放先手棋子。则仅仅需对手方不存在“一空白格子使得棋子有连续的5个”,且先手方此时有"两个空白格子使得他可以得到连续的5个棋子",则先手胜。(攻防转换)

#include
#include
#include
#include
#include
using namespace std;#define N 20const int inf=0x3fffffff;const double eps=1e-8;int xx,yy;int dx[4]={0,1,1,1};int dy[4]={1,1,0,-1};int g[N][N];int bfs1(int v) //推断是否存在一个空白格子使棋子连成连续的5个{ int i,j,k,x,y,sum; for(i=0;i<15;i++) { for(j=0;j<15;j++) { if(g[i][j]==-1) { for(k=0;k<4;k++) { x=i+dx[k]; y=j+dy[k]; sum=1; while(1) { if(x<0||x>=15||y<0||y>=15||g[x][y]!=v) break; sum++; x+=dx[k]; y+=dy[k]; } x=i-dx[k]; y=j-dy[k]; while(1) { if(x<0||x>=15||y<0||y>=15||g[x][y]!=v) break; sum++; x-=dx[k]; y-=dy[k]; } if(sum>=5) { xx=i;yy=j; return 1; } } } } } return 0;}int bfs2(int v) //推断是否存在2个空白格子使棋子连成连续的5个{ int i,j,k,x,y,sum,num=0; for(i=0;i<15;i++) { for(j=0;j<15;j++) { if(g[i][j]==-1) { for(k=0;k<4;k++) { x=i+dx[k]; y=j+dy[k]; sum=1; while(1) { if(x<0||x>=15||y<0||y>=15||g[x][y]!=v) break; sum++; x+=dx[k]; y+=dy[k]; } x=i-dx[k]; y=j-dy[k]; while(1) { if(x<0||x>=15||y<0||y>=15||g[x][y]!=v) break; sum++; x-=dx[k]; y-=dy[k]; } if(sum>=5) { if(num==1) return 1; num++; break; } } } } } return 0;}int bfs3(int v) //情况3,{ int i,j; for(i=0;i<15;i++) { for(j=0;j<15;j++) { if(g[i][j]==-1) { g[i][j]=v; if(bfs1(1-v)==0&&bfs2(v)==1) { xx=i;yy=j; return 1; } g[i][j]=-1; } } } return 0;}int main(){ int i,n,x,y,d,w,b,val; while(scanf("%d",&n),n) { memset(g,-1,sizeof(g)); w=b=0; for(i=0;i
b) { printf("Invalid.\n"); continue; } if(w==b) val=1; else val=0; if(bfs1(val)) { if(val==0) printf("Place white at (%d,%d) to win in 1 move.\n",xx,yy); else printf("Place black at (%d,%d) to win in 1 move.\n",xx,yy); } else if(bfs2(1-val)) printf("Lose in 2 moves.\n"); else { if(bfs3(val)) { if(val==0) printf("Place white at (%d,%d) to win in 3 moves.\n",xx,yy); else printf("Place black at (%d,%d) to win in 3 moves.\n",xx,yy); } else printf("Cannot win in 3 moves.\n"); } } return 0;}

转载地址:http://tetbl.baihongyu.com/

你可能感兴趣的文章
PAT A1071
查看>>
【笔记】重学前端-winter
查看>>
windows下重装xampp并做mysql数据迁移的步骤
查看>>
Java日志组件间关系
查看>>
聊聊前端国际化文案该如何处理
查看>>
JS难点之hoist
查看>>
“独角兽”企业都爱选择腾讯云,背后原因值得考究
查看>>
浅析 Vue 2.6 中的 nextTick 方法
查看>>
199. Binary Tree Right Side View
查看>>
配置SpringBoot方便的切换jar和war
查看>>
2018最佳GAN论文回顾(下)
查看>>
Vue使用element-ui所遇BUG与需求集结(二)
查看>>
弹性公网EIP,让网络更自由、灵活
查看>>
一对一直播源码都实现了哪几种常见的优化技术? ...
查看>>
Unity学习系列一简介
查看>>
利用Python框架pyxxnet_project实现的网络服务
查看>>
一个最简单的WebSocket hello world demo
查看>>
C# 8.0的三个令人兴奋的新特性
查看>>
关于ip_conntrack跟踪连接满导致网络丢包问题的分析
查看>>
烂泥:linux学习之VNC远程控制(一)
查看>>