HDU 1026 Ignatius and the Princess I (BFS)

题目链接

题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间。

思路 : BFS、、、、、

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <iostream>
 5
 6 using namespace std ;
 7
 8 struct node
 9 {
10     int x,y ;
11     int tim ;
12     friend bool operator < (node a,node b)
13     {
14         return a.tim > b.tim ;
15     }
16 } temp,temp1;
17 int N,M ,t;
18 char mapp[110][110] ;
19 int vis[110][110] ;
20 int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}} ;
21
22 int BFS()
23 {
24     temp.x = 0 ;
25     temp.y = 0 ;
26     temp.tim = 0 ;
27     priority_queue<node>Q ;
28     Q.push(temp) ;
29    // vis[0][0] = true ;
30     while(Q.size())
31     {
32         temp = Q.top() ;
33         Q.pop() ;
34         if(temp.x == N-1 && temp.y == M-1) return temp.tim ;
35         for(int i = 0 ; i < 4 ; i++)
36         {
37             temp1.x = temp.x+dir[i][0] ;
38             temp1.y = temp.y+dir[i][1] ;
39             if(temp1.x >= 0 && temp1.y >= 0 && temp1.x <= N-1 && temp1.y <= M-1 && vis[temp1.x][temp1.y] == -1 && mapp[temp1.x][temp1.y] != ‘X‘)
40             {
41                 vis[temp1.x][temp1.y] = temp.x*M+temp.y ;
42                 if(mapp[temp1.x][temp1.y] == ‘.‘)
43                     temp1.tim = temp.tim+1 ;
44                 else
45                     temp1.tim = temp.tim + mapp[temp1.x][temp1.y]-‘0‘+1 ;
46                 Q.push(temp1) ;
47             }
48         }
49     }
50     return -1 ;
51 }
52 void print(int x,int y)
53 {
54     if(x == 0 && y == 0)
55         return ;
56     int xx = vis[x][y]/M ;
57     int yy = vis[x][y]%M ;
58     print(xx,yy) ;
59     printf("%ds:(%d,%d)->(%d,%d)\n",t++,xx,yy,x,y) ;
60     if(mapp[x][y] >= ‘0‘ && mapp[x][y] <= ‘9‘)
61     {
62         while(mapp[x][y] -‘0‘ > 0)
63         {
64             printf("%ds:FIGHT AT (%d,%d)\n",t++,x,y) ;
65             mapp[x][y] -- ;
66         }
67     }
68 }
69 int main()
70 {
71     while(~scanf("%d %d",&N,&M))
72     {
73         for(int i = 0 ; i < N ; i++ )
74             scanf("%s",mapp[i]) ;
75         memset(vis,-1,sizeof(vis)) ;
76         int timee = BFS() ;
77         if(timee == -1)
78         {
79             printf("God please help our poor hero.\nFINISH\n") ;
80         }
81         else
82         {
83             t = 1 ;
84             printf("It takes %d seconds to reach the target position, let me show you the way.\n",timee) ;
85             print(N-1,M-1) ;
86             printf("FINISH\n") ;
87         }
88     }
89     return 0 ;
90 }

时间: 2024-11-06 10:07:44

HDU 1026 Ignatius and the Princess I (BFS)的相关文章

hdu 1026 Ignatius and the Princess I(bfs)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16269    Accepted Submission(s): 5164 Special Judge Problem Description The Princess has been abducted by the BEelzeb

hdu 1026 Ignatius and the Princess I (BFS+优先队列)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11700    Accepted Submission(s): 3653Special Judge Problem Description The Princess has been abducted by the BEelzebub

HDU 1026 Ignatius and the Princess I(bfs +记录路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14184    Accepted Submission(s): 4474 Special Judge Problem Description The Princess has been abducted by the BEelzeb

HDU 1026 Ignatius and the Princess I (基本算法-BFS)

Ignatius and the Princess I Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem si

hdu 1028 Ignatius and the Princess III(母函数)

题意: N=a[1]+a[2]+a[3]+...+a[m];  a[i]>0,1<=m<=N; 例如: 4 = 4;  4 = 3 + 1;  4 = 2 + 2;  4 = 2 + 1 + 1;  4 = 1 + 1 + 1 + 1; 共有5种. 给N,问共有几种构造方式. 思路: 一个数N分解的式子中1的个数可以是0,1,2,3,...,N. 2的个数可以是0,1,2,...,N/2. .... 母函数基础题,, 看代码. 当然也可以用DP(背包) 母函数代码: int N,num;

HDU 1026 Ignatius and the Princess I(优先队列+打印路径)

题意:n*m的迷宫,从(0,0)到(n-1,m-1),遇到怪物停留怪物所在方格中的数字个单位时间,求最短时间并打印路径: 思路:用bfs先搜最短路,搜最短路时一定要用优先队列,不然结果不对:在通过保存上一步的方法保存路径,到达终点时,将路径查询出来,遇到怪物是位置不变: #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; int

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c

hdu 1029 Ignatius and the Princess IV(排序)

题意:求出现次数>=(N+1)/2的数 思路:排序后,输出第(N+1)/2个数 #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int a[999999]; int main(){ int n,i; while(~scanf("%d",&n)){ for(i=0;i<n;++i) scanf("%d",&

hdu 1028 Ignatius and the Princess III(母函数,完全背包)

http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分问题. 第一道母函数...母函数入门 小于等于n的整数共有n个,1,2......n,每个数都有无限多个,对于整数1,它所对应的母函数为(1+x+x^2+...+x^k+...),整数2对应的母函数为(1+x^2+X^4+...+x^(2*k)+...),整数3对应的母函数为(1+x^3+x^6+...+x^(3*k)+...),以此类推,直到整数n. 那么n的整数划分的个数就是这n个母函数乘积