9和11点错误,求大佬帮助
  • 板块P2802 回家
  • 楼主LiXingWang
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/3/25 09:22
  • 上次更新2023/11/5 01:39:06
查看原帖
9和11点错误,求大佬帮助
489506
LiXingWang楼主2021/3/25 09:22
# include <bits/stdc++.h>
using namespace std;
const int maxn = 15;
int n,m;
int vis[maxn][maxn],a[maxn][maxn],fx[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int sx,sy,tx,ty;
int ans = 1e5;
int cnt = 0;
int hp = 6;
bool in_mg(int x,int y)
{
	return x>=0&&x<n&&y>=0&&y<m;
}
void dfs(int x,int y)
{
	vis[x][y] = 1;
	if(a[x][y] == 4) hp = 6;
	if(hp == 1)
	return ;
	if(x == tx&&y == ty)
	{
		if(ans > cnt)
		{
			ans = cnt;
			return ;
		}
	}
	int i;
	for(i = 0;i<4;i++)
	{
		int nx = x+fx[i][0];
		int ny = y+fx[i][1];
		if(in_mg(nx,ny)&&a[nx][ny]!=0&&vis[nx][ny]!=1)
		{
			hp--;
			cnt++; 
			dfs(nx,ny);
			vis[nx][ny] = 0;
			hp++;
			cnt--;
		}
	}
}
int main ()
{
	cin>>n>>m;
	for(int i = 0;i<n;i++)
	{
		for(int j  = 0;j<m;j++)
		{
			cin>>a[i][j];
			if(a[i][j] == 2)
			sx = i,sy = j;
			if(a[i][j] == 3)
			tx = i,ty = j;
		}
	}
	dfs(sx,sy);
	if(ans != 1e5)
	cout<<ans<<endl;
	else
	cout<<-1<<endl;
}
2021/3/25 09:22
加载中...