您的位置:首页 > 其它

ACDREAM 05B Circle vs Triangle(DFS专场)

2015-06-06 19:46 309 查看

ACDREAM 05B Circle vs Triangle

Problem Description

Alice and Bob invent a new game called “circle vs triangle”. At first there are a rectangle map, which consists of n*m unit-squares. Then lady first. Alice can choose any unit square and write a circle in it. And then Bob choose a unit square which next to the one that Alice just choose, and write a triangle in it. Turning to Alice again. Each player should choose a unit square which next to the one that another one chooses, and write a shape standing for him or her.

However, Alice and Bob find such silly game is so boring. Then they let some square in the map become barriers, which means these square can’t be written anything on it.

So, give you a map, please tell me who can win or it can lead to a draw, if both Alice and Bob make a optimal choice.

Input

Multiple case, and each case to begin with two integers: n,m(1<=n,m<=5).

Then an n*m map is shown. Please read the sample to get more informations.

Output

If the map can lead to a draw, please output “Draw”.

Otherwise, please output the name of player who can win if both make optimal choice.

Sample Input

3 3



3 3

**.



Sample Output

Alice

Bob

Hint

For the first case, Alice can write a circle at the left lower corner, then Bob has to write a triangle beside it. After Alice write a circle at the right lower corner, Bob has no way to write. So Alice win.

For the second case, Bob will win in the end wherever Alice choose firstly.

题目大意:两个人在有障碍的棋盘上下棋,游戏规则释放棋子,谁先放不了,谁就输。并且下一个人放棋子的位置必须是上一个人放棋子位置的周围(上下左右,第一个下棋的人可以任意放在无障碍的位置上)。两个人都会选择最优下法。问最后的获胜者是谁。

解题思路:DFS + 博弈。遍历起始位置,进行DFS。寻找必败态(没地方可以下就是必败),必败态的上一层是必胜态(同,必胜态的上一层是必败态)。DFS一层一层往下推,若遍历完所有初始点,先手还是必败,则先手败(最优下法),否则后手败。

[code]#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Node{
    int x, y;
}rec[30];
int n, m, cnt;
char map[6][6];
int vis[6][6];
int DFS(int x, int y) {
    int flag = 0;
    for (int i =  0; i < 4; i++) {
        int px = x + dir[i][0], py = y + dir[i][1];
        if (px < 0 || py < 0 || px >= n || py >= m) continue;
        if (vis[px][py]) continue;
        if (map[px][py] == '*') continue;
        vis[px][py] = 1;
        if (DFS(px, py)) {
            flag = 1;
        } 
        vis[px][py] = 0;
    }   
    if (flag) return 0;
    else return 1;
}
int main() {
    while (scanf("%d %d\n", &n, &m) == 2) {
        memset(vis, 0, sizeof(vis));
        cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                scanf("%c", &map[i][j]);
                if (map[i][j] == '.') {
                    rec[cnt].x = i;
                    rec[cnt++].y = j;
                }
            }
            getchar();
        }
        int flag = 0 ;
        for (int i = 0; i < cnt; i++) {
            vis[rec[i].x][rec[i].y] = 1;
            if (DFS(rec[i].x, rec[i].y)) {
                flag = 1;
                break;
            }
            vis[rec[i].x][rec[i].y] = 0;
        }
        if (flag) {
            printf("Alice\n");
        } else {
            printf("Bob\n");
        }
    } 
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: