Sicily 1737. Funny Game

1737. Funny Game

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Let me introduce an interesting game to you: in a 8 X 8 board, two players (suppose they’re Mr. Black and Mrs. White) alternately lay black and white stones respectively onto the units of that board. When one of them, suppose Mr. Black, puts a black stone onto the board, then we check the 8 directions of that stone, i. e. up, down, left, right, up left, up right, left down and right down. If there’s another black stone in one of the above direction and between the two black stones are all white stones, then those white stones are all “eaten” and replaced with black stones. As to the white stones, the rule is the same. Another rule is as follows: for any move of either player he must eat at least one stone of his opponent. When either of them cannot lay stone onto that board any more, the game ends. The winner is the one who has more stones on the board. If they have the same number of stones, it’s a draw game. 
For example, at first:


Then Mr. Black places a black stone at row 2 column 1, the board becomes:


Then Mrs. White places a white stone at row 3 column 3, the board becomes:


Now you are given the state of that board and who will move first, and suppose that both of them are clever enough to make the best choices, please find out who will win at last or whether it’s a draw game.

Input

For each case there’s an 8 X 8 board (‘b’ for black stone, ‘w’ for white stone, ‘e’ for empty unit) in the first 8 lines. And the following line is for the one who is to move first (Black or White). After all cases there’s a string of “EndOfInput” (without quotes). There’s no space in the input file. 
For each case in the input file there’re no more than 10 empty units at the initial state. 

Output

For each case output one line containing the winner (Black or White) or “Draw” (without quotes) for draw game. Make sure that there are no redundant spaces in each line.

Sample Input

wwwwwwww
wwwwwwww
wwwwwwww
wwwwwwww
wwwwwwww
wwwwwwww
wwwwwwww
wwwwwwwe
Black
wwwwwwww
wwwwwwww
wwwwwwww
wwwwwwww
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
White
EndOfInput

Sample Output

White

Draw

// Problem#: 1737
// Submission#: 3584444
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>
#include <assert.h>

int grid[8][8];
int num, empty[10][2], enable[10];
const int path[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
int cal() {
    int i, j, whitecount = 0, blackcount = 0;
    for (i = 0; i < 8; i++)
        for (j = 0; j < 8; j++) {
            if (grid[i][j] == 1) whitecount++;
            else if (grid[i][j] == -1) blackcount++;
            else assert(grid[i][j] == 0);
        }
        if (whitecount > blackcount) return 1;
        else if (whitecount == blackcount) return 0;
        else return -1;
}

int black();

int white() {
    int i, j, k, u, v, loop, temp1, temp2, res = -1, tempres;
    int rem[32][2], remnum;
    int tag = 0;
    for (i = 0; i < num; i++)
        if (enable[i]) {
            enable[i] = 0;
            u = empty[i][0];
            v = empty[i][1];
            grid[u][v] = 1;
            remnum = 0;
            for (loop = 0; loop < 8; loop++) {
                for (j = u, k = v; 0 <= j + path[loop][0] && j + path[loop][0] < 8
                    && 0 <= k + path[loop][1] && k + path[loop][1] < 8
                    && grid[j + path[loop][0]][k + path[loop][1]];) {
                    j += path[loop][0];
                    k += path[loop][1];
                    if (grid[j][k] == grid[u][v]) {
                        for (temp1 = u + path[loop][0], temp2 = v + path[loop][1];
                        (temp1 != j || temp2 != k);
                        temp1 += path[loop][0], temp2 += path[loop][1]) {
                            rem[remnum][0] = temp1;
                            rem[remnum][1] = temp2;
                            remnum++;
                        }
                        break;
                    }
                }
            }
            if (remnum > 0) {
                for (loop = 0; loop < remnum; loop++) {
                    j = rem[loop][0];
                    k = rem[loop][1];
                    grid[j][k] = -grid[j][k];
                }
                tempres = black();
                tag = 1;
                if (tempres > res) res = tempres;
                for (loop = 0; loop < remnum; loop++) {
                    j = rem[loop][0];
                    k = rem[loop][1];
                    grid[j][k] = -grid[j][k];
                }
            }
            grid[u][v] = 0;
            enable[i] = 1;
        }
    if (tag == 0) return cal();
    else return res;
}

int black() {
    int i, j, k, u, v, loop, temp1, temp2, res = 1, tempres;
    int rem[32][2], remnum;
    int tag = 0;
    for (i = 0; i < num; i++)
        if (enable[i]) {
            enable[i] = 0;
            u = empty[i][0];
            v = empty[i][1];
            grid[u][v] = -1;
            remnum = 0;
            for (loop = 0; loop < 8; loop++) {
                for (j = u, k = v; 0 <= j + path[loop][0] && j + path[loop][0] < 8
                    && 0 <= k + path[loop][1] && k + path[loop][1] < 8
                    && grid[j + path[loop][0]][k + path[loop][1]];) {
                    j += path[loop][0];
                    k += path[loop][1];
                    if (grid[j][k] == grid[u][v]) {
                        for (temp1 = u + path[loop][0], temp2 = v + path[loop][1];
                        (temp1 != j || temp2 != k);
                        temp1 += path[loop][0], temp2 += path[loop][1]) {
                            rem[remnum][0] = temp1;
                            rem[remnum][1] = temp2;
                            remnum++;
                        }
                        break;
                    }
                }
            }
            if (remnum > 0) {
                for (loop = 0; loop < remnum; loop++) {
                    j = rem[loop][0];
                    k = rem[loop][1];
                    grid[j][k] = -grid[j][k];
                }
                tempres = white();
                tag = 1;
                if (tempres < res) res = tempres;
                for (loop = 0; loop < remnum; loop++) {
                    j = rem[loop][0];
                    k = rem[loop][1];
                    grid[j][k] = -grid[j][k];
                }
            }
            grid[u][v] = 0;
            enable[i] = 1;
        }
    if (tag == 0) return cal();
    else return res;
}

int main() {
    char ch, str[100];
    int i, j;
    while (1) {
        scanf("%s", str);
        if (strcmp(str, "EndOfInput") == 0) break;
        for (i = 0; i < 8; i++) {
            if (str[i] == 'w') grid[0][i] = 1;
            else if (str[i] == 'b') grid[0][i] = -1;
            else if (str[i] == 'e') grid[0][i] = 0;
        }
        for (i = 1; i < 8; i++) {
            scanf("%s", str);
            for (j = 0; j < 8; j++) {
                ch = str[j];
                if (ch == 'w') grid[i][j] = 1;
                else if (ch == 'b') grid[i][j] = -1;
                else if (ch == 'e') grid[i][j] = 0;
            }
        }
        
        //for (int i = 0; i < 8; i++) {
        //    for (int j = 0; j < 8; j++) {
        //        printf("%d ", grid[i][j]);
        //    }
        //    printf("\n");
        //}
            
        num = 0;
        for (i = 0; i < 8; i++)
            for (j = 0; j < 8; j++)
                if (grid[i][j] == 0) {
                    empty[num][0] = i;
                    empty[num][1] = j;
                    enable[num] = 1;
                    num++;
                }
        scanf("%s", str);
        if (strcmp(str, "White") == 0) {
            i = white();
            if (i == 1) printf("White\n");
            else if (i == 0) printf("Draw\n");
            else if (i == -1) printf("Black\n");
            else assert(0);
        } else if (strcmp(str, "Black") == 0) {
            i = black();
            if (i == 1) printf("White\n");
            else if (i == 0) printf("Draw\n");
            else if (i == -1) printf("Black\n");
            else assert(0);
        } else assert(0);
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值