跳到正文

题解

AT_abc341_c题解

lkrqwq约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年2月18日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟

版本

  1. v1当前

昨天我过度逆天 ,切了 C 题却没切掉 B 题。

这道题思路十分简单,我们要循环遍历每一个点,当检查到这个点是陆地时,根据字符串模拟。当走到海洋时,这条路就是不可行的;否则,我们将答案加 11,表示新走到了一个点。

当遍历完所有点后,算法结束,总时间复杂度 O(HWN)\mathcal{O}(HWN)。

代码如下(我用了搜索,后来发现没必要,还浪费时间):

C++
#include <iostream>
#include <map>
using namespace std;
int h, w, n;
string a[1000];
bool vis[501][501];
map <pair <int, int>, int> cnt;
map <char, int> dx, dy;
string target;
int ans = 0;
void dfs(int x, int y, int s) {
	if (target[s] == '\0') {
		ans++;
		// cout << x << " " << y;
		return;
	}
	int xx = x + dx[target[s]], yy = y + dy[target[s]];
	if (xx < 1 || xx > h || yy < 1 || yy > w || a[xx][yy] == '#') return;
	dfs(xx, yy, s + 1);
}
int main() {
	dx = { {'L',0},{'R',0},{'U',-1},{'D',1} };
	dy = { {'L',-1},{'R',1},{'U',0},{'D',0} };
	cin >> h >> w >> n;
	cin >> target;
	for (int i = 1; i <= h; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= h; i++) {
		for (int j = 0; j < a[i].size(); j++) {
			if (a[i][j] == '.') {
				dfs(i, j, 0);
			}
		}
	}
	cout << ans;
}

提交记录

第一篇 Atcoder 题解,望管理员审核通过~

原文