#include<bits/stdc++.h>
using namespace std;
inline int read() {
char l = getchar();
int x = 0;
bool f = 1;
while (l < '0' || l > '9') {
if (l == '-') {
f = 0;
}
l = getchar();
}
while ('0' <= l && l <= '9') {
x = x * 10 + l - '0';
l = getchar();
}
return x * ((f << 1) - 1);
}
struct dot {
int x, y;
};
struct map_dot {
dot c;
int stat;
};
int f[35][35][1030];
bool map1[35][35];
int ans = 2147364847, x, y;
dot c[15], C[15];
queue<map_dot>q;
int main() {
int n = read(), m = read(), k;
memset(f, 0x7f, sizeof(f));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c != '#') {
map1[i][j] = 1;
}
if (c == 'S') {
f[i][j][0] = 0;
q.push(map_dot{dot{i, j}, 0});
}
if (c == 'T') {
x = i, y = j;
}
}
}
k = read();
for (int i = 1; i <= k; i++) {
c[i].x = read(), c[i].y = read(), C[i].x = read(), C[i].y = read();
}
while (!q.empty()) {
map_dot now = q.front();
q.pop();
dot l = now.c;
int stat = now.stat;
if (l.x == x && l.y == y) {
ans = f[x][y][stat];
break;
}
bool map2[35][35];
memset(map2, 0, sizeof(map2));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
map2[i][j] = map1[i][j];
}
}
for (int i = 1; i <= k; i++) {
if (stat & (1 << (i - 1))) {
map2[C[i].x][C[i].y] ^= 1;
}
}
int x1, y1;
vector<int>p;
x1 = l.x + 1, y1 = l.y;
if (map2[x1][y1]) {
int flag = 1;
for (int i = 1; i <= k; i++) {
if (x1 == c[i].x && y1 == c[i].y) {
p.push_back(i);
flag = 0;
}
}
if (flag) {
if (f[x1][y1][stat] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat});
}
} else {
int stat1 = stat;
for (int i = 0; i < p.size(); i++) {
stat1 ^= (1 << (p[i] - 1));
}
if (f[x1][y1][stat1] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat1] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat1});
}
}
}
x1 = l.x - 1, y1 = l.y;
if (map2[x1][y1]) {
int flag = 1;
for (int i = 1; i <= k; i++) {
if (x1 == c[i].x && y1 == c[i].y) {
p.push_back(i);
flag = 0;
}
}
if (flag) {
if (f[x1][y1][stat] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat});
}
} else {
int stat1 = stat;
for (int i = 0; i < p.size(); i++) {
stat1 ^= (1 << (p[i] - 1));
}
if (f[x1][y1][stat1] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat1] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat1});
}
}
}
x1 = l.x, y1 = l.y + 1;
if (map2[x1][y1]) {
int flag = 1;
for (int i = 1; i <= k; i++) {
if (x1 == c[i].x && y1 == c[i].y) {
p.push_back(i);
flag = 0;
}
}
if (flag) {
if (f[x1][y1][stat] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat});
}
} else {
int stat1 = stat;
for (int i = 0; i < p.size(); i++) {
stat1 ^= (1 << (p[i] - 1));
}
if (f[x1][y1][stat1] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat1] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat1});
}
}
}
x1 = l.x, y1 = l.y - 1;
if (map2[x1][y1]) {
int flag = 1;
for (int i = 1; i <= k; i++) {
if (x1 == c[i].x && y1 == c[i].y) {
p.push_back(i);
flag = 0;
}
}
if (flag) {
if (f[x1][y1][stat] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat});
}
} else {
int stat1 = stat;
for (int i = 0; i < p.size(); i++) {
stat1 ^= (1 << (p[i] - 1));
}
if (f[x1][y1][stat1] > f[l.x][l.y][stat] + 1) {
f[x1][y1][stat1] = f[l.x][l.y][stat] + 1;
q.push(map_dot{dot{x1, y1}, stat1});
}
}
}
}
if (ans == 0x7f7f7f7f) {
cout << "-1";
} else {
cout << ans;
}
return 0;
}
数据这么水吗,样例错误,输出24,交洛谷100分