求调试
  • 板块P1238 走迷宫
  • 楼主skiyalg
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/25 15:28
  • 上次更新2024/11/25 19:16:49
查看原帖
求调试
6828
skiyalg楼主2024/11/25 15:28

#include <bits/stdc++.h>

#include <bits/stdc++.h>
using namespace std;
const int N = 16;
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};
int a[N][N];
bool vis[N][N];
int n,m,sx,sy,ex,ey,k=1,tmp;
struct node{
	int x,y;
}ans[N*N];
void pr(){
	for(int i = 1; i < k-2 ;i++){
		cout << "("<<ans[i].x << ","<< ans[i].y<< ")->";
	}
	cout << "("<< ans[k-1].x << ","<< ans[k-1].y<< ")" << endl;
	tmp = 1;
	return ;
}
void dfs(int x,int y){
	if (x == ex && y == ey){
		ans[k++] = {x,y};
		pr();
		return ;
	}
	for(int i = 0; i < 4; i++){
		int newx = x + dx[i];
		int newy = y + dy[i];
		if (a[newx][newy] && !vis[newx][newy]){
			vis[newx][newy] = 1;
			ans[k++] = {newx,newy};
			dfs(newx,newy);
			k--;
			vis[newx][newy] = 0;
		}
	}
} 
int main(){
	
	cin >> m >> n;
	for(int i = 1; i <= m; i++){
		for(int j = 1; j <= n; j++)
			cin >> a[i][j];
	} 
	cin >> sx >> sy >> ex >> ey;
	ans[k++] = {sx,sy};
	vis[sx][sy] = 1;
	dfs(sx,sy);
	if (tmp == 0) cout << -1;
	return 0;
}

2024/11/25 15:28
加载中...