正文
增加 18 行删除 18 行
41 行未改动4243444546474849504 行未改动5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991003 行未改动10410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713841 行未改动}}__inline__ void bfs(Node p) {if(p.w > dis[p.x][p.y][p.op]) {return;}4 行未改动b[++ tot] = {to[p.x][p.y].x, to[p.x][p.y].y, 8, p.w};work(tot);}}for(int i = 0; i < 8; i ++) {int xx = p.x + px[i], yy = p.y + py[i], ww = p.w + (p.op != i);if(xx < 1 || xx > n || yy < 1 || yy > n || ww >= dis[xx][yy][i] || ch[xx][yy] == '#') {continue;}dis[xx][yy][i] = ww;b[++ tot] = {xx, yy, i, ww};work(tot);}}int main () {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n >> m;for(int i = 1; i <= n; i ++) {for(int j = 1; j <= m; j ++) {for(int k = 0; k <= 8; k ++) {dis[i][j][k] = 1e9;}}}for(int i = 1; i <= n; i ++) {for(int j = 1; j <= m; j ++) {cin >> ch[i][j];if(ch[i][j] == 'S') {b[++ tot] = {i, j, 8, 0};}if(ch[i][j] >= '1' && ch[i][j] <= '9') {if(x[ch[i][j] - '0']) {to[i][j] = {x[ch[i][j] - '0'], y[ch[i][j] - '0']};to[x[ch[i][j] - '0']][y[ch[i][j] - '0']] = {i, j};3 行未改动}}}}while(tot) {Node u = b[1];swap(b[1], b[tot]);tot --;work1(1);bfs(u);}int ans = 1e9;for(int i = 1; i <= n; i ++) {for(int j = 1; j <= m; j ++) {if(ch[i][j] == 'E') {for(int k = 0; k <= 8; k ++) {ans = min(ans, dis[i][j][k]);}}}}if(ans == 1e9) {cout << -1;return 0;}cout << ans;return 0;}```41 行未改动4243444546474849504 行未改动5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991003 行未改动10410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713841 行未改动}}__inline__ void bfs(Node p) {if(p.w > dis[p.x][p.y][p.op]) {return;}4 行未改动b[++ tot] = {to[p.x][p.y].x, to[p.x][p.y].y, 8, p.w};work(tot);}}for(int i = 0; i < 8; i ++) {int xx = p.x + px[i], yy = p.y + py[i], ww = p.w + (p.op != i);if(xx < 1 || xx > n || yy < 1 || yy > n || ww >= dis[xx][yy][i] || ch[xx][yy] == '#') {continue;}dis[xx][yy][i] = ww;b[++ tot] = {xx, yy, i, ww};work(tot);}}int main () {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n >> m;for(int i = 1; i <= n; i ++) {for(int j = 1; j <= m; j ++) {for(int k = 0; k <= 8; k ++) {dis[i][j][k] = 1e9;}}}for(int i = 1; i <= n; i ++) {for(int j = 1; j <= m; j ++) {cin >> ch[i][j];if(ch[i][j] == 'S') {b[++ tot] = {i, j, 8, 0};}if(ch[i][j] >= '1' && ch[i][j] <= '9') {if(x[ch[i][j] - '0']) {to[i][j] = {x[ch[i][j] - '0'], y[ch[i][j] - '0']};to[x[ch[i][j] - '0']][y[ch[i][j] - '0']] = {i, j};3 行未改动}}}}while(tot) {Node u = b[1];swap(b[1], b[tot]);tot --;work1(1);bfs(u);}int ans = 1e9;for(int i = 1; i <= n; i ++) {for(int j = 1; j <= m; j ++) {if(ch[i][j] == 'E') {for(int k = 0; k <= 8; k ++) {ans = min(ans, dis[i][j][k]);}}}}if(ans == 1e9) {cout << -1;return 0;}cout << ans;return 0;}```