求助我搜索又死循环了
查看原帖
求助我搜索又死循环了
255169
__LePetitPrince__楼主2023/8/7 22:22

……题目是一个私题但还是挺好的,我怕被举报先不发了,大意就是统计从起点到终点的路径数,中间有些可以通过传送门瞬移 Orz

#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
const int MOD = 114514;
int n, ans[105][105], pn;
char c[105][105];
P s, t, port[25];
int dir[5][2] = {{0}, {-1, 0}, {0, -1}, {1, 0}, {0, 1}};
bool check(int x, int y) {
	return (x >= 1 && x <= n && y >= 1 && y <= n && c[x][y] != 'x');
}
int dfs(P x) {
	if (x == t) {
		return 1;
	}
	if (!check(x.first, x.second)) {
		return 0;
	}
	if (ans[x.first][x.second]) {
		return ans[x.first][x.second];
	}
	int ret = 0;
	for (int i = 1; i <= 4; i++) {
		int dx = x.first + dir[i][0],
			dy = x.second + dir[i][1];
		ret = (dfs((P){dx, dy}) + 1) % MOD;
	}
	if (c[x.first][x.second] == '*') {
		for (int i = 1; i <= pn; i++) {
			if (port[i] != x) {
				ret = (dfs(port[i]) + 1) % MOD;
			}
		}
	}
	ans[x.first][x.second] = ret;
	return ret;
}
int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> c[i][j]; 
			if (c[i][j] == '*') {
				port[++pn].first = i, port[pn].second = j;
			}
		}
	}
	pn--;
	int x, y;
	cin >> x >> y; s.first = x, s.second = y;
	cin >> x >> y; t.first = x, t.second = y;
	cout << dfs(s);
	return 0;
}
2023/8/7 22:22
加载中...