版本比较
最短路
正文
增加 11 行删除 11 行
41 行未改动424344454646474849504 行未改动555657585959606162637 行未改动7172737475757677787956 行未改动1361371381391401401411421431441441451461471481491501511521531531541551561571571581591591601611621621631641641651661671687 行未改动1761771781791801801811821831847 行未改动41 行未改动 while(!q.empty()){ node t=q.front(); q.pop(); int tx=t.x,ty=t.y,t_st=t.step;
int k=1; if(mp[tx][ty]=='A'||mp[tx][ty]=='B'){ if(mp[tx][ty]=='B'){ k++;4 行未改动 vis[nx][ny]=1; if(nx==n&&ny==n){ return t_st+1; }
q.push({nx,ny,t_st+1,0}); } } }else if(mp[tx][ty]=='C'){7 行未改动 vis[nx][ny]=1; if(nx==n&&ny==n){ return t_st+1; }
q.push({nx,ny,t_st+1,0}); } } }56 行未改动 for(int j = 0; j < m; j++) { dis[i][j] = 1e9; } }
priority_queue<node> q; q.push({sx, sy, 0}); dis[sx][sy] = 0;
while(!q.empty()) { node t = q.top(); q.pop(); if(t.dis > dis[t.x][t.y]) continue; if(t.x == ex && t.y == ey) { cout << t.dis << endl; return; }
for(int i = 0; i < 4; i++) { int nx = t.x + dx[i]; int ny = t.y + dy[i];
if(!check(nx, ny)) continue;
int new_dis = t.dis + 1; if(mp[nx][ny] == 'x') { new_dis = t.dis + 2; new_dis = t.dis + 2; }
if(new_dis < dis[nx][ny]) { dis[nx][ny] = new_dis; q.push({nx, ny, new_dis}); }7 行未改动 for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cin >> mp[i][j]; if(mp[i][j] == 'a') { ex = i, ey = j; ex = i, ey = j; } if(mp[i][j] == 'r') { sx = i, sy = j; }7 行未改动