有没有深搜的,求助
查看原帖
有没有深搜的,求助
1076328
YHL_RP楼主2024/12/18 19:23

我的代码: 10分,其他全超时

#include<bits/stdc++.h>
using namespace std;

const int N = 1005;
int n, dt[N][N];
int x1, x2, y1s, y2s, ans = INT_MAX;
int GoX[4] = {0, -1, 0, 1};
int GoY[4] = {-1, 0, 1, 0};

void dfs(int x, int y, int level) {
	if (x > n || x < 1 || y < 1 || y > n || dt[x][y] == 1) {
		return;
	}
	if (x == x2 && y == y2s) {
		ans = min(ans, level);
	}
	dt[x][y] = 1;
	for (int i = 0; i < 4; i++) {
		dfs(x + GoX[i], y + GoY[i], level + 1);
	}
	dt[x][y] = 0;
	return;
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			char temp;
			cin >> temp;
			dt[i][j] = temp - '0';
		}
	}
	cin >> x1 >> y1s >> x2 >> y2s;
	dfs(x1, y1s, 0);
	cout << ans;
	return 0;
}

懒得用宽

2024/12/18 19:23
加载中...