#1 Saving Princess Claire_

Source: Internet
Author: User

Saving Princess Claire_Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 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 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 it 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 is many ' * ' in the maze, representing the corresponding grid can is passed through with a cost of certain Amoun T of money, as GDM Teoy have set a toll station.
The grid represented by ' # ' means so can not pass it.
It is said the as GDM Teoy likes to pee and shit everywhere, the this grid is unfortunately damaged by his ugly behavior.
' P ' means it is a transmission port and there could be some in the maze. These ports (if exist) is connected with each other and Prince YKWD can jump from one of the them to another.

They say that there used to being some toll stations, but they exploded (surely they didn ' t exist any more) because of GDM Teo Y ' s Savage Act (Pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince Ykwd have his mysterious power so he can choose his "among the wormholes, even he can choose to Ignor E the wormholes.
Although Prince Ykwd deeply loves Princess Claire_, he's so mean the he doesn ' t want to spend too much of his money in T He maze. Then he turns-to-you, the great workers who loves moving bricks, for help and he hopes you can calculate the minimum Mon EY he needs to take him princess back.

Input

Multiple cases. (No more than fifty.)
The 1st line contains is 3 integers, r, C and cost. ' R ', ' C ' and ' cost ' are as described above. (0 < R * C <= and money are in the range of (0, 10000])
Then a R * C character matrix with ' P ' no further than 10% of the number of all grids and we promise there would be no toll s Tations where the Prince and Princess exist.

Output

One line with a integer, representing the minimum cost. If Prince YKWD cannot rescue his princess whatever he does and 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 namespacestd;6 Const intinf=0x3f3f3f3f;7 8 structNode9 {Ten     intx; One     inty; A }; - Charmp[5005][5005]; - intd[5005][5005],sx,sy,gx,gy,nump; the intdx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; -Node p[505]; -  - intMain () + { -     intn,m,c; +     inti,j; A      while(SCANF (" %d%d%d", &n,&m,&c)! =EOF) at     { -nump=0; - GetChar (); -          for(i=1; i<=n;i++) -         { -              for(j=1; j<=m;j++) in             { -d[i][j]=inf; toscanf"%c",&mp[i][j]); +                 if(mp[i][j]=='Y') -sx=i,sy=J; the                 if(mp[i][j]=='C') *gx=i,gy=J; $                 if(mp[i][j]=='P')Panax Notoginseng                 { -nump++; thep[nump].x=i,p[nump].y=J; +                 } A             } the GetChar (); +         } -  $Queue<node>que; $          while(Que.size ()) Que.pop (); - Node S; -s.x=sx,s.y=Sy; theQue.push (s);d [sx][sy]=0; - Wuyi          while(Que.size ()) the         { -Node now=Que.front (), NEX; Wu             /*For (i=1;i<=m;i++) - printf ("%d", D[1][i]); About printf ("\ n"); $ printf ("%d\n", now.y);*/ - Que.pop (); -  -              for(i=0;i<4; i++) A             { +                 intnx=now.x+dx[i],ny=now.y+Dy[i]; the                 if(1<=nx && nx<=n &&1<=ny && ny<=m && mp[nx][ny]!='#') -                 { $                     intValue=D[NOW.X][NOW.Y]; the                     if(mp[nx][ny]=='*') thevalue++; the                     if(mp[nx][ny]=='P') the                     { -                          for(j=1; j<=nump;j++) in                         { the                             if(value<D[p[j].x][p[j].y]) the                             { About Que.push (P[j]); thed[p[j].x][p[j].y]=value; the                             } the                         } +                     } -                     Else the                     {Bayi                         if(value<D[nx][ny]) the                         { the                             //printf ("%d%d%d\n", now.x,ny,value); -nex.x=nx,nex.y=NY; - Que.push (NEX); thed[nx][ny]=value; the                         } the                     } the                 } -             } the         } the  the         if(d[gx][gy]==inf)94printf"Damn teoy!\n"); the         Else theprintf"%d\n", d[gx][gy]*c); the     }98     return 0; About } - /*101 1 7 5102 py**c*p103 */
View Code

#1 Saving Princess Claire_

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.