WA 25pts求助
  • 板块P1238 走迷宫
  • 楼主shuaiqbr
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/1/23 20:20
  • 上次更新2024/1/23 23:55:22
查看原帖
WA 25pts求助
974005
shuaiqbr楼主2024/1/23 20:20
#include<bits/stdc++.h>
using namespace std;
const int sx[]={0,1,-1,0},sy[]={-1,0,0,1};
struct node{
	int nx,ny;
}q[10000];
int n,m,puz[100][100],startx,starty,endx,endy,cnt;
bool st[100][100];
void dfs(int x,int y,int h)
{
	st[x][y]=1;
	q[h].nx=x;
	q[h].ny=y;
	if(x==endx&&y==endy)
	{
		cnt++;
		for(int i=1;i<=h;i++)
		{
			printf("(%d,%d)",q[i].nx,q[i].ny);
			if(i!=h) cout<<"->";
		}
		cout<<endl;
		st[x][y]=0;
		return;
	}
	for(int i=0;i<4;i++)
	{
		int a=x+sx[i],b=y+sy[i];
		if(!st[a][b]&&puz[a][b])
		{
			dfs(a,b,h+1);
		}
	}
	st[x][y]=0;
	h--;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>puz[i][j];
		}
	}
	cin>>startx>>starty>>endx>>endy;
	dfs(startx,starty,1);
	if(cnt==0) cout<<-1;
}
/*
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
*/

2024/1/23 20:20
加载中...