Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=2653

BFS+优先队列

View Code
 1 #include <cstdio>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int N=100;
6 const int dx[4]={0,1,0,-1};
7 const int dy[4]={1,0,-1,0};
8 char maze[N][N];
9 int vis[N][N][N];
10 int n,m,p,t;
11 struct sta
12 {
13 int x,y,pow,dis;
14 };
15 struct qcmp
16 {
17 bool operator() (const sta a,const sta b)
18 {
19 return a.dis>b.dis;
20 }
21 };
22 priority_queue<sta,vector<sta>,qcmp> q;
23 int bfs(int x,int y)
24 {
25 while (!q.empty()) q.pop();
26 vis[x][y][p]=1;
27 sta u={x,y,p,0};
28 q.push(u);
29 while (!q.empty())
30 {
31 u=q.top(); q.pop();
32 int x,y,d,nx,ny,dis,pow;
33 x=u.x; y=u.y; dis=u.dis; pow=u.pow;
34 if (dis>t) return -1;
35 if (maze[x][y]=='L') return dis;
36 for (d=0;d<4;d++)
37 {
38 nx=x+dx[d]; ny=y+dy[d];
39 if (!maze[nx][ny] || maze[nx][ny]=='#') continue;
40 if (maze[x][y]!='@' && maze[nx][ny]!='@' && !vis[nx][ny][pow])
41 {
42 vis[nx][ny][pow]=1;
43 sta v={nx,ny,pow,dis+2};
44 q.push(v);
45 }
46 if (!vis[nx][ny][pow-1] && pow>0)
47 {
48 vis[nx][ny][pow-1]=1;
49 sta v={nx,ny,pow-1,dis+1};
50 q.push(v);
51 }
52 }
53 }
54 return -1;
55 }
56 int main()
57 {
58 int C=0;
59 int i,j,x,y,ans;
60 while (~scanf("%d%d%d%d",&n,&m,&t,&p))
61 {
62 memset(vis,0,sizeof(vis));
63 memset(maze,0,sizeof(maze));
64 for (i=1;i<=n;i++) scanf("%s",maze[i]+1);
65 for (i=1;i<=n;i++)
66 for (j=1;j<=m;j++)
67 if (maze[i][j]=='Y') {x=i; y=j;}
68 ans=bfs(x,y);
69 printf("Case %d:\n",++C);
70 if (ans<0) printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
71 else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
72 }
73 return 0;
74 }

 

posted on 2012-02-11 22:25  Qiuqiqiu  阅读(307)  评论(0编辑  收藏  举报