版本比较
5.8下午bfs进阶--东塘--蒋
正文
增加 43 行删除 43 行
24 行未改动2526272829293031323233343536373738394041424243444546464748495051525354545556575859606161626364656667676869707172737475767677787980818283848485868687888990919292939495969697989910010110210310410510510610710810938 行未改动1481491501511521521531541551561571571581591601611611621631641651661671681691691701711721731741751761761771781791801811811821831841851861871881861871881891901911921931931941951951961971981992002002012022032042052062072082082092102112112122132142152152162172182196 行未改动22622722822923023023123223223323423523623723823923924024124124224324424536 行未改动2822832842852862862872882892906 行未改动2972982993003013013023033043053063073073083093103113123133133143153163172 行未改动32032132232332432432532632732832832933033133233333333433533633733733833934034134234334234334424 行未改动int bfs(int qx, int qy){ queue<Node> q; memset(vis, 0, sizeof vis);
q.push({qx, qy, 0}); vis[qx][qy] = 1;
while(q.empty() == 0) { Node c = q.front(); q.pop();
if(c.x == zx && c.y == zy) { return c.t; }
for(int i = 0; i < 4; i++) { int nx = c.x + dx[i], ny = c.y + dy[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == 0 && c.t < dis[nx][ny] - 1 && a[nx][ny] != 'X') { q.push({nx, ny, c.t + 1}); vis[nx][ny] = 1; } } }
return -1;}
int main(){ cin >> n >> m;
for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> a[i][j];
if(a[i][j] == 'S') { qx = i, qy = j; } else if(a[i][j] == 'D') { zx = i, zy = j; }
if(a[i][j] == '*') { q1.push({i, j, 0}); vis[i][j] = 1; } } }
memset(dis, 0x3f, sizeof dis);
while(q1.empty() == 0) { Node c = q1.front(); q1.pop(); dis[c.x][c.y] = c.t;
for(int i = 0; i < 4; i++) { int nx = c.x + dx[i], ny = c.y + dy[i];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == 0 && a[nx][ny] != 'X' && !(nx == zx && ny == zy)) { q1.push({nx, ny, c.t + 1}); vis[nx][ny] = 1; } } } dis[zx][zy] = 1e9;
int o = bfs(qx, qy); if(o == -1) { cout << "KAKTUS";38 行未改动 while(q.empty() == 0) { Node c = q.front(); q.pop();
if(c.x == qx && c.y == qy) { return c.bu; }
for(int i = 0; i < 4; i++) { int nx = c.x + dx[i], ny = c.y + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] != 'X' && vis[nx][ny] == 0) { q.push({nx, ny, c.bu + 1}); vis[nx][ny] = 1; } } }
return -1;}
int main(){ cin >> n >> m;
for(int i = 0; i < n; i++) { cin >> a[i]; }
while(cin >> zx >> zy >> qx >> qy) { if(qx == 0 && qy == 0 && zx == 0 && zy == 0) { return 0; } return 0; }
while(q.empty() == 0) { q.pop(); }
qx--, qy--, zx--, zy--;
for(int i = 0; i < n; i++) { vis[i].clear(); }
for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { vis[i].push_back(0); } }
q.push({zx, zy, 0}); vis[zx][zy] = 1;
for(int i = 0; i < 8; i++) { int nx = zx + dx2[i], ny = zy + dy2[i];
while(nx >= 0 && nx < n && ny >= 0 && ny < m) { if(a[nx][ny] != 'X') {6 行未改动 break; } } }
int o = bfs();
if(o == -1) { cout << "Poor Harry\n"; } else { cout << o << "\n"; cout << o << "\n"; } } } return 0;}```# [P1649 [USACO07OCT] Obstacle Course S](https://www.luogu.com.cn/problem/P1649)36 行未改动 int nx = c.x + dx[i], ny = c.y + dy[i], t = c.ci;
if(c.Dx != dx[i] || c.Dy != dy[i]) { t++; t++; }
if(nx < 1 || nx > n || ny < 1 || ny > n || a[nx][ny] == 'x') {6 行未改动 q.push({nx, ny, dx[i], dy[i], t}); } } }
return ;}int main(){ cin >> n;
for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { cin >> a[i][j];
if(a[i][j] == 'B') { zx = i; zy = j;2 行未改动 { qx = i; qy = j; }
vis[i][j] = 1e9; } }
for(int i = 0; i < 4; i++) { bfs(qx, qy, dx[i], dy[i]); }
if(vis[zx][zy] == 1e9) { cout << -1; } } else { cout << vis[zx][zy]; } return 0; } return 0;}```