独轮车问题

#include<iostream>
#include<queue>
using namespace std;
queue<int>q1;
queue<int>q2;
queue<int>q3;
queue<int>q4;
int step[21][21][4][5]={0};//车轮方向,0=E,1=S,2=W,3=N; 颜色;
int dr[4]={0,1,0,-1};//东南西北;
int dc[4]={1,0,-1,0};
int sx,sy,tx,ty,di;
char d[4]={'E','S','W','N'};
char colour[5]={'R','Y','B','W','G'};
char scolor,dire,tcolor;
char mig[21][21]={0};
void readdate();
void init();
int fbs();
int canmoveto(int x,int y,int z,int dire);
int moveto1(int x,int d,int dire);
int moveto2(int y,int d,int dire);
int moveto(int z,int dire);
int main()
{
    int sum;
    readdate();//读入数据;
    init();//初始化;
    sum=fbs();
    cout<<sum<<endl;
    return 0;
}

void readdate()
{
    int i,j;  
    cin>>sx>>sy;//起始坐标;
    cin>>scolor>>dire;//起始颜色,方向;
    cin>>tx>>ty;//结束;
    cin>>tcolor;
    for(i=0;i<5;i++)
    {
        if(scolor==colour[i])         //   将颜色和方向用数字表示;
        {
            scolor=i;
        }
    }
    for(i=0;i<5;i++)
    {
        if(tcolor==colour[i])
        {
            tcolor=i;
        }
    }
    if(dire=='E')
    {
        di=0;
    }
    else if(dire=='S')
    {
        di=1;
    }
    else if(dire=='W')
    {
        di=2;
    }
    else
    {
        di=3;
    }
    for(i=1;i<=20;i++)//输入地图;
    {
        for(j=1;j<=20;j++)
        {
            cin>>mig[i][j];
        }
    }
}

void init()
{
    q1.push(sx);
    q2.push(sy);
    q3.push(di);//方向;
    q4.push(scolor);//颜色;
    step[sx][sy][di][scolor]=0;
}

int fbs()
{
    int i,u1,u2,v1=0,v2=0,d1,d2,step2,step3;
    int t;
    char colour[5]={'R','Y','B','W','G'};
    while(!q1.empty())
    {
        u1=q1.front();q1.pop();    
        u2=q2.front();q2.pop();
        d1=q3.front();q3.pop();
        step2=q4.front();q4.pop();
        for(i=0;i<3;i++)
        {
            if(canmoveto(u1,u2,d1,i)==1)
            {
                if(i==2)
                {
                    step3=step2+1;
                    if(step3>4)
                    {
                        step3=step3%5;
                    }
                }
                else
                {
                    step3=step2;
                }
                v1=moveto1(u1,d1,i);
                v2=moveto2(u2,d1,i);
                d2=moveto(d1,i);
                if(v1==tx&&v2==ty&&step3==tcolor)
                {
                    return step[u1][u2][d1][step2]+1;
                }
                else if(step[v1][v2][d2][step3]==0)
                {
                    if(v1!=tx||v2!=ty)
                    {
                        step[v1][v2][d2][step3]=step[u1][u2][d1][step2]+1;
                        t=step[v1][v2][d2][step3];
                        q1.push(v1);
                        q2.push(v2);
                        q3.push(d2);    
                        q4.push(step3);
                    }
                }
            }
        }
        if(q1.empty()&&v1==0)
        {
            d2=d1+2;
            if(d2>=4)
            {
                d2=d2%4;
            }
            step[u1][u2][d2][step2]=step[u1][u2][d1][step2]+2;
            t=step[u1][u2][d2][step2];
            step3=step2;
            q1.push(u1);
            q2.push(u2);
            q3.push(d2);
            q4.push(step3);//老套路,进行储存;
        }
    }
}

int canmoveto(int x,int y,int z,int dire)//如果下一步是'.',便可以向前移动;
{
    int row,col,dir;
    
    if(x==tx&&y==ty)
    {
        return 0;
    }
    if(dire==0)
    {
        dir=z+1;
        if(dir==4)
        {
            dir=0;
        }
        row=x+dr[dir];
        col=y+dc[dir];
        if(mig[row][col]=='.')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else if(dire==1)
    {
        dir=z-1;
        if(dir==0)
        {
            dir=3;
        }
        row=x+dr[dir];
        col=y+dc[dir];
        if(mig[row][col]=='.')
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        row=x+dr[z];
        col=y+dc[z];
        if(mig[row][col]=='.'&&row<=19&&row>=2&&col<=19&&col>=2)
        {
            return 1;
        }
        return 0;
    }    
}

int moveto1(int x,int d,int dire)//横坐标;
{
    int row;
    if(dire==0||dire==1)
    {
        return x;
    }
    else
    {
        row=x+dr[d];
        return row;
    }    
}

int moveto2(int y,int d,int dire)//纵坐标;
{
    int col;
    if(dire==0||dire==1)
    {
        return y;
    }
    else
    {
        col=y+dc[d];
        return col;
    }    
}

int moveto(int z,int dire)//方向;
{
    if(dire==0)
    {
        if(z+1>3)
        {
            return 0;
        }
        else
        {
            return z+1;
        }    
    }
    else if(dire==1)
    {
        if(z-1<0)
        {
            return 3;
        }
        else
        {
            return z-1;
        }        
    }
    else
    {
        return z;
    }

}

本答案仅供参考,不得抄袭,希望你们尽早解决问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值