HDU 3683 Gomoku(模拟)

18 篇文章 0 订阅

Gomoku

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



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.
 

Source
2010 Asia Hangzhou Regional Contest

大体题意:
给你一个已经有不少棋子的五子棋残局,让你判断出当前是否合法,如果合法的话,问最后结局情况,(三步以内), 可以有 1步内秒杀对方, 第二步浪输,第三步翻盘对方= =!或者是和局!
吐槽:
比赛没有去尝试做,只有盲目跟榜了,赛后才发现,这个题目才符合自己的口味= = !(毕竟也喜欢下五子棋嘛!)
还需努力!!!
思路:
因为判断是三步以内,所以只要理清逻辑顺序就很简单了,不然就成了做一只Alpha狗了= = !
可以写一个函数win()表示当前玩家能否一步赢得游戏,如果能直接return true,如果不能赢得游戏,则需要下一步最优解,在return false。
先说说整体思路:
定义cur变量 (取值0或1) 为当前玩家,如果黑棋多于白棋1个以上,或者 白棋多于黑棋,则不合法!
如果一步能赢,则输出一步赢,否则下一步,换玩家,如果能一步赢,输出第二步输掉,否则在换玩家,如果能赢,输出三步内能赢,否则输出三步谁也赢不了。
能否赢 只用一个win()函数就可以实现。
win()函数判断能否一步赢可以,关键是赢不了的话,怎么去下棋?
先堵对方,枚举对方能一步赢得地方,如果发现,下在那里,先保证不输!
如果没有地方可堵对方,那么就找自己五子连珠最多的地方!  (两层枚举即可)

教训:
用位运算一定要注意优先级括号问题,查了快半个多小时的BUG,, T_T!!

详细见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn =  30;
int a[maxn][maxn];
int cur;
int x1,y1;
const int Border = 15;
const int dx[] = {0,0,-1,1,-1,1,1,-1};
const int dy[] = {1,-1,0,0,1,-1,1,-1};
bool init(int x,int y){
    return x >= 0 && x < Border && y >= 0 && y < Border;
}
bool win1(){
//===========================================================
 // nexr step I can win!!!!
    for (int i = 0; i < Border; ++i){
        for (int j = 0; j < Border; ++j){
            if (a[i][j] != -1)continue;
            for (int k = 0; k < 8; ++k){
                int sum = 0;
                for (int l = 1; ; ++l){
                    int xx = i+dx[k]*l;
                    int yy = j+dy[k]*l;
                    if (!init(xx,yy) || a[xx][yy] != cur)break;
                    ++sum;
                }
                for (int l = -1; ; --l){
                    int xx = i+dx[k]*l;
                    int yy = j+dy[k]*l;
                    if (!init(xx,yy) || a[xx][yy] != cur)break;
                    ++sum;
                }
                if (sum >= 4){
                    x1 = i;
                    y1 = j;
                    return true;
                }
            }
        }
    }
//==============================================================
// I must defeat!
    bool ok =false;
    for (int i = 0; i < Border; ++i){
        for (int j = 0; j < Border; ++j){
            if (a[i][j] != -1)continue;
            for (int k = 0; k < 8; ++k){
                int sum = 0;
                for (int l = 1; ; ++l){
                    int xx = i+dx[k]*l;
                    int yy = j+dy[k]*l;
                    if (!init(xx,yy) || a[xx][yy] != (cur^1))break;
//                    printf("xx = %d,yy = %d\n",xx,yy);
                    ++sum;
                }
                for (int l = -1; ; --l){
                    int xx = i+dx[k]*l;
                    int yy = j+dy[k]*l;
                    if (!init(xx,yy) || a[xx][yy] != (cur^1))break;
                    ++sum;
                }
                if (sum >= 4){
                    ok=true;
                    x1 = i;
                    y1 = j;
                    a[x1][y1] = cur;
//                        printf("x1 = %d,y1 = %d,cur = %d\n",x1,y1,sum);
                    return false;
                }
            }
        }
    }


//==============================================================
// aha,,Finally, you can't win!!!!!!!!!!!!!!!!!!
    int anssum = -1;
    for (int i = 0; i < Border; ++i){
        for (int j = 0; j < Border; ++j){
            if (a[i][j] != -1)continue;
            a[i][j] = cur;
            int ans = 0;
            for (int ii = 0; ii < Border; ++ii){
                for (int jj = 0; jj < Border; ++jj){
                    if (a[ii][jj] != -1)continue;
                    for (int k = 0; k < 8; ++k){
                        int sum = 0;
                        for (int l = 1; ; ++l){
                            int xx = ii+dx[k]*l;
                            int yy = jj+dy[k]*l;
                            if (!init(xx,yy) || a[xx][yy] != cur)break;
                            ++sum;
                        }
                        for (int l = -1; ; --l){
                            int xx = ii+dx[k]*l;
                            int yy = jj+dy[k]*l;
                            if (!init(xx,yy) || a[xx][yy] != cur)break;
                            ++sum;
                        }
                        if (sum >= 4)++ans;
                    }
                }

            }
            if (ans > anssum){
                anssum = ans;
                x1 = i;
                y1 = j;
            }
            a[i][j] = -1;
        }
    }
    a[x1][y1] = cur;

    return false;
}
int main(){
    int n;
    while(scanf("%d",&n) == 1 && n){
        int sum_white = 0,sum_black = 0;
        memset(a,-1,sizeof a);
        for (int i = 0; i < n; ++i){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            a[u][v] = w;
            if (w == 0)sum_white++;
            else sum_black++;
        }
        if (sum_black < sum_white || sum_black-sum_white > 1){
            printf("Invalid.\n");
            continue;
        }
        if (sum_black == sum_white)cur = 1;
        else cur = 0;
        if (win1()){
            printf("Place %s at (%d,%d) to win in 1 move.\n",cur == 0 ? "white" : "black",x1,y1);
            continue;
        }
        int ansx = x1,ansy = y1;
        cur ^= 1;
        if (win1()){
            printf("Lose in 2 moves.\n");
            continue;
        }
        cur^=1;
        if (win1()){
            printf("Place %s at (%d,%d) to win in 3 moves.\n",cur == 0 ? "white" : "black",ansx,ansy);
            continue;
        }
        printf("Cannot win in 3 moves.\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值