76 pts求助
查看原帖
76 pts求助
227723
syysongyuyang楼主2021/11/27 17:06
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int sx,sy,ex,ey;
const int N=10;
const int HP=6;
struct node
{
    int x,y;
    int time,hp;
};
queue <node> q;
int ans=0x7fffffff;
int map[N][N];
int n,m;
int dx[5]={1,-1,0,0};
int dy[5]={0,0,1,-1};
int vis[N][N];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=m;j++)
        {
            cin>>map[j][i];
            if (map[j][i]==2)
            {
                sx=j;
                sy=i;
                continue;
            }
            if (map[j][i]==3)
            {
                ex=j;
                ey=i;
                continue;
            }
            if (map[j][i]==0)
                vis[j][i]=1;
        }
    }
    node start={sx,sy,0,HP};
    node end={ex,ey};
    q.push(start);
    while (!q.empty())
    {
        node now=q.front();
        q.pop();
        for (int i=0;i<4;i++)
        {
            int tx=now.x+dx[i];
            int ty=now.y+dy[i];
            int time=now.time;
            int hp=now.hp;
            if (tx>0 && tx<=m && ty>0 && ty<=n && vis[tx][ty]==0)
            {
                vis[tx][ty]=1;
                hp--;
                time++;
                if (hp==1 && map[tx][ty]!=4)
                {
                    cout<<"-1";
                    return 0;
                }
                else if (hp==1 && map[tx][ty]==4)
                {
                    hp=6;
                    node new_one={tx,ty,time,hp};
                    q.push(new_one);
                    continue;
                }
                else if (map[tx][ty]==4)
                {
                    hp=6;
                    node new_one={tx,ty,time,hp};
                    q.push(new_one);
                    continue;
                }
                if (tx==end.x && ty==end.y)
                {
                    ans=min(ans,time);
                    vis[tx][ty]=0;
                    continue;
                }
                node next={tx,ty,time,hp};
                q.push(next);
            }
        }
    }
    if (ans==0x7fffffff)
		cout<<"-1";
	else cout<<ans;
    return 0;
}

调了半天了,求求大佬帮帮吧

2021/11/27 17:06
加载中...