LG:P1126机器人搬重物 题解

由于这道题刚了三天才做完(还不是因为我菜……),于是写一写题解记录一下……

题解中共有3份代码,前两份可忽略,第三份会有部分解释。代码中会有很多重复内容和枚举内容,其实可以存一下过程后循环做。

点我看题

首先,我发现了这句话:“如果无法到达,输出−1。”

于是交了一发-1…… score:20。

开始专心看题。

看完便知道是搜索,但需要考虑的是机器人站在格点上,但障碍物在格子里,这样如果全存在一张图里肯定会乱。然鹅存在两个图里我肯定会乱……

于是突发奇想,只记录机器人左上角的格子的位置,判断能否走到下一格时顺带判断周围三个格子,方向单独判断不就没了?

于是兴奋地写了一发深搜(注意,是深搜,会TLE+RE+……)打完发现样例过不了……

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#define maxn 100
using namespace std;

int n,m,stx,sty,enx,eny,to,ans=47483647;
int f[maxn][maxn][5];
int dx[5]={0,1,0,-1,0};
int dy[5]={0,0,1,0,-1};
bool map[maxn][maxn];

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=10*x+ch-'0',ch=getchar();
    return f*x;
}

void dfs(int x,int y,int pos,int sum)
{
    if(x==enx&&y==eny) {ans=min(ans,sum);return;}
    if(sum>f[x][y][pos]) return;
    f[x][y][pos]=sum;
    if(pos==1)
    {
        for(register int i=1;i<=3;i++)
        {
            int xx=x+i,yy=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,2,sum+2);
            xx=x-i;y=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,4,sum+2);
            xx=x;yy=y+i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,1,sum+1);
            xx=x;yy=y-i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,3,sum+3);
        }
    }else
    if(pos==2)
    {
        for(register int i=1;i<=3;i++)
        {
            int xx=x+i,yy=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,2,sum+1);
            xx=x-i;y=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,4,sum+3);
            xx=x;yy=y+i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,1,sum+2);
            xx=x;yy=y-i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,3,sum+2);
        }
    }else
    if(pos==3)
    {
        for(register int i=1;i<=3;i++)
        {
            int xx=x+i,yy=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,2,sum+2);
            xx=x-i;y=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,4,sum+2);
            xx=x;yy=y+i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,1,sum+3);
            xx=x;yy=y-i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,3,sum+1);
        }
    }else
    if(pos==4)
    {
        for(register int i=1;i<=3;i++)
        {
            int xx=x+i,yy=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,2,sum+3);
            xx=x-i;y=y;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,4,sum+1);
            xx=x;yy=y+i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,1,sum+2);
            xx=x;yy=y-i;
            if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) dfs(xx,yy,3,sum+2);
        }
    }
}

int main()
{
    n=read(),m=read();
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=m;j++)
            map[i][j]=read();
    stx=read(),sty=read(),enx=read(),eny=read();
    char ch;
    while(ch!='E'&&ch!='S'&&ch!='W'&&ch!='N') ch=getchar();
    if(ch=='E') to=1;
    else if(ch=='S') to=2;
    else if(ch=='W') to=3;
    else if(ch=='N') to=4;
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n;j++) 
            f[i][j][1]=f[i][j][2]=f[i][j][3]=f[i][j][4]=47483647;
    dfs(stx,sty,to,0);
    printf("%d",ans==47483647? -1:ans);

    return 0;
}

2AC+3TLE+5WA

于是疯狂差错……

第二天,蔡老师提示我:首先,网格图用DFS就是个SB错误……

继续改………………………………………………………………………………

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#define maxn 1000
using namespace std;

int n,m,stx,sty,enx,eny,ans=47483647;
int map[maxn][maxn],f[maxn][maxn][5];
struct node
{
    int x,y,pos,val;
};
queue<node> q;

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=10*x+ch-'0',ch=getchar();
    return f*x;
}

