宁搜都不搜是吧

P1746 离开中山路

IOSwallet @ 2022-11-01 16:00:05

RT,本人代码如下:

#include <bits/stdc++.h>
using namespace std;
struct points{
    int x;
    int y;
    int times;
};
queue<points> q;
char mapp[1005][1005]={};
bool flag[1005][1005]={};
int x1,y3,x2,y2,n;
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>mapp[i][j];
            if(mapp[i][j]=='1')flag[i][j]=false;
            else flag[i][j]=true;
        }
    }
    cin>>x1>>y3>>x2>>y2;
    q.push({x1,y3,1});
    flag[x1][y3]=false;
    while(!q.empty()){
        points work=q.front();
        if(work.x==x2&&work.y==y2)cout<<work.times;break;
        if(work.x>0&&flag[work.x-1][work.y]){
            q.push({work.x-1,work.y,work.times+1});
            flag[work.x-1][work.y]=false;
        }
        if(work.x<n-1&&flag[work.x+1][work.y]){
            q.push({work.x+1,work.y,work.times+1});
            flag[work.x+1][work.y]=false;
        }
        if(work.y>0&&flag[work.x][work.y-1]){
            q.push({work.x,work.y-1,work.times+1});
            flag[work.x][work.y-1]=false;
        }
        if(work.y<n-1&&flag[work.x][work.y+1]){
            q.push({work.x,work.y+1,work.times+1});
            flag[work.x][work.y+1]=false;
        }
//      cout<<"test\n";
        q.pop();
    }
//  cout<<q.front().times;
    return 0;
}

根本进不了while循环,求解。

可能有点不好读

附:本人尝试过,队列q中有值。


by yukimianyan @ 2022-11-01 16:07:49

if(work.x==x2&&work.y==y2)cout<<work.times;break;

您好好审视一下


by yukimianyan @ 2022-11-01 16:08:33

@IOSwallet 没加大括号


by donghanwen1225 @ 2022-11-01 16:09:33

@IOSwallet if(work.x==x2&&work.y==y2)cout<<work.times;break; 没有打大括号的话,只有 cout<<work.times; 在 if 语句内,后面的 break 实际上是每次都执行了,当然进不了循环。


by IOSwallet @ 2022-11-01 16:13:08

@donghanwen1225 @yukimianyan

不不不您们误会了

我指的是这东西啥都没输出,包括那个

cout<<woek.times;

小声:我记得我那样写也是可以的吧


by IOSwallet @ 2022-11-01 16:14:06

捉虫,那段代码打错了个字母


cout<<work.times;

by yukimianyan @ 2022-11-01 16:16:49

@IOSwallet 那它确实不满足 x_1=x_2\land y_1=y_2


by Dantal10n @ 2022-11-01 16:32:42

@IOSwallet 不是啊,不加大括号的话无论条件是否满足都会break 那你第一次就不满足break不就寄了


by IOSwallet @ 2022-11-01 16:37:27

回上边众佬:本人改了一下,加上了大括号:

if(work.x==x2&&work.y==y2){cout<<work.times;break;}

这东西还是搜都不搜。

我何德何能能让一众红名橙名佬帮我看代码


by yukimianyan @ 2022-11-01 16:49:08

@IOSwallet 下标从 1 开始


by IOSwallet @ 2022-11-01 16:55:18

@yukimianyan ? 求解


|