#16样例WA 95pts求调
查看原帖
#16样例WA 95pts求调
635711
linshangyun楼主2024/10/7 11:57
#include <bits/stdc++.h>
using namespace std;
struct node {
	int x, y;
}ans[500];
int n, m;
const int N =25, M = 25;
int a[N][M], sx, sy, ex, ey;
int dx[4] = {0,-1,0,1};
int dy[4] = {-1,0,1,0};
int flag = 0;
void print(int nn) {
	flag = 1;
	for (int i = 0; i < nn-1; i++) {
		cout << "(" << ans[i].x << "," << ans[i].y << ")" << "->";
	}
	cout << "(" << ans[nn-1].x << "," << ans[nn-1].y << ")" << endl;
}
void dfs (int x, int y, int sum) {
	if (x == ex-1 && y == ey-1) {
		print(sum);
	}
	for (int i = 0; i < 4; i++) {
		a[x][y] = 0;
		int xx = x+dx[i], yy = y+dy[i];
		if (a[xx][yy]) {
			ans[sum].x = xx+1, ans[sum].y = yy+1;
			dfs(xx,yy,sum+1);
		}
		a[x][y] = 1;
	}
}
int main () { 
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
		}
	}
	cin >> sx >> sy;
	ans[0].x = sx, ans[0].y = sy;
	cin >> ex >> ey;
	dfs(sx-1, sy-1, 1);
	if (flag == 0) {
		cout << "-1";
	}
	return 0;
} 
2024/10/7 11:57
加载中...