【玄 3 关哦】帮忙改一下代码
  • 板块灌水区
  • 楼主littlesnake
  • 当前回复8
  • 已保存回复8
  • 发布时间2024/5/16 21:02
  • 上次更新2024/5/16 23:06:55
查看原帖
【玄 3 关哦】帮忙改一下代码
574745
littlesnake楼主2024/5/16 21:02

rt.CE了,因为已经取地址了不能调用,怎么改才能逃脱 CE?

/*
对于每次送货:
1. BFS 从 (1,1) 到最近的商铺
2. 再从商铺 BFS 到顾客家中
*/
# include <bits/stdc++.h>

# define N 210

using namespace std;

int n, m, gktot, ans, ANS, x = 1, y = 1, X, Y;
int mp[N][N];
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

struct Edge {
	int xt, yt;
} gk[N * N];

bool vis[N][N];

void DFS1 (int &x, int &y, int &ans) {
	if (!mp[x][y]) return ;
	vis[x][y] = 1;
	for (int i = 0; i < 4; i ++) {
		int fx = x + dx[i], fy = y + dy[i];
		if (fx < 0 || fx > n || fy < 0 || fy > n || vis[fx][fy]) continue;
		DFS1(fx, fy, ans + 1);
	}
}

void DFS2 (int &x, int &y, int &ans) {
	if (x == X && y == Y) return ;
	vis[x][y] = 1;
	for (int i = 0; i < 4; i ++) {
		int fx = x + dx[i], fy = y + dy[i];
		if (fx < 0 || fx > n || fy < 0 || fy > n || vis[fx][fy]) continue;
		DFS2(fx, fy, ans + 1);
	}
}

int main () {
	
	cin >> n >> m;
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= m; j ++) {
			cin >> mp[i][j];
			if (!mp[i][j]) gk[++ gktot] = {i, j};
		}
	}
	ANS = 0;
	for (int i = 1; i <= gktot; i ++) {
		X = gk[i].xt, Y = gk[i].yt;
		ans = 0;
		memset (vis, 0, sizeof(vis));
		DFS1 (x, y, ans);
		memset (vis, 0, sizeof(vis));
		DFS2 (x, y, ans);
		ANS += ans;
	}
	cout << ANS;
		
	return 0;
}
2024/5/16 21:02
加载中...