void bfs()
{
    while(!q.empty())
    {
        node tmp=q.front();q.pop();
        int x=tmp.x,y=tmp.y,pos=tmp.pos,sum=tmp.val;
        if(x==enx&&y==eny) {ans=min(ans,sum);break;}
        if(sum>=f[x][y][pos]) continue;
        f[x][y][pos]=sum;
        if(pos==1)
        {
            for(register int i=1;i<=3;i++)
            {
                int xx=x+i,yy=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,2,sum+2});
                xx=x-i;y=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,4,sum+2});
                xx=x;yy=y+i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,1,sum+1});
                xx=x;yy=y-i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,3,sum+3});
            }
        }else
        if(pos==2)
        {
            for(register int i=1;i<=3;i++)
            {
                int xx=x+i,yy=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,2,sum+1});
                xx=x-i;y=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,4,sum+3});
                xx=x;yy=y+i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,1,sum+2});
                xx=x;yy=y-i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,3,sum+2});
            }
        }else
        if(pos==3)
        {
            for(register int i=1;i<=3;i++)
            {
                int xx=x+i,yy=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,2,sum+2});
                xx=x-i;y=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,4,sum+2});
                xx=x;yy=y+i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,1,sum+3});
                xx=x;yy=y-i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,3,sum+1});
            }
        }else
        if(pos==4)
        {
            for(register int i=1;i<=3;i++)
            {
                int xx=x+i,yy=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,2,sum+3});
                xx=x-i;y=y;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,4,sum+1});
                xx=x;yy=y+i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,1,sum+2});
                xx=x;yy=y-i;
                if((!map[xx][yy])&&(!map[xx+1][yy])&&(!map[xx][yy+1])&&(!map[xx+1][yy+1])&&(xx>=1)&&(xx<n)&&(yy>=1)&&(yy<m)) q.push((node){xx,yy,3,sum+2});
            }
        }
    }
}

int main()
{
    n=read(),m=read();
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=m;j++)
            map[i][j]=read();
    stx=read(),sty=read(),enx=read(),eny=read();
    char ch;
    int to; 
    while(ch!='E'&&ch!='S'&&ch!='W'&&ch!='N') ch=getchar();
    if(ch=='E') to=1;
    else if(ch=='S') to=2;
    else if(ch=='W') to=3;
    else if(ch=='N') to=4;
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n;j++) 
            f[i][j][1]=f[i][j][2]=f[i][j][3]=f[i][j][4]=47483647;
    q.push((node){stx,sty,to,0});
    bfs();
    
    printf("%d",ans==47483647? -1:ans);

    return 0;
}

改完还是不对啊,连结果都一样……

第三天疯狂询问机房各大佬神仙,某W姓神仙提醒:输出q中弹出的点找找路径吧

找了几个点便发现了问题:机器人想去下一个点可以走1或2或3步,然鹅因为我直接判断的目的点是否合法,中间点是否合法没有判断……

在某D姓大佬嘲笑下手动修改完毕,便直接AC(长舒一口气)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#define maxn 1000
using namespace std;

int n,m,stx,sty,enx,eny,ans=47483647;          //变量应该很好理解吧……
int map[maxn][maxn],f[maxn][maxn][5];          //map存地图,f记录坐标为x,y,位置为pos的最小值
struct node
{
    int x,y,pos,val;
};
queue<node> q;                                 //bfs用的队列

inline int read()                              //可忽略的快读,没啥用
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=10*x+ch-'0',ch=getchar();
    return f*x;
}

inline bool pd1(int x,int y) {return ((!map[x][y])&&(!map[x+1][y])&&(!map[x][y+1])&&(!map[x+1][y+1]));}
inline bool pd2(int x,int y) {return ((x>=1)&&(x<n)&&(y>=1)&&(y<m));}

/*
	判断机器人的位置是否合法
	判断点坐标是否合法
*/

