TLE5个点

P2199 最后的迷宫

@[For_Miku](/user/133116) ``` #include<iostream> #include<queue> #include<cstdio> #include<queue> #include<cstring> using namespace std; struct xy { int x; int y; int t; }; queue < xy > q; int vis[1001][1010]; char ma[1010][1010]; int mx[9] = { 0, 1, 1, 1, 0, -1, -1, -1 }; int my[9] = { 1, 1, 0, -1, -1, -1, 0, 1 }; int mx1[5] = { 0, 1, 0, -1 }; int my1[5] = { 1, 0, -1, 0 }; int n, m; int xx, yy, xxx, yyy; int f; int cnt; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) cin >> ma[i][j]; } while (scanf("%d%d%d%d", &xx, &yy, &xxx, &yyy)) { cnt++; if (!xx && !yy && !xxx && !yyy) return 0; f = 0; vis[xx][yy] = cnt * 2; for (int i = 0; i <= 7; ++i) { int u = xx + mx[i]; int v = yy + my[i]; while (1 <= u && u <= n && 1 <= v && v <= m && ma[u][v] == 'O') { vis[u][v] = cnt * 2;; u += mx[i]; v += my[i]; } } if (vis[xxx][yyy] == cnt * 2) { cout << 0 << endl; continue; } while (!q.empty()) q.pop(); q.push(xy{xxx, yyy, 0}); while (!q.empty()) { int u = q.front().x; int v = q.front().y; int ut = q.front().t; q.pop(); for (int i = 0; i <= 3; ++i) { int u1 = u + mx1[i]; int v1 = v + my1[i]; if (u1 < 1 || u1 > n || v1 < 1 || v1 > m) continue; if (vis[u1][v1] == cnt * 2 - 1) continue; if (vis[u1][v1] == cnt * 2) { f = 1; cout << ut + 1 << endl; break; } if (ma[u1][v1] == 'O') { vis[u1][v1] = cnt * 2 - 1; q.push((xy){u1, v1, ut + 1}); } } if (f) break; } if (!f) printf("Poor Harry\n"); } return 0; } ``` 有空请您访问我的 [CSDN博客](https://blog.csdn.net/metaphysis),里面有我写的一本书,内有编程竞赛相关内容的介绍,并附有对应的练习题目(题目源自UVa OJ),可免费下载此书的PDF版本:[《C++,挑战编程——程序设计竞赛进阶训练指南》](https://blog.csdn.net/metaphysis/article/details/90288252)。
by metaphysis @ 2020-10-05 13:32:41


@[metaphysis](/user/333388) 不胜感激(呜呜呜)
by Xhesika_Frost @ 2020-10-05 14:00:45


|