求助!84分两个wa
  • 板块P2802 回家
  • 楼主Officer
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/20 21:49
  • 上次更新2024/5/20 22:22:57
查看原帖
求助!84分两个wa
872386
Officer楼主2024/5/20 21:49
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,step,dep;
};
int n,m;
int a[10][10];
bool vis[10][10];
int ax,ay,sx,sy;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void bfs(int x,int y)
{
	queue<node> q;
	q.push(node{x,y,0,6});
	vis[x][y]=1;
	while(!q.empty())
	{
		node u=q.front();
		q.pop();
		if(u.x==sx&&u.y==sy)
		{
			cout<<u.step<<endl;
			return;
		}
		for(int i=0;i<4;i++)
		{
			int bx=u.x+dir[i][0];
			int by=u.y+dir[i][1];
			if(bx<1||bx>n||by<1||by>m)
			{
				continue;
			}
			if(a[bx][by]==0)
			{
				continue;
			}
			if(u.dep==1)
			{
				continue;
			}
			if(vis[bx][by])
			{
				continue;
			}
			vis[bx][by]=1;
			if(a[bx][by]==4)
			{
				q.push(node{bx,by,u.step+1,6});
			}
			else
			{
				q.push(node{bx,by,u.step+1,u.dep-1});
			}
		}
	}
	cout<<-1<<endl;
	return;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
			if(a[i][j]==2)
			{
				ax=i;
				ay=j;
			}
			if(a[i][j]==3)
			{
				sx=i;
				sy=j;
			}
		}
	}
	bfs(ax,ay);
    return 0;
}
/*
*/

2024/5/20 21:49
加载中...