zoukankan      html  css  js  c++  java
  • HDU 1026 Ignatius and the Princess I

    HDU 1026 Ignatius and the Princess I
    题意:给定一张图,X位置不能走,带数字的位置需要在该位置停留 '数字' 秒。然后回溯走的路程

    思路:BFS+回溯;


    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    typedef pair<int ,int > P;
    #define inf 0x3f3f3f3f
    struct node
    {
    	int x,y;
    };
    node pre[150][150];
    char mapp[150][150];
    int d[150][150];
    bool vis[150][150];
    int dx[5]={0,1,0,-1},
    	dy[5]={1,0,-1,0};
    int n,m;
    void BFS()
    {
    	memset(d,inf,sizeof(d));
    	queue<P> que;
    	que.push(P(0,0));
    	pre[0][0].x=-1;
    	pre[0][0].y=-1;
    	d[0][0]=0;
    	while(!que.empty())
    	{
    		P q=que.front();
    		que.pop();
    		for(int i=0; i<4; i++)
    		{
    			int nx=q.first+dx[i];
    			int ny=q.second+dy[i];
    			if(nx>=0 && nx<n && ny>=0 && ny<m && mapp[nx][ny]!='X')
    			{
    				if(mapp[nx][ny]=='.')
    				{
    					if(d[q.first][q.second]+1<d[nx][ny])
    					{
    						d[nx][ny]=d[q.first][q.second]+1;
    						pre[nx][ny].x=q.first;
    						pre[nx][ny].y=q.second;
    						que.push(P(nx,ny));
    					}
    				}
    				else
    				{
    					if(d[nx][ny]>d[q.first][q.second]+(mapp[nx][ny]-'0')+1)
    					{
    						d[nx][ny]=d[q.first][q.second]+(mapp	[nx][ny]-'0')+1;
    						pre[nx][ny].x=q.first;
    						pre[nx][ny].y=q.second;
    						que.push(P(nx,ny));
    					}
    				}
    			}
    		}
    	}
    }
        //路径回溯
    void  prinpath(int s,int t)
    {
    	if(pre[s][t].x==-1) return;
    	prinpath(pre[s][t].x,pre[s][t].y);
    	if(mapp[s][t]=='.')
    		printf("%ds:(%d,%d)->(%d,%d)
    ",d[s][t],pre[s][t].x,pre[s][t].y,s,t);
    	else
    	{
    		int num=mapp[s][t]-'0';
    		int k=d[s][t]-num;
    		printf("%ds:(%d,%d)->(%d,%d)
    ",k++,pre[s][t].x,pre[s][t].y,s,t);
    		for(int i=1; i<=num; i++)
    		printf("%ds:FIGHT AT (%d,%d)
    ",k++,s,t);
    	} 
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		memset(vis,0,sizeof(vis));
    		for(int i=0; i<n; i++)
    		{
    			scanf(" %s",mapp[i]);
    		}
    		BFS();
    		int ss=d[n-1][m-1];
    		if(ss==inf) printf("God please help our poor hero.
    ");
    		else{
    			printf("It takes %d seconds to reach the target position, let me show you the way.
    ",ss);
    			prinpath(n-1,m-1);
    		}
    		printf("FINISH
    ");
    	} 
    	return 0;
    }
  • 相关阅读:
    php中数组排序的基本方法
    mysql的锁--行锁,表锁,乐观锁,悲观锁
    php数组内数据个数统计函数
    《自我介绍》
    结对编项目作业
    《结对-英文词频检测程序-需求分析》
    对软件工程课程的期望
    挑战图像处理100问(4)——Otsu
    挑战图像处理100问(3)——二值化
    挑战图像处理100问(2)——灰度化
  • 原文地址:https://www.cnblogs.com/zzulipomelo/p/5284588.html
Copyright © 2011-2022 走看看