POJ 2697 - A Board Game

原创 2016年08月29日 20:11:36

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;
}


版权声明:Designed by Sureina 2016©Copyright.All Right Reserved

相关文章推荐

poj 2697 A Board Game-dfs

Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk at 1999. A var...
  • ly59782
  • ly59782
  • 2016年08月30日 18:12
  • 199

POJ 2697 字典树 + bfs

这题一点都不难啊 ,不知道为什么这么少人过 像这种题,一看就是BFS,然后需要记录状态是否被访问过 要么你就hash  要么你就塞到字典树里 状态很少 , 也就C(16, 4) * C(12, ...

fzuoj 2143 Board Game

题目请戳这里 思路: 最小费用最大流,代价拆开变成A[i][j] ^2 - A[i][j] * B[i][j] * 2 + B[i][j] ^ 2,假设给定的数组是B,那么B[i][j] ^ 2 可...

CodeForces 605D Board Game(树状数组)

分析:找所有比(a,b)(a,b)小的数,可以先以找第一个数aa比小的所有集合中,第二个数比bb小的集合,肯定不能两次sortsort,可以用线段树+setset来搞定这件事情,线段树的坐标为aa的值...

Board clipper game

  • 2008年12月02日 16:17
  • 901KB
  • 下载

【poj 1691】Painting A Board 题意&题解&代码(C++)

poj dfs
  • DERITt
  • DERITt
  • 2016年04月11日 14:30
  • 317

poj 1691 Painting A Board(dfs,拓扑排序)

http://poj.org/problem?id=1691 大致题意:给出n个矩形,其参数有左上角顶点坐标,右下角顶点坐标以及该矩形所涂颜色。规定是涂当前矩形当且仅当它上面的矩形都已经被涂了色。...

POJ1691 Painting A Board ACM解题报告(DFS+构造)难题=。=

大致题意: 墙上有一面黑板,现划分为多个矩形,每个矩形都要涂上一种预设颜色C。 由于涂色时,颜料会向下流,为了避免处于下方的矩形的颜色与上方流下来的颜料发生混合,要求在对矩形i着色时,处于矩形i上方直...

[POJ 1691]Painting A Board[DFS][排序]

题目链接:[POJ 1691]Painting A Board[DFS][排序] 题意分析: 正方形中的块需要被染色,当且仅当该块上方相邻的所有块都被染色时,可以染色。色块可以连续染色,只要你拿着...

poj 1691 Painting A Board

参考:http://blog.csdn.net/tsaid/article/details/6841823
  • ctxdecs
  • ctxdecs
  • 2014年04月23日 16:24
  • 582
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 2697 - A Board Game
举报原因:
原因补充:

(最多只允许输入30个字)