poj 2697 A Board Game-dfs

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

阅读更多
文章标签: dfs 代码
个人分类: dfs
上一篇poj3723Conscription-最小生成树Kruskal
下一篇poj 2406 Power Strings-字符串(kmp)
想对作者说点什么? 我来说一句

poj经典数据结构题目解题报告

2008年03月17日 314KB 下载

poj1087贪心算法实验报告

2010年12月25日 41KB 下载

POJ 1002 487-3279 测试数据 完整

2016年08月13日 5.26MB 下载

简易数字示波器设计外文资料

2011年07月23日 335KB 下载

本人整理的POJ解题报告大全

2008年10月11日 18.93MB 下载

POJ+HDU+ZOJ ACM题解

2011年11月03日 839KB 下载

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

关闭
关闭