思路:d[x][y][z]表示以z方向走到(x, y)的转弯次数。
如果用优先队列会超时,因为加入队列的节点太多,无用的节点不能及时出队,会造成MLE,用单调队列即可。
AC代码
#include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <utility> #include <string> #include <iostream> #include <map> #include <set> #include <vector> #include <queue> #include <stack> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10 #define inf 0x3f3f3f3f #define PI pair<int, int> typedef long long LL; const int maxn = 1000 + 5; map<PI, int>dir; const int dx[] = {0,0,-1,1}; const int dy[] = {1,-1,0,0}; int n, m, d[maxn][maxn][4], G[maxn][maxn]; struct node{ int x, y, tc, dir; node() {} node(int x, int y, int tc, int dir):x(x), y(y), tc(tc), dir(dir) { } }; bool bfs(int x1, int y1, int x2, int y2) { memset(d, inf, sizeof(d)); queue<node>q; for(int i = 0; i < 4; ++i) { d[x1][y1][i] = 0; q.push(node(x1, y1, 0, i)); } while(!q.empty()) { node p = q.front(); q.pop(); int x = p.x, y = p.y, tc = p.tc, dir = p.dir; if(tc > d[x][y][dir]) continue; for(int i = 0; i < 4; ++i) { int px = x + dx[i], py = y + dy[i]; if(px < 0 || py < 0 || px >= n || py >= m) continue; int pt = tc; if(i != dir) ++pt; //发生偏转 if(pt > 2) continue; if(px == x2 && py == y2) return true; if(G[px][py]) continue; if(pt < d[px][py][i]) { d[px][py][i] = pt; q.push(node(px, py, pt, i)); } } } return false; } int main() { int q; while(scanf("%d%d", &n, &m) == 2 && n && m) { for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) scanf("%d", &G[i][j]); scanf("%d", &q); int x1, y1, x2, y2; while(q--) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); --x1, --y1, --x2, --y2; if( G[x1][y1] && G[x2][y2] && G[x1][y1] == G[x2][y2] && bfs(x1, y1, x2, y2)) printf("YES\n"); else printf("NO\n"); } } return 0; }
如有不当之处欢迎指出!