2 条题解

  • 0
    @ 2025-8-4 18:43:34
    • 经典错误 2
    • 结果:TLE + MLE
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int maxn=400+10;
    int dx[]={-1,-2,-2,-1,1,2,2,1};
    int dy[]={-2,-1,1,2,-2,-1,1,2};
    int n,m;
    int d[maxn][maxn];
    void bfs(int x,int y){
    	memset(d,0x3f,sizeof(d));
    	queue<pair<int,int>>q;
    	q.push({x,y});
    	d[x][y]=0;
    	int cnt=0;
    	while(!q.empty()){
    		int mx=q.front().first,my=q.front().second;
    		q.pop();
    		for(int i=0;i<8;i++){
    			int nx=mx+dx[i],ny=my+dy[i];
    			if(nx<1||nx>n||ny<1||ny>m)continue;
    			d[nx][ny]=min(d[nx][ny],d[mx][my]+1);
    			q.push({nx,ny});
    		}
    		cnt++;
    		if(cnt>1000000){
    			throw(114514);
    		}
    	}
    }
    int main(){
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	int x,y;
    	cin>>n>>m>>x>>y;
    	bfs(x,y);
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			cout<<(d[i][j]==0x3f3f3f3f?-1:d[i][j])<<" ";
    		}
    		cout<<"\n";
    	}
    	return 0;
    }
    
    • 0
      @ 2025-8-4 18:41:53
      • 经典错误 1
      • 结果:TLE + MLE
      #include <bits/stdc++.h>
      using namespace std;
      const int maxn = 400 + 10 , maxm = 400 + 10;
      int n , m , x , y;
      int dis[maxn][maxm];
      struct Node {int x , y;};
      queue <Node> q;
      int dx[8] = {-2 , -1 , -1 , -2 , 2 , 1 , 1 ,  2};
      int dy[8] = {-1 , -2 , 2 ,  1 , 1 , 2 , -2 , -1};
      void bfs (int x , int y) {
      	for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) dis[i][j] = 999999;
      	dis[x][y] = 0;
      	q.push ({x , y});
      	while (!q.empty ()) {
      		Node u = q.front ();
      		//cout << u.x << " " << u.y << "\n";
      		q.pop ();
      		for (int i = 0; i <= 8 - 1; i++) {
      			int cx = u.x + dx[i] , cy = u.y + dy[i];
      			//cout << cx << " " << cy << "\n";
      			if (cx < 1 || cx > n || cy < 1 || cy > m) continue;
      			if (dis[cx][cy] < dis[u.x][u.y] + 1) continue;
      			dis[cx][cy] = dis[u.x][u.y] + 1;
      			q.push ({cx , cy});
      		}
      	}
      }
      int main () {
      	ios::sync_with_stdio (false);
      	cin.tie (0) , cout.tie (0);
      	cin >> n >> m >> x >> y;
      	bfs (x , y);
      	for (int i = 1; i <= n; i++) {
      		for (int j = 1; j <= m; j++) cout << (dis[i][j] == 999999 ? -1 : dis[i][j])<< " ";
      		cout << "\n";
      	}
      	return 0; 
      }
      
      • 1

      信息

      ID
      57
      时间
      1000ms
      内存
      128MiB
      难度
      4
      标签
      递交数
      75
      已通过
      32
      上传者