标签:
Description
Input
Output
Sample Input
Sample Output
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 5 102 PY**C*P 103 */
2012 #1 Saving Princess claire_
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4771438.html