HDU 3683 Gomoku

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3683

题目大意:给一个五子棋的棋局,求在3步之内的胜负可能。

题目分析:嗯,比较简单是吧。

现在首先设给定棋局由A方先走,则只需判定如下情况:

1、若A有获胜策略,则A一步获胜。

2、若1不成立,B有两个或以上获胜策略,则B的第二步获胜(A第一步只能阻止一种策略)。

3、若1不成立,B只有一种获胜策略,则A的第一步必然在B的获胜点上,然后判断此时A是否有两个以上必胜点,若有,则A三步获胜。

4、若以上情况均不成立,则枚举A的第一步走法,如果有一种第一步使A拥有两个获以上获胜策略,则A三步获胜。否则三步之内没人获胜。

 

附代码:

View Code
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

struct point
{
int x, y;
};

int n, white, black;
int player, other;
int a[15][15];
int dir[4][2]={{0,1}, {1,0}, {1,1}, {1,-1}};
char str[][10]={"", "white", "black"};

bool init()
{
int i, col;
point tmp;
scanf("%d", &n);
if (n == 0)
{
return false;
}
memset(a, 0, sizeof(a));
white=black=0;
for (i=0;i<n;i++)
{
scanf("%d %d %d", &tmp.x, &tmp.y, &col);
if (col==0)
{
white++;
a[tmp.x][tmp.y] = 1;
}
else
{
black++;
a[tmp.x][tmp.y] = 2;
}
}
return true;
}

bool Check()
{
if (black == white)
{
player = 2;
other = 1;
}
else if (black == white + 1)
{
player = 1;
other = 2;
}
else
{
return false;
}
return true;
}
bool Bound(point p)
{
return p.x >= 0 && p.x <= 14 && p.y >= 0 && p.y <= 14;
}
int Calc(point p, int c)
{
int res = 0;
for (int i = 0; i < 4; i++)
{
int len = 0;
point tmp = p;
while (true)
{
tmp.x = tmp.x + dir[i][0];
tmp.y = tmp.y + dir[i][1];
if (!Bound(tmp) || a[tmp.x][tmp.y] != c)
{
break;
}
len++;
}
tmp = p;
while (true)
{
tmp.x = tmp.x - dir[i][0];
tmp.y = tmp.y - dir[i][1];
if (!Bound(tmp) || a[tmp.x][tmp.y] != c)
{
break;
}
len++;
}
len++;
if (len > res)
{
res = len;
}
}
return res;
}
int GetWinPoint(point &p, int c)
{
int num=0;
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
point tmp;
tmp.x = i;
tmp.y = j;
if (a[tmp.x][tmp.y] == 0 && (Calc(tmp, c)) >= 5)
{
num++;
if (num == 1)
{
p = tmp;
}
}
}
}
return num;
}

void work()
{
if (!Check())
{
printf("Invalid.\n");
return;
}
point p, p2;
int num;
if (GetWinPoint(p, player) >= 1)
{
printf("Place %s at (%d,%d) to win in 1 move.\n", str[player], p.x, p.y);
return;
}
if ((num = GetWinPoint(p, other)) >= 2)
{
printf("Lose in 2 moves.\n");
return;
}
if (num == 1)
{
a[p.x][p.y] = player;
if (GetWinPoint(p2, player) >= 2)
{
printf("Place %s at (%d,%d) to win in 3 moves.\n", str[player], p.x, p.y);
return;
}
}
else
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
if (a[i][j] == 0)
{
a[i][j] = player;
if (GetWinPoint(p2, player) >= 2)
{
printf("Place %s at (%d,%d) to win in 3 moves.\n", str[player], i, j);
return;
}
a[i][j] = 0;
}
}
}
}
printf("Cannot win in 3 moves.\n");
}

int main()
{
while (init())
{
work();
}
return 0;
}



posted @ 2012-02-29 15:49  Evan1004  阅读(192)  评论(0编辑  收藏  举报