1 条题解

  • 0
    @ 2025-7-17 19:28:23
    #include<bits/stdc++.h>
    using namespace std;
    namespace Fastio{
    	//char ch,tmp[100000],*p(0),*top(0);
    	//#define getchar()(p==top&&(top=(p=tmp)+fread(tmp,1,100000,stdin)),*p++)
    	inline int read(){
    		int x=0;bool f=1;char ch=getchar();
    		while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
    		while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    		return f?x:-x;
    	}
    	inline void write(int x){
    		if(x<0){x=~(x-1);putchar('-');}
    		if(x>9) write(x/10);
    		putchar(x%10+'0');
    		return;
    	}
    }
    using namespace Fastio;
    
    int n,m;
    char a[1005][1005];
    int dis[1005][1005];
    struct node{
    	int x,y,z;
    };
    queue<node>Q;
    int mx[4]={-1,0,0,1};
    int my[4]={0,-1,1,0};
    void bfs(){
    	while(!Q.empty()){
    		node head=Q.front();
    		Q.pop();
    		for(int i=0;i<4;i++){
    			int x=head.x+mx[i];
    			int y=head.y+my[i];
    			if(dis[x][y]>dis[head.x][head.y]+1){
    				dis[x][y]=dis[head.x][head.y]+1;
    				Q.push({x,y,dis[x][y]});
    			} 
    		}
    	}
    	return;
    }
    signed main(){
    	n=read();m=read();
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			dis[i][j]=1e9;
    		}
    	}
    	for(int i=1;i<=n;i++){
    		int j=0;
    		while(j<m){
    			char ch=getchar();
    			if(ch=='0'||ch=='1'){
    				a[i][++j]=ch;
    				if(ch=='1'){
    					Q.push({i,j,0});
    					dis[i][j]=0;
    				}
    			}
    		}
    	}
    	bfs();
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			write(dis[i][j]);putchar(' ');
    		}
    		putchar('\n');
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    554
    时间
    1000ms
    内存
    256MiB
    难度
    5
    标签
    (无)
    递交数
    89
    已通过
    33
    上传者