ac了,抄的吱一声
查看原帖
ac了,抄的吱一声
1110408
mayuhao666楼主2024/8/9 14:42
#include <bits/stdc++.h>
using namespace std;

int m, n, a[100][100], b[100][100], qx, qy, zx, zy, zb[10000], s;

int hb[10000], sum = 1, jx[100] = {0, 0, -1, 0, +1}, jy[100] = {0, -1, 0, +1, 0};

void dfs (int x, int y) {
	if (x == zx && y == zy) {
		cout << '(' << qx << ',' << qy << ')';
		for (int i = 1; i < sum; i++)
			cout << "->(" << zb[i] << ',' << hb[i] << ')' ;
		s++;
		cout << '\n';
	} else {
		for (int i = 1; i <= 4; i++) {
			int nx = x + jx[i];
			int ny = y + jy[i];
			if (nx <= m && nx >= 1 && ny <= n && ny >= 1 && a[nx][ny] == 1 && b[nx][ny] == 0) {
				b[x][y] = 1;
				zb[sum] = nx;
				hb[sum] = ny;
				sum++;
				dfs(nx, ny);
				b[x][y] = 0;
				sum--;

			}
		}
	}
}

int main () {
	cin >> m >> n;
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	cin >> qx >> qy >> zx >> zy;
	dfs(qx, qy);
	if (s == 0)
		cout << -1;
	return 0;
}
2024/8/9 14:42
加载中...