2012 #1 Saving Princess claire_

來源:互聯網
上載者:User

標籤:

Saving Princess claire_Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64uSubmit Status Practice HDU 4308

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 3Y*C1 3 2Y#C1 5 2YP#PC  

Sample Output

3Damn teoy!0
  1 #include <stdio.h>  2 #include <string.h>  3 #include <queue>  4 #include <algorithm>  5 using namespace std;  6 const int inf=0x3f3f3f3f;  7   8 struct Node  9 { 10     int x; 11     int y; 12 }; 13 char mp[5005][5005]; 14 int d[5005][5005],sx,sy,gx,gy,nump; 15 int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; 16 Node P[505]; 17  18 int main() 19 { 20     int n,m,c; 21     int i,j; 22     while(scanf("%d %d %d",&n,&m,&c)!=EOF) 23     { 24         nump=0; 25         getchar(); 26         for(i=1;i<=n;i++) 27         { 28             for(j=1;j<=m;j++) 29             { 30                 d[i][j]=inf; 31                 scanf("%c",&mp[i][j]); 32                 if(mp[i][j]==‘Y‘) 33                     sx=i,sy=j; 34                 if(mp[i][j]==‘C‘) 35                     gx=i,gy=j; 36                 if(mp[i][j]==‘P‘) 37                 { 38                     nump++; 39                     P[nump].x=i,P[nump].y=j; 40                 } 41             } 42             getchar(); 43         } 44  45         queue<Node> que; 46         while(que.size()) que.pop(); 47         Node s; 48         s.x=sx,s.y=sy; 49         que.push(s);d[sx][sy]=0; 50  51         while(que.size()) 52         { 53             Node now=que.front(),nex; 54             /*for(i=1;i<=m;i++) 55                 printf("%d ",d[1][i]); 56             printf("\n"); 57             printf("%d\n",now.y);*/ 58             que.pop(); 59  60             for(i=0;i<4;i++) 61             { 62                 int nx=now.x+dx[i],ny=now.y+dy[i]; 63                 if(1<=nx && nx<=n && 1<=ny && ny<=m && mp[nx][ny]!=‘#‘) 64                 { 65                     int value=d[now.x][now.y]; 66                     if(mp[nx][ny]==‘*‘) 67                         value++; 68                     if(mp[nx][ny]==‘P‘) 69                     { 70                         for(j=1;j<=nump;j++) 71                         { 72                             if(value<d[P[j].x][P[j].y]) 73                             { 74                                 que.push(P[j]); 75                                 d[P[j].x][P[j].y]=value; 76                             } 77                         } 78                     } 79                     else 80                     { 81                         if(value<d[nx][ny]) 82                         { 83                             //printf("%d %d %d\n",now.x,ny,value); 84                             nex.x=nx,nex.y=ny; 85                             que.push(nex); 86                             d[nx][ny]=value; 87                         } 88                     } 89                 } 90             } 91         } 92  93         if(d[gx][gy]==inf) 94             printf("Damn teoy!\n"); 95         else 96             printf("%d\n",d[gx][gy]*c); 97     } 98     return 0; 99 }100 /*101 1 7 5102 PY**C*P103 */
View Code

 

2012 #1 Saving Princess claire_

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.