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

x1 y1 c1

x2 y2 c2

......

xn yn cn

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: xi and yi and ci. xi and yi are
coordinates of the stone, and ci means the color of the stone. If ci=0 the stone is white. If ci=1 the stone is black. It is guaranteed that 0<=xi,yi<=14, and ci=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 1st 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 2nd move. In this situation, print:



Lose in 2 moves.



3)Mr. Lucky wins at the 3rd 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 1st 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<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define N 20
const 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<n;i++)
{
scanf("%d%d%d",&x,&y,&d);
g[x][y]=d;
if(d==0)
w++;
else
b++;
}
if(w>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;
}

hdu 3683 Gomoku (模拟、搜索)的更多相关文章

  1. HDU - 4431 Mahjong (模拟+搜索+哈希+中途相遇)

    题目链接 基本思路:最理想的方法是预处理处所有胡牌的状态的哈希值,然后对于每组输入,枚举每种新加入的牌,然后用哈希检验是否满足胡牌的条件.然而不幸的是,由于胡牌的状态数过多(4个眼+一对将),预处理的 ...

  2. [博弈] hdu 3683 Gomoku

    题意: 两个人下五子棋.给你现有棋盘,推断在三步之内的胜负情况. 输出分为几种. 1.棋盘不合法 2.黑或白在第一步赢下在(x,y)点,多个输出x最小的.y最小的. 3.输在第二步 4.黑或白在第三步 ...

  3. 【LOJ6254】最优卡组 堆(模拟搜索)

    [LOJ6254]最优卡组 题面 题解:常用的用堆模拟搜索套路(当然也可以二分).先将每个卡包里的卡从大到小排序,然后将所有卡包按(最大值-次大值)从小到大排序,并提前处理掉只有一张卡的卡包. 我们将 ...

  4. 【BZOJ4524】[Cqoi2016]伪光滑数 堆(模拟搜索)

    [BZOJ4524][Cqoi2016]伪光滑数 Description 若一个大于1的整数M的质因数分解有k项,其最大的质因子为Ak,并且满足Ak^K<=N,Ak<128,我们就称整数M ...

  5. 【BZOJ4345】[POI2016]Korale 堆(模拟搜索)

    [BZOJ4345][POI2016]Korale Description 有n个带标号的珠子,第i个珠子的价值为a[i].现在你可以选择若干个珠子组成项链(也可以一个都不选),项链的价值为所有珠子的 ...

  6. JavaScript在表格中模拟搜索多关键词搜索和筛选

    模拟搜索需要实现以下功能: 1.用户的模糊搜索不区分大小写,需要小写字母匹配同样可以匹配到该字母的大写单词. 2.多关键词模糊搜索,假设用户关键词以空格分隔,在关键词不完整的情况下仍然可以匹配到包含该 ...

  7. HDU 3683 模拟&amp;搜索

    给出五子棋残局,推断三步内能否分出胜负,玩家为当前该走旗子的颜色,下一步为白棋或黑棋不定. 依照顺序推断就可以: 1:推断棋盘是否合法,并确定玩家颜色 2:推断当前玩家颜色是否有一个必胜点,有玩家则在 ...

  8. HDU 3262 Seat taking up is tough (模拟搜索)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3262 题意:教室有n*m个座位,每个座位有一个舒适值,有K个学生在不同时间段进来,要占t个座位,必须是连 ...

  9. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

随机推荐

  1. Windows Phone开发(44):推送通知第二集——磁贴通知

    原文:Windows Phone开发(44):推送通知第二集--磁贴通知 前面我们说了第一个类型--Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对 ...

  2. erlang shell表格数据对齐

    近期在erlang shell做一些測试,为了让測试结果数据显得更直观,想对齐须要打印的数据,做成像表格一样的效果. 開始的想法是在数据中插入tab. 当然,erlang也有对tab的支持,但实际效果 ...

  3. Cnblogs API

    http://wcf.open.cnblogs.com/news/help http://wcf.open.cnblogs.com/blog/help/ 不支持用户登陆

  4. java程序开发代写(QQ:928900200)

    条件:手机1.2都是安卓智能机,手机1开热点,手机2链接手机1,功能:A手机2通过刷手机网页,登陆手机1设定的页面并下载其手机的指定文件,B手机1控制手机2的流量,当通过的流量多的时候,停止流量的供应

  5. SharePoint 2010 新列表模板列表

    SharePoint 2010 新列表模板列表 项目描述叙事 发展环境创造了良好的名单为模板.然后使用列表模板将其复制到生产环境. 脚步 1. 打开"列表设置",找到"将 ...

  6. VC版八皇后

    一.  功能需求: 1. 可以让玩家摆棋,并让电脑推断是否正确 2. 能让电脑给予帮助(给出全部可能结果) 3. 实现悔棋功能 4. 实现重置功能 5. 加入点按键音效果更佳 二.  整体设计计: 1 ...

  7. AC自己主动机

    AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS  这里另一个Kuangbin开的比赛,大家 ...

  8. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  9. 白板编程浅谈——Why, What, How(转)

    原文链接:http://lucida.me/blog/whiteboard-coding-demystified/ 这篇文章节选自我正在撰写的一本关于应届生面试求职的书籍,欢迎在评论或微博(@peng ...

  10. UIControl-IOS发展

    UIKit提供了一组控件:UISwitch开关.UIButtonbutton.UISegmentedControl分段控件.UISlider滑块.UITextField文本字段控件. UIPageCo ...