2018年湘潭大学程序设计竞赛 F - maze

时间:2024-06-15 15:33:50

把点抽出来 跑个最短路就好啦。

 #include<bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std; const int N=+;
const int M=1e5+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+; int n, m, q, idx, S, T, d[N * N], hs[N][N], dx[] = {, -, , }, dy[] = {, , , -};
char s[N][N]; vector<pii> edge[N * N]; int Dj(int S, int T) {
priority_queue<pii, vector<pii>, greater<pii> > que;
d[S] = ; que.push(mk(, S));
while(!que.empty()) {
pii u = que.top();
que.pop();
if(u.fi < d[u.se]) continue;
if(u.se == T) {
return u.fi;
}
for(pii v : edge[u.se]) {
if(d[u.se] + v.fi < d[v.se]) {
d[v.se] = d[u.se] + v.fi;
que.push(mk(d[v.se], v.se));
}
}
}
return -;
}
void init() {
idx = ;
for(int i = ; i <= n * n + ; i++)
edge[i].clear(), d[i] = inf;
} bool check(int x, int y) {
if(x > n || x < ) return false;
if(y > m || y < ) return false;
if(s[x][y] == '#') return false;
return true;
}
int main() {
while(scanf("%d%d%d", &n, &m, &q) != EOF) {
init();
for(int i = ; i <= n; i++)
scanf("%s", s[i] + );
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
hs[i][j] = ++idx;
if(s[i][j] == 'S') S = idx;
if(s[i][j] == 'T') T = idx;
}
} for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(s[i][j] == '#') continue;
for(int k = ; k < ; k++) {
int nx_x = i + dx[k];
int nx_y = j + dy[k];
if(check(nx_x, nx_y)) {
edge[hs[i][j]].push_back(mk(, hs[nx_x][nx_y]));
}
}
}
} for(int i = ; i <= q; i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
a++, b++, c++, d++;
if(s[a][b] == '#' || s[c][d] == '#') continue;
edge[hs[a][b]].push_back(mk(, hs[c][d]));
} int ans = Dj(S, T);
printf("%d\n", ans);
}
return ;
}
/*
*/