poj 2697 A Board Game-dfs

原创 2016年08月30日 18:12:26
A Board Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 518   Accepted: 353

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的棋盘,初始4个黑棋子4个白棋子,摆放位置随机。问最小多少步能达到最终状态。移动棋子有以下规则:

1、先移动白棋子再移动黑棋子再白黑交替移动

2、8个方位移动,每次移动到碰到边界线或者是有别的棋子挡住为止

输入初始状态,最终状态,输出最小步数


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char start[5][5],result[5][5];
int mmove[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
char bw[2]={'w','b'};

bool success()
{
    int i,j;
    for(i=0;i<4;++i)
    {
        for(j=0;j<4;++j)
            if(start[i][j]!=result[i][j])
            {
                return false;
            }
    }
    return true;
}
bool ok(int x,int y)
{
    if(x>=0&&x<4 && y>=0 &&y<4) return true;
    else return false;
}

bool dfs(int step,int n,int num)//当前步数,答案,当前移动哪个颜色
{
    if(step==n)
    {
        if(success()) return true;
        else return false;
    }
    int i ,j;
    for(i=0;i<4;++i) //每次从当前状态的棋盘中的第一个格子找棋子
    {
        for(j=0;j<4;++j)
        {
            if(start[i][j]!=bw[num]) continue;
            for(int k=0;k<8;++k)
            {
                int x=i,y=j;
                while(ok(x+mmove[k][0],y+mmove[k][1]) && start[(x+mmove[k][0])][(y+mmove[k][1])]=='*')
                {
                    x+=mmove[k][0];
                    y+=mmove[k][1];
                }

                if(x==i && y==j) continue ;//向这个方位走不行

                start[x][y]=bw[num];
                start[i][j]='*';

                if(dfs(step+1,n,1-num)) {return true;}

                start[i][j]=bw[num];
                start[x][y]='*';
            }
        }
    }
    return false;//达不到n步的话
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {

        int i;
        for(i=0;i<4;++i)
            scanf("%s",start[i]);
        for(i=0;i<4;++i)
            scanf("%s",result[i]);
        int ans=0;//0步能到吗,1步能到吗,2步能到吗……
        while(1)
        {
            if(dfs(0,ans,0))
            {
                printf("%d\n",ans);
                break;
            }
           ans++;
        }
    }
    return 0;
}

相关文章推荐

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 (拓扑排序+dfs)

题目链接:http://poj.org/problem?id=1691 题目大意:给你n个小矩形,可以一个大矩形。给这n个小矩形涂色,涂色的要求是:只有位于它上方的并且和它邻接的小矩形涂完了色,它才能...

POJ 1691 Painting A Board (DFS/状态DP)

题意:给定一个矩形块,它里面包含了n个小矩形块,现在要求将每个小矩形块涂上给定的颜色。涂色过程中,小矩形必须是整块整块的涂,并且只有当某个矩形上面的所有矩形都被涂色之后它才能被涂色。假如每只笔只能涂一...
  • Tsaid
  • Tsaid
  • 2011年10月03日 13:48
  • 1266

[poj 1691] Painting A Board dfs+拓扑排序

Painting A Board Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3611 Accepted...
  • ALPS233
  • ALPS233
  • 2016年04月08日 08:51
  • 730

POJ 1753 Flip Game【翻转棋盘+枚举+dfs】

方法:一、枚举(此处所用)           二、用二进制记录下标(尚未实现)          三、类比于玩魔方游戏(思路来自黄超,尚未实现) 原题链接:http://poj.org/proble...

(枚举,dfs)Flip Game poj 1753

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square...
  • TchChan
  • TchChan
  • 2017年05月01日 18:57
  • 96

POJ 1753 Flip Game(枚举+dfs)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40052   Accepted: 17381...

POJ 1753-Flip Game(枚举&&DFS)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38472   Accepted: 1672...
  • MIKASA3
  • MIKASA3
  • 2016年04月26日 20:46
  • 299
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 2697 A Board Game-dfs
举报原因:
原因补充:

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