问题 B: 脱离地牢

题目

题解及坑点:

1. 首先不需要判断坐标是否会越界,因为是地牢,周围一圈都不能走,我一开始判断了,但是写成了x<m,y<n;其实是n行m列,所以应该是x<n,y<m;致wa

2. 再来位置移动的增量数组容易写错,向北移动是x+1,y不变,向西移动是y-1,x不变,和平面直角坐标系相反,所以有点反直觉

3. 用一个四维数组来记录两者以访问过的状态,不可以分别用两个数组来记录,因为点A可能已经被P访问过了,但是P在A,H在B的这种状态还未访问过

4. 判断结束的条件有两个:两者相遇或者两者互换位置(移动后碰在一起)

5. 判断visit是否访问过,要在两者是否互换位置之后判断,因为visit数组一般是用来记录从这个点出发的状态访问过,而这道题可能是出发访问过,到达没访问过,也就是说若这个状态先被某个过程访问了,但是其下面的过程可以使其结束得更早,这样就会造成问题

上代码

char a[25][25];
bool visit[21][21][21][21]={false};
int X[4]={-1,1,0,0};///注意东南西北平移时的写法,北移是x+1,y不变,东移是y+1,x不变
int Y[4]={0,0,-1,1};
int nx[4],ny[4];
int n,m;///n行m列
int cnt=0;
struct node{int px, py, hx,hy,step=0;}temp1,S;
bool check1(int x,int y)
{
    //if(x>n||y>m||x<1||y<1) {return false;}///x>n不是y>m;致wa
    if(a[x][y]=='#'||a[x][y]=='!') {return false;}
    else return true;
}
int check2(int x,int y)
{
    if(/*x>n||y>m||x<1||y<1||*/a[x][y]=='!') return -1;///P不能移动
    if(a[x][y]=='#') return 0;///P可以移动,H呆在原地
    return 1;///H也可以移动
}
int bfs()
{
    queue<node>q1;
    visit[S.px][S.py][S.hx][S.hy]=true;
    q1.push(S);
    while(!q1.empty())
    {
        //cout<<111;
        node top1=q1.front();
        q1.pop();
        if(temp1.step>=256) return -1;
        for(int i=0;i<4;i++)
        {
            temp1.px=top1.px+X[i],temp1.py=top1.py+Y[i];
            temp1.hx=top1.hx+nx[i],temp1.hy=top1.hy+ny[i];
            ///以下两行判断是否能移动
            if(check1(temp1.px,temp1.py)==false||check2(temp1.hx,temp1.hy)==-1) continue;
            if(check2(temp1.hx,temp1.hy)==0) temp1.hx-=nx[i],temp1.hy-=ny[i];
            temp1.step=top1.step+1;
            ///判断是否位置互换
            if((top1.hx==temp1.px&&top1.hy==temp1.py)&&(temp1.hy==top1.py&&temp1.hx==top1.px))///位置互换
                return temp1.step;
            ///判断是否访问过要在判断是否位置互换后面,
            if(visit[temp1.px][temp1.py][temp1.hx][temp1.hy]==true) continue ;
            if(temp1.hx==temp1.px&&temp1.hy==temp1.py) ///相遇
                return temp1.step;
            q1.push(temp1);
            ///判断是否相遇,在处理visit数组
            visit[temp1.px][temp1.py][temp1.hx][temp1.hy]=true;///多了个'='导致内存超限
        }
    }
    return -1;
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='P')  S.px=i,S.py=j;
            if(a[i][j]=='H')  S.hx=i,S.hy=j;
        }
    }
    //getchar();
    for(int i=0;i<4;i++)
    {
        char c;
        cin>>c;
        ///处理Helen的增量数组
        if(c=='N') nx[i]=-1,ny[i]=0;
        if(c=='S') nx[i]=1,ny[i]=0;
        if(c=='E') nx[i]=0,ny[i]=1;
        if(c=='W') nx[i]=0,ny[i]=-1;
    }
    int ans=bfs();
    //for(int i=0;i<4;i++) cout<<X[i]<<' '<<Y[i]<<endl,cout<<nx[i]<<' '<<ny[i]<<endl;
    //for(int i=0;i<4;i++)
    if(ans==-1) cout<<"Impossible\n";
    else cout<<ans<<endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值