样例过不了,但提交 50pts。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll r, c, p, stx, sty, edx, edy, ta[15], tb[15], tc[15], td[15], fx[5] = {0, 1, -1, 0, 0}, fy[5] = {0, 0, 0, 1, -1};
char k;
bool b[35][35], vis[35][35][1030];
struct qp
{
ll x, y, sz, k;
}tmp;
queue <qp> q;
int main()
{
cin >> r >> c;
for (ll i = 1; i <= r; i++)
{
for (ll j = 1; j <= c; j++)
{
cin >> k;
if (k == '#') b[i][j] = 1;
if (k == 'S') stx = i, sty = j;
if (k == 'T') edx = i, edy = j;
}
}
cin >> p;
for (ll i = 1; i <= p; i++) cin >> ta[i] >> tb[i] >> tc[i] >> td[i];
q.push({stx, sty, 0, 0});
while (q.size())
{
tmp = q.front();
q.pop();
//cout << tmp.x << " " << tmp.y << " " << tmp.sz << " " << tmp.k << endl;
if (vis[tmp.x][tmp.y][tmp.k]) continue;
vis[tmp.x][tmp.y][tmp.k] = 1;
if (tmp.x == edx && tmp.y == edy)
{
cout << tmp.sz;
break;
}
for (ll i = 1; i <= 4; i++)
{
ll nx = tmp.x + fx[i], ny = tmp.y + fy[i];
if (nx < 1 || nx > r || ny < 1 || ny > c) continue;
bool num = 0;
for (ll j = 1; j <= p; j++) if (tc[j] == nx && td[j] == ny) num ^= (tmp.k >> (j - 1)) & 1;
if (num ^ b[nx][ny]) continue;
for (ll j = 1; j <= p; j++) if (ta[j] == nx && tb[j] == ny) tmp.k ^= (1 << (j - 1));
q.push({nx, ny, tmp.sz + 1, tmp.k});
}
}
}