HDUOJ 1026 Ignatius and the Princess I(优先队列)(广搜)


优先队列广搜,经典广搜好题,建议都做做

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=110;
struct node{
	int x,y;
	int time;
	bool operator<(const node &b) const{
	return b.time<time;
	}
};
struct Pre{
	int px;
	int py;
}pre[maxn][maxn];
int dr[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int mp[maxn][maxn];
char str[maxn];
int visit[maxn][maxn];
int n,m;
const int INF=0x3f3f3f3f;


void bfs()
{
	node st,ed;
	priority_queue<node> q;
	while(!q.empty()) q.pop();

    st.x=n;st.y=m;
    st.time=mp[n][m];
    pre[n][m].px=-1;
    q.push(st);

    memset(visit,0,sizeof(visit));
    visit[n][m]=1;

    while(!q.empty())
    {
    	st=q.top();
    	q.pop();
    	if(st.x==1&&st.y==1)
    	{
    	     printf("It takes %d seconds to reach the target position, let me show you the way.\n",st.time);
			 int time=1;
			 int x=st.x,y=st.y;
			 int nx=pre[x][y].px,ny=pre[x][y].py;
			 while(pre[x][y].px!=-1)
			 {
			 	printf("%ds:(%d,%d)->(%d,%d)\n",time++,x-1,y-1,nx-1,ny-1);
			 	while(mp[nx][ny]--)
			 		printf("%ds:FIGHT AT (%d,%d)\n", time++, nx-1, ny-1);
			 	x=nx;y=ny;
			 	nx=pre[x][y].px;
			 	ny=pre[x][y].py;
			 }
			 printf("FINISH\n");
			 return ;
    	}
    	for(int i=0;i<4;i++)
    	{
    		ed.x=st.x+dr[i][0];
    		ed.y=st.y+dr[i][1];
    		if(mp[ed.x][ed.y]>=0&&!visit[ed.x][ed.y])
    		{
    			visit[ed.x][ed.y]=1;
    			ed.time=st.time+1+mp[ed.x][ed.y];
    			pre[ed.x][ed.y].px=st.x;
    			pre[ed.x][ed.y].py=st.y;

    			q.push(ed);
    		}
    	}
    }
    printf("God please help our poor hero.\n");
    printf("FINISH\n");
    return ;
}
int main()
{
    while(scanf("%d%d", &n,&m) != EOF)
    {
        gets(str);
        for(int i = 0; i <= n+1; i++)
            for(int j = 0; j <= m+1; j++)
                mp[i][j] = -1;

        char c;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%c", &c);
                if(c != 'X')
                {
                    if(c == '.') mp[i][j] = 0;
                    else mp[i][j] = c-'0';
                }
            }
            gets(str);
        }

        bfs();
    }
    return 0;
}
全部评论

相关推荐

02-18 17:30
腾讯_TEG_技术
多刷**&nbsp;背八股&nbsp;刷面经&nbsp;项目话术准备好&nbsp;不会差的!!!后台看到好多小伙伴们都出现其中一个环节的错误,,,可惜了抓紧机会吧&nbsp;有的是hc&nbsp;但缺的就是稍微用心的人
野猪不是猪🐗:多刷星星,背八股背话术,真的能过你们?对一个个没实习过的学生狂问场景题设计题和底层深挖,别以为我不知道一边说缺人还一边各种kpi面
点赞 评论 收藏
分享
点赞 评论 收藏
分享
mq2:我倒是觉得这种敞亮一点好。能接受就去不能就不去呗。 完了跟现在“正常”公司一样,hr说的天花乱坠,进去一看根本就是996核动力牛马,想走又没应届生身份了。岂不是更糟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务