3 条题解

  • -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);
    }
    

    信息

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