2 条题解

  • 0
    @ 2025-5-24 18:47:09
    #include<bits/stdc++.h>
    using namespace std;
    char a[30][30];
    bool vis[30][30];
    int x1,y1_,x2,y2,t,m,n;
    int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    bool arrived = false;
    struct node{
    	int x,y,step;
    };
    void bfs(int x,int y,int step){
    	queue<node> q;
    	q.push(node{x,y,step});
    	while(!q.empty()){
    		node front = q.front();
    		q.pop();
    		if(a[front.x][front.y] == 'm'){
    			if (front.step <= t) cout << front.step;
    			else cout << "55555";
    			arrived = true;
    			return;
    		}
    		for(int i = 0;i < 4;i++){
    			int dx = front.x + d[i][0];
    			int dy = front.y + d[i][1];
    			
    			if(dx >= 1 && dx <= m && dy >= 1 && dy <= n && !vis[dx][dy] && a[dx][dy] != 'o'){
    				
    				if(a[front.x][front.y] == '#') q.push(node{dx,dy,front.step + 2});
    				if(a[front.x][front.y] == '.' or a[front.x][front.y] == 's'){
    					q.push(node{dx,dy,front.step + 1});
    //					cout << a[dx][dy] << " ";
    				}
    				vis[dx][dy] = 1;
    				
    			}
    			
    		}
    		
    	}
    	if(!arrived) cout << "55555";
    }
    int main(){
    	cin >> t >> n >> m;
    	for(int i = 1;i <= m;i++){
    		for(int j = 1;j <= n;j++){
    			cin >> a[i][j];
    			if(a[i][j] == 's'){
    				x1 = i;
    				y1_ = j;
    			}
    			if(a[i][j] == 'm'){
    				x2 = i;
    				y2 = j;
    			}
    		}
    	}
    	vis[x1][y1_] = 1;
    	bfs(x1,y1_,0);
    }
    
    • -1
      @ 2024-7-27 9:55:17
      #include<iostream>
      #include<queue>
      #include<cstring>
      using namespace std;
      struct point{
          int x,y;
      };
      queue<point> q;
      int d[30][30],x,y,t,sx,sy,tx,ty;
      char g[30][30];
      bool inq[30][30];
      const int xx[4]={-1,0,1,0};
      const int yy[4]={0,1,0,-1};
      bool check(int nx,int ny){
          if(nx<1||nx>x||ny<1||ny>y)return false;
          if(g[nx][ny]=='o')return false;
          return true;
      }
      void spfa(int sx,int sy){
          memset(d,0x7f,sizeof(d));
          d[sx][sy]=0;
          inq[sx][sy]=true;
          q.push((point){sx,sy});
          while(!q.empty()){
              point u=q.front();
              q.pop();
              inq[u.x][u.y]=false;
              for(int i=0;i<4;i++){
                  int nx=u.x+xx[i];
                  int ny=u.y+yy[i];
                  if(check(nx,ny)){
                      int tmp=1;
                      if(g[u.x][u.y]=='.')tmp=1;
                      if(g[u.x][u.y]=='#')tmp=2;
                      if(d[u.x][u.y]+tmp<d[nx][ny]){
                          d[nx][ny]=d[u.x][u.y]+tmp;
                          if(!inq[nx][ny]){
                              q.push((point){nx,ny});
                              inq[nx][ny]=true;
                          }
                      }
                  }
              }
          }
      }
       
      int main(){
      
          cin>>t>>y>>x;
          for(int i=1;i<=x;i++){
              for(int j=1;j<=y;j++){
                  cin>>g[i][j];
                  if(g[i][j]=='s'){
                      sx=i;
                      sy=j;
                  }
                  if(g[i][j]=='m'){
                      tx=i;
                      ty=j;
                  }
              }
          }
          spfa(sx,sy);
          if(d[tx][ty]<t)cout<<d[tx][ty];
          else cout<<"55555";
      }
      
      • 1

      信息

      ID
      3053
      时间
      2000ms
      内存
      256MiB
      难度
      8
      标签
      递交数
      138
      已通过
      22
      上传者