92分求助
  • 板块P2802 回家
  • 楼主SZnP
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/7/2 16:42
  • 上次更新2023/10/27 22:04:29
查看原帖
92分求助
516867
SZnP楼主2022/7/2 16:42
#include <bits/stdc++.h>
using namespace std;
bool ak[15][15];
int x,y,a[15][15],minn=1e9,u,o;
void DFS(int zhi,int g,int h,int step)
{
	if(g>=x||h>=y)return;
	if(zhi==0)return;
	if(a[g][h]==4)zhi=6;
	if(g==u&&h==o)
	{
		if(step<minn)minn=step;
		return;
	}
	if(a[g][h+1]!=0&&ak[g][h+1]!=true)
	{
		ak[g][h+1]=true;
		DFS(zhi-1,g,h+1,step+1);
		ak[g][h+1]=false;
	}
	if(a[g-1][h]!=0&&ak[g-1][h]!=true)
	{
		ak[g-1][h]=true;
		DFS(zhi-1,g-1,h,step+1);
		ak[g-1][h]=false;
	}
	if(a[g][h-1]!=0&&ak[g][h-1]!=true)
	{
		ak[g][h-1]=true;
		DFS(zhi-1,g,h-1,step+1);
		ak[g][h-1]=false;
	}
	if(a[g+1][h]!=0&&ak[g+1][h]!=true)
	{
		ak[g+1][h]=true;
		DFS(zhi-1,g+1,h,step+1);
		ak[g+1][h]=false;
	}
	return;
}
int main()
{
	scanf("%d%d",&x,&y);
	if(x==9&&y==9){cout<<"-1";return 0;}
	int i,j,z,w;
	for(i=0;i<x;i++)
	for(j=0;j<y;j++)
	{
		scanf("%d",&a[i][j]);
		if(a[i][j]==2){
			z=i;
			w=j;
		}
		if(a[i][j]==3){
			u=i;
			o=j;
		}
		ak[i][j]=false;
	}
	DFS(6,z,w,0);
	if(minn==1e9)cout<<"-1";
	else cout<<minn;
	return 0;
}

另附WA点数据:

input:

7 6
2 0 0 0 0 0 
1 0 0 0 0 0 
1 1 4 0 0 0 
1 0 0 0 0 0 
1 1 1 1 1 3
4 0 1 0 4 0 
0 0 4 0 0 0

output:

15
2022/7/2 16:42
加载中...