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

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
 
拥有传送们的搜索.....wa了一下午....(/ □ \)
终于搞成AC了....
代码:
 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 }
View Code

 

posted @ 2014-08-24 18:15  龚细军  阅读(234)  评论(0编辑  收藏  举报