3 条题解

  • 1
    @ 2025-5-28 20:48:33
    #include <iostream>
    #include <vector>
    #include <queue>
    #include <climits>
    using namespace std;
    
    struct sttr {
        int x, y;
        int time;
        sttr(int _x, int _y, int _t) : x(_x), y(_y), time(_t) {}
    };
    
    int main() {
        int t, x, y;
        cin >> t >> x >> y;
        vector<string> grid(y);
        sttr start(-1, -1, 0), end(-1, -1, 0);
        for (int i = 0; i < y; ++i) {
            cin >> grid[i];
            for (int j = 0; j < x; ++j) 
                if (grid[i][j] == 's') start = sttr(j, i, 0);
                else if (grid[i][j] == 'm') end = sttr(j, i, 0);        
        }
        if (start.x == -1 || end.x == -1) {
            cout << "55555" << endl;
            return 0;
        }
        const int dx[] = {0, 0, -1, 1};
        const int dy[] = {-1, 1, 0, 0};
        vector<vector<int>> vv(y, vector<int>(x, INT_MAX));
        vv[start.y][start.x] = 0;
        queue<sttr> q;
        q.push(start);
        while (!q.empty()) {
            sttr str = q.front();
            q.pop();
            if (str.x == end.x && str.y == end.y) {
                if (str.time <= t) cout << str.time << endl;
                 else cout << "55555" << endl;
                return 0;
            }
            for (int i = 0; i < 4; ++i) {
                int nx = str.x + dx[i];
                int ny = str.y + dy[i];
                if (nx >= 0 && nx < x && ny >= 0 && ny < y && grid[ny][nx] != 'o') {
                    int new_time = str.time;
                    if (grid[ny][nx] == '.') 
                        new_time += 1;
                     else if (grid[ny][nx] == '#') new_time += 2;
                     else if (grid[ny][nx] == 'm') new_time += 1; 
                    if (new_time < vv[ny][nx]) {
                        vv[ny][nx] = new_time;
                        q.push(sttr(nx, ny, new_time));
                    }
                }
            }
        }
        cout << "55555";
    }
    
    
    • -1
      @ 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);
      }
      
      • -2
        @ 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
        标签
        递交数
        139
        已通过
        23
        上传者