hdu 1401 Solitaire (单/双bfs)

题目链接Solitaire
大意:
一个8*8的棋盘上有四个棋子,给出始末状态下四个棋子坐标,问能不能在8步内从初始状态变成末状态
1、单bfs,开一个8维数组,将整个棋盘上的四个棋子作为整体,然后一般的bfs就可以了,主要是下面的双向bfs
单bfs代码

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

bool vis[8][8][8][8][8][8][8][8];
bool maps[10][10];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

struct node
{
    int x[4];
    int y[4];
    int step;
};

//判断最后是否为最终状态
int check(node now)
{
    for(int i = 0; i < 4; i++){
        if(!maps[now.x[i]][now.y[i]]){
            return 0;
        }
    }
    return 1;
}

//判断要去的那一个格是否为空
int if_empty(node now,int k)
{
    for(int i = 0; i < 4; i++){
        if(i != k&&now.x[i] == now.x[k]&&now.y[i] == now.y[k]){
            return 0;
        }
    }
    return 1;
}

//判断是否符合要求
int judge(node now)
{
    for(int i = 0; i < 4; i++){
        if(now.x[i] < 0||now.y[i] < 0||now.x[i] >= 8||now.y[i] >= 8){
            return 1;
        }
    }
    if(vis[now.x[0]][now.y[0]][now.x[1]][now.y[1]][now.x[2]][now.y[2]][now.x[3]][now.y[3]]){
        return 1;
    }
    return 0;
}

int bfs(node b)
{
    memset(vis,0,sizeof(vis));
    queue <node> q;
    q.push(b);
    vis[b.x[0]][b.y[0]][b.x[1]][b.y[1]][b.x[2]][b.y[2]][b.x[3]][b.y[3]] = true;
    while(!q.empty()){
        node now = q.front();
        q.pop();
        //这里因为后面会有剪枝,需要包括第8步,不然会mle;
        if(now.step >= 8){
            return 0;
        }
        if(check(now)){
            return 1;
        }
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                node next = now;
                next.x[i] += dir[j][0];
                next.y[i] += dir[j][1];
                next.step++;
                if(judge(next)){
                    continue;
                }
                if(if_empty(next,i)){
                    //剪枝,减少内存消耗,让进入队列的都是至少差一步到最终态的,对应上面>=8的判断条件
                    if(check(next)){
                        return 1;
                    }
                    vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = true;
                    q.push(next);
                }
                //非空则继续向前
                else{
                    next.x[i] += dir[j][0];
                    next.y[i] += dir[j][1];
                    //也有要满足往前一个不越界且还是空格子
                    if(judge(next)||!if_empty(next,i)){
                        continue;
                    }
                    //剪枝,同上
                    if(check(next)){
                        return 1;
                    }
                    vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = true;
                    q.push(next);
                }
            }
        }
    }
    return 0;

}

int main()
{
    node b;
    while(cin>>b.x[0]>>b.y[0]>>b.x[1]>>b.y[1]){
       cin>>b.x[2]>>b.y[2]>>b.x[3]>>b.y[3];
        memset(maps,false,sizeof(maps));
        for(int i = 0; i < 4; i++){
            int x,y;
            cin>>x>>y;
            x--,y--;
            maps[x][y] = true;
        }
        for(int i = 0; i < 4; i++){
            b.x[i]--,b.y[i]--;
        }
        b.step = 0;
        int flag = bfs(b);
        if(flag){
            cout<<"YES\n";
        }
        else{
            cout<<"NO\n";
        }
    }
    return 0;
}

2、双向bfs
这里有一个我自己最开始写的,感觉思路上是双向bfs,但是感觉实现过程有点假,没过。。也没看出来哪错了,如果有大佬有时间希望帮忙指正

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

char vis[8][8][8][8][8][8][8][8];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}},flag;
bool maps[10][10];

struct node
{
    int x[4];
    int y[4];
    int step;
};
node b,e;

//判断最后是否为最终状态
int check(node now,int mark)
{
    if(mark == 2){
        if(vis[now.x[0]][now.y[0]][now.x[1]][now.y[1]][now.x[2]][now.y[2]][now.x[3]][now.y[3]] == 3){
            return 1;
        }
        for(int i = 0; i < 4; i++){
            if(now.x[i] != b.x[i]||now.y[i] != b.y[i]){
                return 0;
            }
        }
        return 1;
    }
    else{
        for(int i = 0; i < 4; i++){
            if(!maps[now.x[i]][now.y[i]]){
                return 0;
            }
        }
        flag = 1;
        return 1;
    }
}

//判断要去的那一个格是否为空
int if_empty(node now,int k)
{
    for(int i = 0; i < 4; i++){
        if(i != k&&now.x[i] == now.x[k]&&now.y[i] == now.y[k]){
            return 0;
        }
    }
    return 1;
}

//判断是否符合要求
int judge(node now,int mark)
{
    for(int i = 0; i < 4; i++){
        if(now.x[i] < 0||now.y[i] < 0||now.x[i] >= 8||now.y[i] >= 8){
            return 1;
        }
    }
    if(vis[now.x[0]][now.y[0]][now.x[1]][now.y[1]][now.x[2]][now.y[2]][now.x[3]][now.y[3]] == mark){
        return 1;
    }
    return 0;
}

void bfs(node &a,int mark)
{
    queue <node> q;
    q.push(a);
    vis[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]] += mark;
    while(!q.empty()){
        node now = q.front();
        q.pop();
        //bfs如果超过4步就不走了
        if(now.step >= 4){
            continue;
        }
        if(check(now,mark)){
            cout<<"YES"<<endl;
            return ;
        }
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                node next = now;
                next.x[i] += dir[j][0];
                next.y[i] += dir[j][1];
                next.step++;
                if(judge(next,mark)){
                    continue;
                }
                if(if_empty(next,i)){
                    if((check(now,mark))){
                        cout<<"YES"<<endl;
                        return ;
                    }
                    vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] += mark;
                    if((check(now,mark))){
                        cout<<"YES"<<endl;
                        return ;
                    }
                    q.push(next);
                }
                //非空则继续向前
                else{
                    next.x[i] += dir[j][0];
                    next.y[i] += dir[j][1];
                    //也有要满足往前一个不越界且还是空格子
                    if(judge(next,mark)||!if_empty(next,i)){
                        continue;
                    }
                    //剪枝,同上
                    if(check(now,mark)){
                        cout<<"YES"<<endl;
                        return ;
                    }
                    vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] += mark;
                    if((check(now,mark))){
                        cout<<"YES"<<endl;
                        return ;
                    }
                    q.push(next);
                }
            }
        }
    }
    if(mark == 2){
        cout<<"NO"<<endl;
    }
    return ;

}

int main()
{
    while(cin>>b.x[0]>>b.y[0]>>b.x[1]>>b.y[1]){
       cin>>b.x[2]>>b.y[2]>>b.x[3]>>b.y[3];
        memset(vis,0,sizeof(vis));
        memset(maps,false,sizeof(maps));
        for(int i = 0; i < 4; i++){
            cin>>e.x[i]>>e.y[i];
            e.x[i]--,e.y[i]--;
            maps[e.x[i]][e.y[i]] = true;
        }
        for(int i = 0; i < 4; i++){
            b.x[i]--,b.y[i]--;
        }
        b.step = 0;
        e.step = 0;
        flag = 0;
        bfs(b,1);
        if(!flag){
            bfs(e,2);
        }
    }
    return 0;
}




至于正经和能ac的代码,我还得研究一下。。先到这,学会了再更(doge)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值