求调,10分,壶关
查看原帖
求调,10分,壶关
1346089
Celestial_Lock楼主2025/8/30 20:48

me样例过了才十分…… 望各位大佬帮蒟蒻一把

DFS调不出来了

#include<bits/stdc++.h>
using namespace std;
#define M 51
#define N 200
int n,m,sx,sy,fx,fy;
int cnt=0;
int g[M][M];
int a[N],b[N];
bool vis[M][M],f;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
bool check(int x,int y){
	return x<=n && y<=m && x>=1 && y>=1 && vis[x][y]==0 && g[x][y]==1;
}
void dfs(int x,int y,int s)
{
	if(x==fx && y==fy)
	{
 		for(int i=1;i<s;i++)
 			cout<<"("<<a[i]<<","<<b[i]<<")->";
        cout<<"("<<fx<<","<<fx<<")"<<endl;
        f=1;
		return ;
	}
	a[s]=x;
	b[s]=y;
	for(int i=0;i<4;i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(check(tx,ty))
		{
			vis[tx][ty]=1;
			dfs(tx,ty,s+1);
			vis[tx][ty]=0;
		}
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>g[i][j];
	cin>>sx>>sy;
	cin>>fx>>fy;
	f=0;
	dfs(sx,sy,0);
	if(!f)
		cout<<-1<<'\n';
	return 0;
}


2025/8/30 20:48
加载中...