40分求助
查看原帖
40分求助
874676
silentzdw楼主2023/6/25 17:44
#include <bits/stdc++.h>
using namespace std;
char a[110][110];
int n, m;

int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
bool st[110][110];

bool dfs(int x, int y) {
	if (x == n && y == m) {
		return true;
	}
	for (int k = 0; k < 4; k++) {
		int tx = x + dir[k][0];
		int ty = y + dir[k][1];
		if (tx < 1 || tx > n || ty < 1 || ty > m || a[tx][ty] == '#') {
			continue;
		}
		if (!st[tx][ty]) {
			st[tx][ty] = true;
			if (dfs(tx, ty)) return true;
			st[tx][ty] = false;
		}
	}
	return false;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	if (dfs(1, 1)) {
		cout << "Yes" << "\n";
	} else {
		cout << "No" << "\n";
	}
	return 0;
}

2023/6/25 17:44
加载中...