poj 2697 A Board Game-dfs

本文介绍了一个名为S-Dao的棋盘游戏及其算法解决方案。游戏目标是在最少的步数内将棋盘从初始状态变为指定的最终状态。文章详细解释了游戏规则,并提供了一段使用深度优先搜索(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;
}

WPF 国际象棋 棋子ChessProgrammingTest.zip 要求: You have been provided with a third-party library "ChessLib" which calculates the legal moves a knight can make given a position on an 8 by 8 board. The library has been used to create a program which moves a knight randomly around a board, given an initial starting position and a total number of moves to make. Problem: ======== Extend this program to set up an 8 by 8 square game board containing several different pieces in predefined positions. For each move of the game, the program will choose a piece at random, and move it to a randomly selected valid position. You are not allowed to change any of the ChessLib code. Extend the program as required. Use Object Oriented Design and Modeling appropriately for extensibility. Please supply all the code for your solution in the file Answer.cs in the SampleProgram project. Please supply all the tests for your solution in the file TestAnswer.cs in the SampleProgram.Test project. Game Rules: ----------- * Only one piece can occupy any position on the board at a given time. * All pieces can “jump” any occupied position. Note: Although the game bears a striking resemblance to Chess, this is entirely coincidental. Do not assume other chess rules apply. Game Pieces to support: ----------------------- * Knight – Moves as implemented by ChessLib * Bishop - Moves diagonally, any distance within board boundaries * Queen – Moves diagonally, horizontally or vertically, any distance within board boundaries
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值