hdu 1401 Solitaire(bfs)

Solitaire

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3547    Accepted Submission(s): 1093


Problem Description
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively.

There are four identical pieces on the board. In one move it is allowed to:

> move a piece to an empty neighboring field (up, down, left or right),

> jump over one neighboring piece to an empty field (up, down, left or right).



There are 4 moves allowed for each piece in the configuration shown above. As an example let's consider a piece placed in the row 4, column 4. It can be moved one row up, two rows down, one column left or two columns right.

Write a program that:

> reads two chessboard configurations from the standard input,

> verifies whether the second one is reachable from the first one in at most 8 moves,

> writes the result to the standard output.
 

Input
Each of two input lines contains 8 integers a1, a2, ..., a8 separated by single spaces and describes one configuration of pieces on the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position of one piece - the row number and the column number respectively. Process to the end of file.
 

Output
The output should contain one word for each test case - YES if a configuration described in the second input line is reachable from the configuration described in the first input line in at most 8 moves, or one word NO otherwise.
 

Sample Input
  
  
4 4 4 5 5 4 6 5 2 4 3 3 3 6 4 6
 

Sample Output
  
  
YES
 

Source
 题目分析:
拓展到了多个棋子的bfs,稍微减一下枝,就能裸奔过去
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
bool used[8][8][8][8][8][8][8][8];
bool mp[10][10];

struct Node
{
    int x[4],y[4],t;
}e,u;

bool check ( const Node& a )
{
    for ( int i = 0 ; i < 4 ; i++ )
        if ( a.x[i] < 0 || a.x[i] > 7 || a.y[i] < 0 || a.y[i] > 7 ) return true;
    return used[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]];
}

void set ( const Node& a )
{
    used[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]] = true;
}

bool hasChess ( const Node& a , int k )
{
    for ( int i = 0 ; i < 4 ; i++ )
    {
        if ( i == k ) continue;
        if ( a.x[i] == a.x[k] && a.y[i] == a.y[k] ) return true;
    }
    return false;
}

bool judge ( const Node& a )
{
    for ( int i = 0 ; i < 4 ; i++ )
        if ( !mp[a.x[i]][a.y[i]] ) return false;
    return true;
}

bool bfs ( )
{
    u.t = 0;
    memset ( used , 0 , sizeof ( used ) );
    for ( int i = 1 ; i < 4 ; i++ )
    {
        scanf ( "%d%d" , &u.x[i] , &u.y[i] );
        u.x[i]-- , u.y[i]--;
    }
    for ( int i = 0 ; i < 4 ; i++ )
    {
        scanf ( "%d%d" , &e.x[i] , &e.y[i] );
        e.x[i]-- , e.y[i]--;
        mp[e.x[i]][e.y[i]] = true; 
    }
    set ( u );
    queue<Node> q;
    q.push ( u );
    while ( !q.empty() )
    {
        u = q.front();
        q.pop();
        if ( judge ( u ) ) return true;
        if ( u.t >= 8 ) return false;
        for ( int i = 0 ; i < 4 ; i++ )
            for ( int j = 0 ; j < 4 ; j++ )
            {
                Node a = u;
                a.x[i] += dx[j];
                a.y[i] += dy[j];
                a.t++;
                if ( check ( a ) ) continue;
                if ( hasChess ( a , i ) )
                {
                    Node b = a;
                    b.x[i] += dx[j];
                    b.y[i] += dy[j];
                    if ( !check ( b ) && !hasChess( b , i ) )
                    {
                        if ( judge ( b ) ) return true;
                        q.push ( b );
                        set ( b );
                    }    
                }
                else
                {
                    if ( judge ( a ) ) return true;
                    q.push ( a );
                    set ( a );
                }
            }
    }
}


int main ( )
{
    while ( ~scanf ( "%d%d" , &u.x[0] , &u.y[0]  ) )
    {
        u.x[0] -- , u.y[0] --;
        memset ( mp , 0 , sizeof ( mp ) );
        if ( bfs ( ) ) puts ("YES" );
        else puts ( "NO" );
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值