站外题糖果迷宫求调(有题面),玄关
  • 板块灌水区
  • 楼主Jimmy_Cao
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/21 22:07
  • 上次更新2024/11/22 11:26:04
查看原帖
站外题糖果迷宫求调(有题面),玄关
1351065
Jimmy_Cao楼主2024/11/21 22:07

我的代码(连样例都过不了):

#include <bits/stdc++.h>
using namespace std;
int n, m, dn, c, maze[55][55], cs[55][55], ans;
int mov[2][2] = {0, 1, 1, 0};
void dfs(int x, int y) {
	for (int i = 0; i < 2; i++) {
		int xx = x + mov[i][0];
		int yy = y + mov[i][1];
		//cout << xx << ' ' << yy << endl;
		if (xx <= n && yy <= m) {
			c += maze[xx][yy];
			if (xx == n && yy == m) {
				ans = max(ans, c);
				c -= maze[xx][yy];
				return;
			}
			if (cs[xx][yy] && cs[xx][yy] != -1) {
				for (int i = 1; i <= n; i++) 
					for (int j = 1; j <= m; j++) 
						if (cs[i][j] && i != xx && j != yy) {
							for (int x = 1; x <= n; x++) 
								for (int y = 1; y <= m; y++) 
									if (cs[x][y] == 1) cs[x][y] = -1;
							dfs(i, j);
							for (int x = 1; x <= n; x++) 
								for (int y = 1; y <= m; y++) 
									if (cs[x][y] == -1) cs[x][y] = 1;
						} 
			}
			dfs(xx, yy);
			c -= maze[xx][yy];
		}
	}
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) cin >> maze[i][j];
	cin >> dn;
	for (int i = 1; i <= dn; i++) {
		int x, y;
		cin >> x >> y;
		cs[x][y] = 1;
	} 
	cin >> c;
	dfs(1, 1);
	if (ans) cout << ans;
	else cout << -1;
	return 0;
}
2024/11/21 22:07
加载中...