社区讨论

求救 样例多个(2,6)->

P1238走迷宫参与者 3已保存回复 8

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
8 条
当前快照
1 份
快照标识符
@mi1vd6gt
此快照首次捕获于
2025/11/16 23:27
3 个月前
此快照最后确认于
2025/11/17 09:09
3 个月前
查看原帖
CPP

#include<iostream>
using namespace std;

const int N = 110;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int grid[N][N];
bool vis[N][N] = {false};
int n, m;
bool ok = false;
void dfs(int x, int y) {
	if (ok) return;

	if (x == n - 1 && y == m - 1) {
		ok = true;
		return;
	}

	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] == 1 && !vis[nx][ny]) {
			vis[nx][ny] = true;
			if (x != n - 1 && y != m - 1) {
				cout << "(" << nx + 1 << "," << ny + 1 << ")->";
			} else {
				cout << "(" << nx + 1 << "," << ny + 1 << ")";
			}
			dfs(nx, ny);
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
		}
	}
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	cout << "(1,1)->";
	vis[0][0] = true;
	dfs(0, 0);
	return 0;
}

回复

8 条回复,欢迎继续交流。

正在加载回复...