POJ 2697 - A Board Game

版权声明:Designed by Sureina 2016©Copyright.All Right Reserved https://blog.csdn.net/Sureina/article/details/52356135

Description

Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at 1999. A variation of it, called S-Dao, is a one-player game. In S-Dao, the game board is a 4 * 4 square with 16 cells. There are 4 black stones and 4 white stones placed on the game board randomly in the beginning. The player is given a final position and asked to play the game using the following rules such that the final position is reached using the minimum number of moves:
  • 1. You first move a white stone, and then a black stone. You then alternatively move a white stone and a black stone.
    2. A stone can be moved horizontally, vertically or diagonally. A stone must be moved in a direction until the boarder or another stone is encountered. There is no capture or jump.
    3. During each move, you need to move a stone of the right color. You cannot pass.

An example of a sequence of legal moves is shown in the following figure. This move sequence takes 4 moves. This is not a sequence of legal moves

using the least number of moves assume the leftmost board is the initial position and the rightmost board is the final position. A sequence of moves using only 3 moves is shown below.

Given an initial position and a final position, your task is to report the minimum number of moves from the initial position to the final position.


Input

The first line contains the number of test cases w, w <= 6. Then the w test cases are listed one by one. Each test case consists of 8 lines, 4 characters per line. The first 4 lines are the initial board position. The remaining 4 lines are the final board position. The i-th line of a board is the board at the i-th row. A character 'b' means a black stone, a character 'w' means a white stone, and a '*' means an empty cell.

Output

For each test case, output the minimum number of moves in one line. If it is impossible to move from the initial position to the final position, then output -1.

Sample Input

2
w**b
*wb*
*bw*
b**w
w**b
*wb*
*bw*
bw**
w**b
*b**
**b*
bwww
w**b
*bb*
****
bwww


Sample Output

1
3


题意:在一个4*4的棋盘中,有白色和黑色棋子【w代表白色,b代表黑色】。给出一个开始的情况,再给出一个结束的情况,每次先移动白棋,然后黑棋,每次移动能够向8个方向中的其中一个移动到棋盘边界或者碰到其他棋子。求出达到结束情况最少需要多少步。

直接使用深搜即可,注意将每次移动到底。

#include <cstdio>
#include <cstring>

int dp[8][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};
char turn[2] = {'w', 'b'};
char start[20], result[20];

inline bool confirm(int x, int y)
{
    return (x >= 0 && y >= 0 && x <= 3 && y <= 3);
}

bool DFS(char maps[], int step, int n, int sym)
{
    if (step == n)
    {
        if (strcmp(maps, result) == 0)
            return true;
        return false;
    }

    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            if (maps[i*4 + j] != turn[sym])
                continue;
            for (int k = 0; k < 8; ++k)
            {
                int x = i;
                int y = j;
                while (confirm(x + dp[k][0], y + dp[k][1]) == true &&
                       maps[(x + dp[k][0])*4 + y + dp[k][1]] == '*')
                {
                    x += dp[k][0];
                    y += dp[k][1];
                }
                maps[x*4+y] = turn[sym];
                maps[i*4+j] = '*';

                if (DFS(maps, step+1, n, 1-sym) == true)
                    return true;
                maps[x*4+y] = '*';
                maps[i*4+j] = turn[sym];
            }
        }
    }
    return false;
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        memset(start, 0, sizeof(start));
        memset(result, 0, sizeof(result));
        for (int i = 0; i < 4; ++i)
            scanf("%s", start + 4*i);
        for (int i = 0; i < 4; ++i)
            scanf("%s", result + 4*i);
        int ans = 0;
        while (true)
        {
            if (DFS(start, 0, ans, 0) == true)
            {
                printf("%d\n", ans);
                break;
            }
            ans++;
        }
    }
    return 0;
}


阅读更多
换一批

Board Game

12-28

DescriptionnnAlice and Bob started to play the following game: they have an m×n chessboard, with some of the fields removed. There are two chess pieces on distinct (non-removed) fields of the board. Alice always makes the first move and then she alternates with Bob in turns. Each turn consists of moving one of the pieces by one field horizontally or vertically. Both players can move any of the pieces, regardless of the piece moved in the previous turn. The piece cannot be moved to a removed field. The player that is able to move one of the pieces to the field occupied by the other one, thus capturing it, wins. nnAfter some time, they found the game very boring -- nobody could win, and the pieces just chased each other around the board. Therefore, they introduced a new rule -- no player may move a piece in such a way that a position that already appeared during the game is repeated. The position is considered to be the same if the fields occupied by the pieces are the same (the pieces cannot be distinguished), regardless of who is on turn in the particular position. Additionally, they introduced a rule that the player who cannot make a legal move loses. Now the game is always finite and one of the players will surely win. Your goal is to find a winning strategy, if one exists. nnInputnnThe input consists of several instances, separated by single empty lines. nnThe first line of each instance consists of two integers m and n, 1 m, n 8. Each of m following lines consists of n characters and determines the initial state of the chessboard. The characters are one of the following:n"." for an empty field of the chessboardn"#" for a removed field of the chessboardn"P" for the field of the chessboard where one of the pieces startsnThere are always precisely two characters "P" in each instance.nOutputnnThe output for each instance consists of a single line containing either the string "Alice wins." if Alice has a winning strategy in the described position, or the string "Bob wins.", if she has no such strategy.nSample Inputnn4 4nP.##n..##n##..n##.Pnn1 5nP...PnSample OutputnnAlice wins.nBob wins.

没有更多推荐了,返回首页