前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu----(4308)Saving Princess claire_(搜索)

hdu----(4308)Saving Princess claire_(搜索)

作者头像
Gxjun
发布2018-03-26 14:49:24
5320
发布2018-03-26 14:49:24
举报
文章被收录于专栏:mlml

Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2354    Accepted Submission(s): 843

Problem Description

Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy. Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess. The maze can be described as a matrix of r rows and c columns, with grids, such as 'Y', 'C', '*', '#' and 'P', in it. Every grid is connected with its up, down, left and right grids. There is only one 'Y' which means the initial position when Prince ykwd breaks into the maze. There is only one 'C' which means the position where Princess claire_ is jailed. There may be many '*'s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station. The grid represented by '#' means that you can not pass it. It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior. 'P' means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another. They say that there used to be some toll stations, but they exploded(surely they didn't exist any more) because of GDM teoy's savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes. Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn't want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he needs to take his princess back.

Input

Multiple cases.(No more than fifty.) The 1st line contains 3 integers, r, c and cost. 'r', 'c' and 'cost' is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] ) Then an r * c character matrix with 'P' no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.

Output

One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)

Sample Input

1 3 3 Y*C 1 3 2 Y#C 1 5 2 YP#PC

Sample Output

3 Damn teoy! 0

Author

BUPT

Source

2012 Multi-University Training Contest 1

拥有传送们的搜索.....wa了一下午....(/ □ \)

终于搞成AC了....

代码:

代码语言:javascript
复制
 1 #include<cstring>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<vector>
 5 using namespace std;
 6 
 7 const int maxn=5003;
 8 int cc,rr,cost;
 9 char str[maxn][maxn];
10  struct node {
11     int x,y;
12     int val;
13 };
14 vector <node> pot;
15 
16 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
17 node st,en;
18 queue<node>seek;
19 node tem,qq;
20  vector<node>::iterator it;
21 int main(){
22 
23     //freopen("test.in","r",stdin);
24   while(scanf("%d%d%d",&rr,&cc,&cost)!=EOF){
25 
26        pot.clear();
27        vector< vector<node> > map(rr+10);  
28        for(int i=0;i<rr+10;i++)
29           map[i].resize(cc+10);
30         for(int i=0;i<rr;i++){
31          scanf("%s",str[i]);
32       for(int j=0;j<cc;j++){
33             map[i][j]=(node){i,j,0};
34           if(str[i][j]=='P'){    
35             pot.push_back((node){i,j,0});
36           }
37           if(str[i][j]=='Y')
38             st=(node){i,j,0};
39           if(str[i][j]=='C'){
40             en=(node){i,j,-1};
41             map[i][j]=(node){i,j,-1};
42           }
43       }
44     }
45      //bfs();
46   seek.push(st);
47   while(!seek.empty()){
48     tem=seek.front();
49     seek.pop();
50     for(int i=0;i<4;i++){
51             qq=(node){tem.x+dir[i][0],tem.y+dir[i][1],0};
52         if(qq.x>=0&&qq.x<rr&&qq.y>=0&&qq.y<cc&&str[qq.x][qq.y]!='#'){
53               if(str[qq.x][qq.y]!='C'){
54                 if(str[qq.x][qq.y]=='P'){
55                       str[qq.x][qq.y]='#';
56                     if(map[qq.x][qq.y].val==0||map[qq.x][qq.y].val>map[tem.x][tem.y].val)
57                             map[qq.x][qq.y].val=map[tem.x][tem.y].val;
58                   if(pot.size()!=0){
59                      for(it=pot.begin();it<pot.end();it++)
60                       if((*it).x==qq.x&&(*it).y==qq.y)  break;
61                       pot.erase(it);
62                      }
63                     if(pot.size()!=0){
64                      for(it=pot.begin();it<pot.end();it++){    
65                      map[(*it).x][(*it).y].val=map[qq.x][qq.y].val;
66                      seek.push((map[(*it).x][(*it).y]));
67                        str[(*it).x][(*it).y]='#';
68                     }
69                 }else  seek.push(map[qq.x][qq.y]);
70 
71                 }
72                 else if(map[qq.x][qq.y].val==0||map[qq.x][qq.y].val>tem.val+cost){ 
73                   map[qq.x][qq.y].val=tem.val+cost;
74                   seek.push(map[qq.x][qq.y]);
75               }
76             }
77             else
78              if(map[qq.x][qq.y].val==-1||map[qq.x][qq.y].val>tem.val)   
79              {
80                      map[qq.x][qq.y].val=tem.val;
81              }
82         }
83     }
84   }
85   if(map[en.x][en.y].val==-1)
86     printf("Damn teoy!\n");
87   else
88     printf("%d\n",map[en.x][en.y].val);
89   }
90   return 0;
91 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Saving Princess claire_
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档