求dalao纠错,一直90分

P1746 离开中山路

最喜欢saber了 @ 2018-11-06 20:24:30

求dalao纠错

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cstdlib>
#include<cctype>
#include<queue>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
    x=0;T f=1;char c=getchar();
    for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
    for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';
    x*=f;
}
const int maxa = 1100,dx[]={0,1,0,-1},dy[]={1,0,-1,0};
struct node{
    int x,y,cnt;
}q[maxa*maxa];
int n;
char f[maxa][maxa];
void bfs(int sx,int sy,int ex,int ey){
    int head=1,tail=2;
    q[head].x=sx;
    q[head].y=sy;
    q[head].cnt=0;
    while(head<=tail){
        for(int i=0;i<4;++i){
            int nx=q[head].x+dx[i],ny=q[head].y+dy[i];
            if(nx==ex&&ny==ey){
                printf("%d",q[head].cnt+1);
                return;
            }
            if(nx<1||nx>n||ny<1||ny>n||f[nx][ny]=='1')continue;
            f[nx][ny]='1';
            q[tail].x=nx;
            q[tail].y=ny;
            q[tail].cnt=q[head].cnt+1;
            tail++;
        }
        head++;
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%s",&f[i]);
    }
    int sx,sy,ex,ey;
    scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
    bfs(sx,sy,ex,ey);
    return 0;
}

by kqing @ 2019-02-01 11:16:52

while(head<tail)


|