52分求调
  • 板块P2802 回家
  • 楼主XXCCVV
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/10/26 13:16
  • 上次更新2023/10/27 05:49:28
查看原帖
52分求调
638832
XXCCVV楼主2022/10/26 13:16
#include<iostream>
#include<queue>
using namespace std;
struct node{
	int x,y,step;
	int HP;
};
queue<node>q;
int n,m,a[10][10],vis[10][10];
int ex,ey,sx,sy;
short dxx[4]={1,0,-1,0};
short dyy[4]={0,1,0,-1};
bool flag=1;
void bfs(){
	while(!q.empty()){
		node temp;
		temp=q.front();
		q.pop();
		if(temp.x==ex&&temp.y==ey){
			cout<<temp.step;
			flag=0;
			return;
		}
		if(temp.HP==0){
			cout<<-1;
			return ;
		}
		for(int i=0;i<4;i++){
			int dx=temp.x+dxx[i];
			int dy=temp.y+dyy[i];
			if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&!vis[dx][dy]&&a[dx][dx]==1||a[dx][dy]==3||a[dx][dy]==4){
				node v;
				v.x=dx;
				v.y=dy;
				v.step=temp.step+1;
				v.HP=temp.HP-1;
				q.push(v);
				vis[dx][dy]=1;
				if(a[dx][dy]==4&&v.HP<0){
					q.front().HP=6;
				}
			}
		}
	}
}
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){
				sx=i;
				sy=j;
			}
			if(a[i][j]==3){
				ex=i;
				ey=j;
			}
		}
	}
	node v;
	v.x=sx;
	v.y=sy;
	v.step=0;
	v.HP=6;
	q.push(v);
	bfs();
	if(flag){
		cout<<-1;
	}
	return 0;
}
2022/10/26 13:16
加载中...