[博弈论]Mine

Mine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1769    Accepted Submission(s): 513

 

Problem Description

Have you ever played a game in Windows: Mine?
This game is played on a n*m board, just like the Pic(1)


On the board, Under some grids there are mines (represent by a red flag). There are numbers ‘A(i,j)’ on some grids means there’re A(i,j) mines on the 8 grids which shares a corner or a line with gird(i,j). Some grids are blank means there’re no mines on the 8 grids which shares a corner or a line with them.
At the beginning, all grids are back upward.
In each turn, Player should choose a back upward grid to click.
If he clicks a mine, Game over.
If he clicks a grid with a number on it , the grid turns over.
If he clicks a blank grid, the grid turns over, then check grids in its 8 directions.If the new grid is a blank gird or a grid with a number,it will be clicked too.
So If we click the grid with a red point in Pic(1), grids in the area be encompassed with green line will turn over.
Now Xiemao and Fanglaoshi invent a new mode of playing Mine. They have found out coordinates of all grids with mine in a game. They also find that in a game there is no grid will turn over twice when click 2 different connected components.(In the Pic(2), grid at (1,1) will turn over twice when player clicks (0,0) and (2,2) , test data will not contain these cases).
Then, starting from Xiemao, they click the grid in turns. They both use the best strategy. Both of them will not click any grids with mine, and the one who have no grid to click is the loser.
Now give you the size of board N, M, number of mines K, and positions of every mine Xi,Yi. Please output who will win.

Input

Multicase
The first line of the date is an integer T, which is the number of the text cases. (T<=50)
Then T cases follow, each case starts with 3 integers N, M, K indicates the size of the board and the number of mines.Then goes K lines, the ith line with 2 integer Xi,Yi means the position of the ith mine.
1<=N,M<=1000 0<=K<=N*M 0<=Xi  

Output

For each case, first you should print "Case #x: ", where x indicates the case number between 1 and T . Then output the winner of the game, either ”Xiemao” or “Fanglaoshi”. (without quotes)

Sample Input

2 3 3 0 3 3 1 1 1

Sample Output

Case #1: Xiemao Case #2: Fanglaoshi

Source

2013 Multi-University Training Contest 8

#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <memory.h>
#include <queue>

using namespace std;

int board[1010][1010];
int dir[][2] = {{-1,-1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

int clickNum(int x, int y, int sizeX, int sizeY) {
    int i;
    int sum;
    int tmpX, tmpY;

    queue<pair<int,int> >q;
    q.push(make_pair(x,y));

    board[x][y] = -2;

    sum = 1;
    while (q.empty() == false) {
        pair<int,int> tmp = q.front();
        q.pop();

        x = tmp.first;
        y = tmp.second;

        for (i = 0; i < 8; ++i) {
            tmpX = x + dir[i][0];
            tmpY = y + dir[i][1];

            if (tmpX < 0 || tmpY < 0 || tmpX >= sizeX || tmpY >= sizeY) {
                continue;
            }

            if (board[tmpX][tmpY] > 0) {
                ++sum;
                board[tmpX][tmpY] = 0 - board[tmpX][tmpY];
                continue;
            }

            if (board[tmpX][tmpY] == 0) {
                board[tmpX][tmpY] = -2;
                q.push(make_pair(tmpX, tmpY));
            }
        }
    }
    
    return sum;
}

int main()
{
    int i, j, k, num;
    int sizeX, sizeY;
    int mineX, mineY;
    int mine;

    int tmpX, tmpY;
    int tmpSum;
    int sum = 0;

    scanf("%d", &num);
    for (i = 0; i < num; ++i) {
        sum = 0;
        memset(board, 0, sizeof(board));
        scanf("%d %d %d", &sizeX, &sizeY, &mine);

        for (j = 0; j < mine; ++j) {
            scanf("%d %d", &mineX, &mineY);
            board[mineX][mineY] = -1;
            for (k = 0; k < 8; ++k) {
                tmpX = mineX + dir[k][0];
                tmpY = mineY + dir[k][1];

                if (tmpX < 0 || tmpY < 0 || tmpX >= sizeX || tmpY >= sizeY) {
                    continue;
                }

                if (board[tmpX][tmpY] >= 0) {
                    ++board[tmpX][tmpY];
                }
            }
        }

        for (j = 0; j < sizeX; ++j) {
            for (k = 0; k < sizeY; ++k) {
                if (board[j][k] < 0) {
                    continue;
                }

                if (board[j][k] == 0) {
                    tmpSum = clickNum(j, k, sizeX, sizeY);
                    if ((tmpSum & 1) == 1) {
                        tmpSum = 1;
                    } else {
                        tmpSum = 2;
                    }
                    sum ^= tmpSum;
                }
            }
        }

        for (j = 0; j < sizeX; ++j) {
            for (k = 0; k < sizeY; ++k) {
                if (board[j][k] > 0) {
                    sum ^= 1;
                    board[j][k] = -board[j][k];
                }
            }
        }

        if (sum == 0) {
            printf("Case #%d: Fanglaoshi\n", i + 1);
        } else {
            printf("Case #%d: Xiemao\n", i + 1);
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/micklongen/article/details/90049703