求调,悬关
查看原帖
求调,悬关
834542
HuangGuangWu666楼主2024/12/24 11:42
#include<bits/stdc++.h>
using namespace std;
struct s{
	int x,y;
};
int a[15][15];
int n,m,k,k1,z,z1;
deque<s> q;
bool falg=1;
int dx[]={0,0,-1,0,1},dy[]={0,-1,0,1,0};
int cnt=1;
bool vis[15][15];
void print(){
    falg=0;
	for(int i=1;i<=cnt;i++){
		s tmp=q.front();
		if(i==1) cout<<"("<<tmp.x<<","<<tmp.y<<")";
		else cout<<"->"<<"("<<tmp.x<<","<<tmp.y<<")";
		q.pop_front();
		q.push_back(tmp);
	}
	cout<<endl;
}
void dfs(int x,int y){
	if(x==z&&y==z1){
		print();return;
	}
	for(int i=1;i<=4;i++){
		int xx=x+dx[i],yy=y+dy[i];
		if(xx>n||xx<1||yy>m||yy<1||a[xx][yy]==0||vis[xx][yy]==1) continue;
		q.push_back({xx,yy});
		vis[xx][yy]=1;
		cnt++;
		dfs(xx,yy);
		cnt--;
		vis[xx][yy]=0;
		q.pop_back();
	}
}
int main() {
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
		}
	}
	cin>>k>>k1>>z>>z1;q.push_back({k,k1});
	dfs(k,k1);
	if(falg) cout<<-1<<endl;
	return 0;
} 
/*
12 9
1 1 0 0 1 1 1 0 1
0 1 1 1 1 0 1 1 1
0 0 0 0 1 1 0 0 0
1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 1 1 1
1 1 1 1 1 0 0 1 0
0 1 0 0 0 0 1 0 1
0 1 1 1 1 0 1 1 1
0 0 1 1 0 1 0 1 0
0 0 0 1 1 0 1 0 1
0 0 0 0 1 1 1 1 1
1 1 1 1 1 1 0 0 0
1 1
12 5
*/ 
2024/12/24 11:42
加载中...