#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5, INF = 2e9, MOD = 1e9 + 7;
inline int read()
{
char ch = getchar();
int x = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
void write(const int x)
{
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, t, k;
bool a[N][N];
struct Node
{
int x, y, p;
Node(int _x, int _y, int _p): x(_x), y(_y), p(_p){}
};
struct Node2
{
int x, y;
}q[N * N];
int dis[N][N];
int sx, sy, ex, ey;
int dx[] = { 0, 0, -1, 1 };
int dy[] = { -1, 1, 0, 0 };
void bfs()
{
queue<Node> q;
q.push(Node(ex, ey, 0));
while (q.size())
{
Node l = q.front();
q.pop();
if (dis[l.x][l.y] != -1) continue;
dis[l.x][l.y] = l.p;
for (register int i = 0; i < 4; ++i)
{
int nx = l.x + dx[i], ny = l.y + dy[i];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !a[nx][ny] && dis[nx][ny] == -1)
{
q.push(Node(nx, ny, l.p + 1));
}
}
}
}
signed main()
{
// freopen("*.in", "r", stdin);
// freopen("*.out", "w", stdout);
t = read();
while (t--)
{
n = read(), m = read();
for (register int i = 1; i <= n; ++i)
{
for (register int j = 1; j <= m; ++j)
{
char c;
scanf(" %c", &c);
a[i][j] = c - '0';
dis[i][j] = -1;
}
}
k = read();
for (register int i = 1; i <= k; ++i) q[i].x = read(), q[i].y = read();
sx = read(), sy = read(), ex = read(), ey = read();
bfs();
int kl = dis[sx][sy];
if (kl == -1) printf("F\n");
else
{
bool f(1);
for (register int i(1); i <= k; ++i)
{
if (dis[q[i].x][q[i].y] != -1 && dis[q[i].x][q[i].y] <= kl)
{
f = false;
break;
}
}
if (f) printf("T\n");
else printf("F\n");
}
}
return 0;
}
这份代码复杂度应该是 O(nmt) 的,为什么会被卡超时几十毫秒。
是常数问题还是复杂度问题?