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

• 本文已收录于以下专栏：

## POJ 2697 A Board Game

• work_freedom
• 2012年12月10日 13:31
• 295

## POJ 2697 - A Board Game

Description Dao was a simple two-player board game designed by Jeff Pickering and Ben van Buskirk a...
• Sureina
• 2016年08月29日 20:11
• 345

## poj1691 Painting A Board

• u012914220
• 2014年07月17日 10:15
• 688

## POJ 2697 A Board Game （bfs模拟）

• 2013年09月29日 21:31
• 1182

## 419. Battleships in a Board（计算甲板上的军舰数）

Given an 2D board, count how many different battleships are in it. The battleships are represented w...
• xiangwanpeng
• 2016年11月04日 13:23
• 891

## LeetCode 419. Battleships in a Board 解题报告

LeetCode 419. Battleships in a Board 解题报告
• camellhf
• 2016年10月20日 12:16
• 3750

## 【poj 1691】Painting A Board

DescriptionThe CE digital company has built an Automatic Painting Machine (APM) to paint a flat boar...
• WilliamCode
• 2016年04月11日 15:38
• 284

## u-boot分析之Makefile结构分析---编译

• ljcome
• 2017年02月17日 23:29
• 163

## 【POJ2286】The Rotation Game

The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 606...
• qq_35479641
• 2016年07月22日 19:42
• 308

## POJ 3801 Crazy Circuits 有源汇的上下界最小流

• Dinivity123
• 2014年01月20日 14:59
• 1083

举报原因： 您举报文章：poj 2697 A Board Game-dfs 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)