独轮车(广搜)noj

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<queue>
#define true 1
#define false 0
using namespace std;

struct node
{
    int x;
    int y;
    int color;
    int dire;
};
node start,target;
queue<node>q1;

int sx,sy;
char sc,sd;
int ex,ey;
char ec;
char maze[20][20];

int used[20][20][5][4];
int time[20][20][5][4];

int walk[4][2]={//注意要与下面的所设定的四个方向的数字值对应一致
    0,1,    //E-东
    1,0,    //S-南
    0,-1,   //W-西
    -1,0    //N-北
};//注意电脑显示屏上的坐标与数学上坐标的区别!!!
void input();
void init();
int colorToInt(char sc);
int direToInt(char sd);
int bfs();
node moveto(node now,int i);
int effective(node next);
int isTarget(node next);

int main()
{
    input();
    init();
    cout<<bfs()<<endl;
    return 0;
}
void input()
{
    cin>>sx>>sy>>sc>>sd;
    cin.get();
    cin>>ex>>ey>>ec;
    cin.get();
    for(int i=0; i<20; i++){
        for(int j=0; j<20; j++){
            maze[i][j]=cin.get();
          // cin>>maze[i][j];
        }
        cin.get();
    }
}
void init()
{
    start.x=sx-1;
    start.y=sy-1;
    start.color=colorToInt(sc);
    start.dire=direToInt(sd);
    used[start.x][start.y][start.color][start.dire]=1;
    q1.push(start);
    target.x=ex-1;
    target.y=ey-1;
    target.color=colorToInt(ec);
}
int colorToInt(char sc)
{
    switch(sc)
    {
        case 'R':return 0;
        case 'Y':return 1;
        case 'B':return 2;
        case 'W':return 3;
        case 'G':return 4;
        default:return -1;
    }
}
int direToInt(char sd)
{
    switch(sd)//注意要与上面的walk[][]一致
    {
        case 'E':return 0;
        case 'S':return 1;
        case 'W':return 2;
        case 'N':return 3;
        default:return -1;
    }
}
int bfs()
{
    node now,next;
    while(!q1.empty()){
        now=q1.front();
        q1.pop();
        for(int i=0;i<3;i++){
            next=moveto(now,i);
            if(effective(next)){
                used[next.x][next.y][next.color][next.dire]=1;
                time[next.x][next.y][next.color][next.dire]=time[now.x][now.y][now.color][now.dire]+1;
                if(isTarget(next))
                    return time[next.x][next.y][next.color][next.dire];
                else q1.push(next);
            }
        }
    }
    return -1}
node moveto(node now,int i)
{
    node next;
    if(i==0){
        next.x=now.x+walk[now.dire][0];
        next.y=now.y+walk[now.dire][1];
        next.color=(now.color+1)%5;
        next.dire=now.dire;
    }
    else{
        next.x=now.x;
        next.y=now.y;
        next.color=now.color;
        if(i==1)//左转
            next.dire=(now.dire+3)%4;//注意dire和color的设定是有顺序的,左转相当于顺时针也即右转3次
        else
            next.dire=(now.dire+1)%4;
    }
    return next;
}
int effective(node next)
{
    if(next.x>=0&&next.x<20&&next.y>=0&&next.y<20){
        if(maze[next.x][next.y]!='X'){
            if(!used[next.x][next.y][next.color][next.dire]){
                return true;
            }
        }
    }
    return false;
}
int isTarget(node next)
{
    if(next.x==target.x&&next.y==target.y&&next.color==target.color){
        return true;
    }
    else return false;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DrmBee#

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值