首页 > 其他分享 >CodeForces 1898F Vova Escapes the Matrix

CodeForces 1898F Vova Escapes the Matrix

时间:2023-11-23 10:57:02浏览次数:48  
标签:typedef Matrix int CodeForces long nx ny 1898F maxn

洛谷传送门

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;
}

标签:typedef,Matrix,int,CodeForces,long,nx,ny,1898F,maxn
From: https://www.cnblogs.com/zltzlt-blog/p/17851067.html

相关文章

  • Codeforces Round 697 (Div. 3)
    A.OddDivisor#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128#definedoublelongdoubletypedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string>PSS;constintN=......
  • [Codeforces] CF1475C Ball in Berland 题解
    BallinBerland-洛谷题意在毕业典礼上,有​个男孩和​个女孩准备跳舞,不是所有的男孩和女孩都准备结伴跳舞。现在你知道​个可能的舞伴,你需要选择其中的两对,以便使没有人重复地出现在舞伴里,求可能的数量。思路暴力最朴素,也是简单的方法,就是通过暴力组合进行配对。#include......
  • Codeforces Round 905 (Div. 2)
    \(A.Chemistry\)https://codeforces.com/contest/1888/submission/233505834\(B.Raspberries\)https://codeforces.com/contest/1888/submission/233506474\(C.YouAreSoBeautiful\)题意:给定一个长\(n\)的序列\(a\)。对于区间\([l,r]\),如果\(a\)没有其它子序列(......
  • Codeforces Round 910 E
    tilian我们发现可以通过交换相邻两个的方式让字典序小的任意移动我们目标串t要是t[0]为c我们肯定是找到第一个合法的c的位置每次去找合法并且最优的那么哪些是不合法的呢比如我比c小的a,b位置还在第一个c前肯定就不能用了我们用26个set维护这个过程即可voidsolve()......
  • Codeforces Round 909 (Div. 3)
    CodeforcesRound909(Div.3)A.GamewithIntegers题意:给定一个数\(x\),\(A,B\)两人轮流进行操作,\(A\)先操作。每次给\(x\)加一或者减一,操作完后\(x\%3==0\)者获胜。判断获胜者。解题思路:判断\(A\)操作完是否能获胜,如果不能,那么一定是\(B\)获胜。代码:#include<bit......
  • codeforces 50题精选训练
    本章节参考:2020,2021年CF简单题精选-题单-洛谷|计算机科学教育新生态(luogu.com.cn) T1:首先,很容易观察到点的一些特征:-都在第一象限;-点的分布越来越稀疏。以样例为例:   还有无限个点没有画出来。根据点的分布越来越稀疏的特性,能不能发现收集点的规......
  • Codeforces Round 905 (Div. 3) ABCDEG1
    CodeforcesRound905(Div.3)ABCDEG1A.Morning思路:签到,直接模拟。//AConemoretimes//nndbk#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintmod=1e9+7;constintN=2e5+10;intmain(){ios::sync_with_stdio(fal......
  • Educational Codeforces Round 99 (Rated for Div. 2)
    https://codeforces.com/contest/1455很久没有vp了,感觉思维又僵化了A题直接看样例,直接猜是长度。B题首先如果是\(x=\frac{n(n+1)}{2}\),那么就是n否则如果\(x=\frac{n(n+1)}{2}+y\),分成两类y=n,ans=n+2,y<n,我们总可以找到前面一个替换,然后恰好的到n,选取z=n-y即可C题感觉比B......
  • Educational Codeforces Round 156 (Rated for Div. 2) ABCD
    EducationalCodeforcesRound156(RatedforDiv.2)ABCDA.SumofThree题意:给定正整数\(n\),判断是否存在正整数\(a\),\(b\),\(c\)满足:\(a+b+c=n\)。\(a\),\(b\),\(c\)均不是\(3\)的倍数。如存在,输出YES并构造一组方案,否则输出NO。思路:法一:我们分类讨论。根据......
  • Codeforces Round 904 (Div. 2)
    \(A.SimpleDesign\)https://codeforces.com/contest/1884/submission/233628914\(B.HauntedHouse\)https://codeforces.com/contest/1884/submission/233629446\(C.MediumDesign\)https://codeforces.com/contest/1884/submission/233632930\(D.Counting......