CodeForces 1898F Vova Escapes the Matrix

洛谷传送门

CF 传送门

Type \(1\) 是简单的。直接输出空格个数即可。

Type \(2\) 也是简单的。显然要堵住不在起点和出口最短路上的格子,答案为空格个数减去起点到任一出口的最短路。

考虑 Type \(3\)。容易发现答案为空格个数减去起点到任两个出口的最短路(公共部分只算一次)。考虑从起点开始出发,一定最多存在一个格子,使得从起点走到它后,两条路径分道扬镳,不再相交。那我们可以枚举这个格子,求出它到最近的两个不同出口的距离(就是最小和次小)。可以简单 bfs 求出。

总时间复杂度 \(O(nm)\)

code
// Problem: F. Vova Escapes the Matrix
// Contest: Codeforces - Codeforces Round 910 (Div. 2)
// URL: https://codeforces.com/contest/1898/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<int, int> pii;

const int maxn = 1010;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};

int n, m, f[maxn][maxn][2], g[maxn][maxn];
bool vis[maxn][maxn][2], mk[maxn][maxn];
pii h[maxn][maxn][2];
char s[maxn][maxn];

struct node {
	int x, y, o;
	node(int a = 0, int b = 0, int c = 0) : x(a), y(b), o(c) {}
};

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%s", s[i] + 1);
	}
	int sx = -1, sy = -1;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			f[i][j][0] = f[i][j][1] = g[i][j] = 1e8;
			vis[i][j][0] = vis[i][j][1] = mk[i][j] = 0;
			h[i][j][0] = h[i][j][1] = mkp(0, 0);
			if (s[i][j] == 'V') {
				sx = i;
				sy = j;
			}
		}
	}
	queue<node> q;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if ((i == 1 || i == n || j == 1 || j == m) && s[i][j] != '#') {
				f[i][j][0] = 0;
				vis[i][j][0] = 1;
				h[i][j][0] = mkp(i, j);
				q.emplace(i, j, 0);
			}
		}
	}
	while (q.size()) {
		int x = q.front().x, y = q.front().y, o = q.front().o;
		q.pop();
		for (int i = 0; i < 4; ++i) {
			int nx = x + dx[i], ny = y + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > m || s[nx][ny] == '#') {
				continue;
			}
			if (!h[nx][ny][0].fst) {
				h[nx][ny][0] = h[x][y][o];
				f[nx][ny][0] = f[x][y][o] + 1;
				q.emplace(nx, ny, 0);
			} else if (h[nx][ny][0] != h[x][y][o] && !h[nx][ny][1].fst) {
				h[nx][ny][1] = h[x][y][o];
				f[nx][ny][1] = f[x][y][o] + 1;
				q.emplace(nx, ny, 1);
			}
		}
	}
	static pii Q[maxn * maxn];
	int hd = 1, tl = 0;
	Q[++tl] = mkp(sx, sy);
	mk[sx][sy] = 1;
	g[sx][sy] = 0;
	while (hd <= tl) {
		int x = Q[hd].fst, y = Q[hd].scd;
		++hd;
		for (int i = 0; i < 4; ++i) {
			int nx = x + dx[i], ny = y + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > m || s[nx][ny] == '#') {
				continue;
			}
			if (!mk[nx][ny]) {
				mk[nx][ny] = 1;
				g[nx][ny] = g[x][y] + 1;
				Q[++tl] = mkp(nx, ny);
			}
		}
	}
	int cnt = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if (i == 1 || i == n || j == 1 || j == m) {
				cnt += mk[i][j];
			}
		}
	}
	int t = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			t += (s[i][j] == '.');
		}
	}
	if (cnt == 0) {
		printf("%d\n", t);
		return;
	}
	if (cnt == 1) {
		int mn = 1e8;
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= m; ++j) {
				if (i == 1 || i == n || j == 1 || j == m) {
					mn = min(mn, g[i][j]);
				}
			}
		}
		printf("%d\n", t - mn);
		return;
	}
	int ans = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			ans = max(ans, t - g[i][j] - f[i][j][0] - f[i][j][1]);
		}
	}
	printf("%d\n", ans);
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

posted @ 2023-11-23 10:46  zltzlt  阅读(22)  评论(0编辑  收藏  举报