void bfs()
{
    while(!q.empty())
    {
        node tmp=q.front();q.pop();
        int x=tmp.x,y=tmp.y,pos=tmp.pos,sum=tmp.val;
        if(x==enx&&y==eny) {ans=min(ans,sum);continue;}
        if(sum>=f[x][y][pos]) continue;
        f[x][y][pos]=sum;                                                //做记录
        //以下内容可能会引起部分人生理和心理上的不适
		//简单说就是分4个方向判断走几步,如果没问题便加入队列
		//可以直接跳过这段……
		if(pos==1)
        {
            int xx=x+1,yy=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+2});
            xx=x-1;y=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+2});
            xx=x;yy=y+1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+1});
            xx=x;yy=y-1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+3});
            
            xx=x+2,yy=y;
            if(pd2(xx,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+2});
            xx=x-2;y=y;
            if(pd2(xx,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+2});
            xx=x;yy=y+2;
            if(pd2(xx,yy)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+1});
            xx=x;yy=y-2;
            if(pd2(xx,yy)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+3});
            
            xx=x+3,yy=y;
            if(pd2(xx,yy)&&pd1(xx-2,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+2});
            xx=x-3;y=y;
            if(pd2(xx,yy)&&pd1(xx+2,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+2});
            xx=x;yy=y+3;
            if(pd2(xx,yy)&&pd1(xx,yy-2)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+1});
            xx=x;yy=y-3;
            if(pd2(xx,yy)&&pd1(xx,yy+2)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+3});
        }else
        if(pos==2)
        {
            int xx=x+1,yy=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+1});
            xx=x-1;y=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+3});
            xx=x;yy=y+1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+2});
            xx=x;yy=y-1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+2});

            xx=x+2,yy=y;
            if(pd2(xx,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+1});
            xx=x-2;y=y;
            if(pd2(xx,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+3});
            xx=x;yy=y+2;
            if(pd2(xx,yy)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+2});
            xx=x;yy=y-2;
            if(pd2(xx,yy)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+2});

            xx=x+3,yy=y;
            if(pd2(xx,yy)&&pd1(xx-2,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+1});
            xx=x-3;y=y;
            if(pd2(xx,yy)&&pd1(xx+2,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+3});
            xx=x;yy=y+3;
            if(pd2(xx,yy)&&pd1(xx,yy-2)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+2});
            xx=x;yy=y-3;
            if(pd2(xx,yy)&&pd1(xx,yy+2)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+2});
        }else
        if(pos==3)
        {
            int xx=x+1,yy=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+2});
            xx=x-1;y=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+2});
            xx=x;yy=y+1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+3});
            xx=x;yy=y-1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+1});

            xx=x+2,yy=y;
            if(pd2(xx,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+2});
            xx=x-2;y=y;
            if(pd2(xx,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+2});
            xx=x;yy=y+2;
            if(pd2(xx,yy)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+3});
            xx=x;yy=y-2;
            if(pd2(xx,yy)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+1});

            xx=x+3,yy=y;
            if(pd2(xx,yy)&&pd1(xx-2,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+2});
            xx=x-3;y=y;
            if(pd2(xx,yy)&&pd1(xx+2,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+2});
            xx=x;yy=y+3;
            if(pd2(xx,yy)&&pd1(xx,yy-2)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+3});
            xx=x;yy=y-3;
            if(pd2(xx,yy)&&pd1(xx,yy+2)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+1});
        }else
        if(pos==4)
        {
            int xx=x+1,yy=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+3});
            xx=x-1;y=y;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+1});
            xx=x;yy=y+1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+2});
            xx=x;yy=y-1;
            if(pd2(xx,yy)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+2});

            xx=x+2,yy=y;
            if(pd2(xx,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+3});
            xx=x-2;y=y;
            if(pd2(xx,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+1});
            xx=x;yy=y+2;
            if(pd2(xx,yy)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+2});
            xx=x;yy=y-2;
            if(pd2(xx,yy)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+2});

            xx=x+3,yy=y;
            if(pd2(xx,yy)&&pd1(xx-2,yy)&&pd1(xx-1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,2,sum+3});
            xx=x-3;y=y;
            if(pd2(xx,yy)&&pd1(xx+2,yy)&&pd1(xx+1,yy)&&pd1(xx,yy)) q.push((node){xx,yy,4,sum+1});
            xx=x;yy=y+3;
            if(pd2(xx,yy)&&pd1(xx,yy-2)&&pd1(xx,yy-1)&&pd1(xx,yy)) q.push((node){xx,yy,1,sum+2});
            xx=x;yy=y-3;
            if(pd2(xx,yy)&&pd1(xx,yy+2)&&pd1(xx,yy+1)&&pd1(xx,yy)) q.push((node){xx,yy,3,sum+2});
        }
    }
}

int main()
{
    n=read(),m=read();
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=m;j++)
            map[i][j]=read();
    stx=read(),sty=read(),enx=read(),eny=read();
    char ch;
    int to; 
    while(ch!='E'&&ch!='S'&&ch!='W'&&ch!='N') ch=getchar();
	//读入部分
    if(ch=='E') to=1;
    else if(ch=='S') to=2;
    else if(ch=='W') to=3;
    else if(ch=='N') to=4;
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n;j++) 
            f[i][j][1]=f[i][j][2]=f[i][j][3]=f[i][j][4]=47483647;
	//初始化
    q.push((node){stx,sty,to,0});
    bfs();
    //结束……
    printf("%d",ans==47483647? -1:ans);

    return 0;
}

结束

posted @ 2019-09-07 23:46  opqrst678  阅读(35)  评论(0)    收藏  举报