HDU1026 Ignatius and the Princess I(优先队列+BFS)
2016-06-04 00:24
363 查看
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16500 Accepted Submission(s): 5248
Special Judge
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 simply, we assume the labyrinth is a N*M two-dimensional
array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here
is some rules:
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated
by the end of file. More details in the Sample Input.
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero
the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
Sample Input
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
题解:优先队列+BFS就好了,结构体Node里面存一下路径。别忘了入口就有怪物的可能情况。
代码:
//************************************************************************// //*Author : Handsome How *// //************************************************************************// //#pragma comment(linker, "/STA CK:1024000000,1024000000") #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> #include <sstream> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <string> #include <ctime> #if defined(_MSC_VER) || __cplusplus > 199711L #define aut(r,v) auto r = (v) #else #define aut(r,v) __typeof(v) r = (v) #endif #define foreach(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it) #define fur(i,a,b) for(int i=(a);i<=(b);i++) #define furr(i,a,b) for(int i=(a);i>=(b);i--) #define cl(a) memset((a),0,sizeof(a)) #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define dbg(x) cout << #x << " = " << x << endl #ifdef HandsomeHow #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> pii; const int inf=0x3f3f3f3f; const double eps=1e-8; const int mod=1000000007; const double pi=acos(-1); inline void gn(long long&x){ int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0'); while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg; } inline void gn(int&x){long long t;gn(t);x=t;} inline void gn(unsigned long long&x){long long t;gn(t);x=t;} int gcd(int a,int b){return a? gcd(b%a,a):b;} ll powmod(ll a,ll x,ll mod){ll t=1ll;while(x){if(x&1)t=t*a%mod;a=a*a%mod;x>>=1;}return t;} // (づ°ω°)づe★ //------------------------------------------------ int dx[] = {-1,0,1,0}; int dy[] = {0,1,0,-1}; const int maxn = 105; char mp[maxn][maxn]; bool vis[maxn][maxn]; int X,Y; struct Node{ int x; int y; int time; vector<pii>v; friend bool operator<(Node a, Node b){ return a.time > b.time ; } }; bool check(int x, int y){ return x>=0&&y>=0&&x<X&&y<Y&&!vis[x][y]&&mp[x][y]!='X'; } Node bfs(){ vis[0][0]=true; Node now,next; now.x = 0; now.y = 0; now.time = 0; now.v.push_back(make_pair(0,0)); priority_queue<Node>q; q.push(now); while(!q.empty()){ now = q.top(); if(now.x == X-1 && now.y == Y -1) return now; q.pop(); next = now; fur(i,0,3){ next.x = now.x + dx[i]; next.y = now.y + dy[i]; if(!check(next.x,next.y))continue; if(mp[next.x][next.y]=='.') next.time = now.time + 1; else next.time = now.time + mp[next.x][next.y]-'0' + 1; next.v.push_back(make_pair(next.x,next.y)); vis[next.x][next.y] = true; q.push(next); next.v.pop_back(); } } Node tmp; tmp.time = -1; return tmp; } int main(){ #ifdef HandsomeHow //freopen("E:\\data.in","r",stdin); //freopen("E:\\data.out","w",stdout); time_t beginttt = clock(); #endif while(scanf("%d %d",&X, &Y) != EOF){ cl(vis); fur(i,0,X-1)scanf("%s",mp[i]); Node ans; if(mp[0][0] == 'X') ans.time = -1; else ans = bfs(); if(ans.time == -1){ printf("God please help our poor hero.\nFINISH\n"); } else{ printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans.time); int SZ = ans.v.size(); pii now; int t = 1; now = ans.v[0]; if(mp[0][0]!='.'){ for(int k = 0; k < mp[now.first][now.second] - '0'; ++k){ printf("%ds:FIGHT AT (%d,%d)\n",t++,now.first,now.second); } } for(int i = 1; i < SZ; ++i){ printf("%ds:(%d,%d)->(%d,%d)\n",t++,now.first,now.second,ans.v[i].first,ans.v[i].second); now = ans.v[i]; if(mp[now.first][now.second]!='.'){ for(int k = 0; k < mp[now.first][now.second] - '0'; ++k){ printf("%ds:FIGHT AT (%d,%d)\n",t++,now.first,now.second); } } } printf("FINISH\n"); } } #ifdef HandsomeHow time_t endttt = clock(); debug("time: %d\n",(int)(endttt - beginttt)); #endif return 0; }
相关文章推荐
- ubuntu在终端下的字体设置快捷键
- Win7 下安装Unbuntu14.04双系统
- 内部类,非局部类
- Android/Linux根目录下常见目录作用
- linux入门基础笔记
- GUI中的事件绑定
- [NOIP2010] 提高组 洛谷P1525 关押罪犯
- 开始入坑-大蟒蛇首发
- windows下git中文乱码的解决方法
- 几种常用动画总结
- 《CSS3实战》笔记--选择器(二)
- linux后台开发具备能力集锦
- 计算圆周率
- GPU(CUDA)学习日记(十二)------ CUDA并行编程较有用的总结
- GPU(CUDA)学习日记(十一)------ 深入理解CUDA线程层次以及关于设置线程数的思考
- GPU(CUDA)学习日记(十)------ Kernal 内核函数 参数的传递
- 如何自制DIY电视天线【方法】
- 数据结构学习笔记之广义表的相关知识点
- GPU(CUDA)学习日记(九)------ CUDA存储器模型
- mysql索引和分表