1 #include<stdio.h> 2 #include<string.h> 3 typedef struct que 4 { 5 int x,y; 6 long num; 7 }st; 8 st q[10001]; 9 char str[1001][1001]; 10 int f[1001][1001]; 11 int p,d; 12 void inque(int a,int b) 13 { 14 d++; 15 if(d>10000) 16 d = d%10000; 17 q[d].x = a; 18 q[d].y = b; 19 } 20 int main() 21 { 22 int i,j,x3,y3,r,flag,c,w,cost,a[501],b[501],x1,y1,dis[2]={-1,1}; 23 while(scanf("%d%d%d%*c", &r,&c,&cost)!=EOF) 24 { 25 int g = 0; 26 flag = 0; 27 memset(q,0,sizeof(q)); 28 memset(f,0,sizeof(f)); 29 for(i = 1 ; i <= r ; i++) 30 { 31 for(j = 1; j <= c ; j++) 32 { 33 scanf("%c", &str[i][j]); 34 if(str[i][j]=='Y') 35 { 36 x1 = i; 37 y1 = j; 38 } 39 if(str[i][j]=='P') 40 { 41 a[g] = i; 42 b[g] = j; 43 g++; 44 } 45 } 46 getchar(); 47 } 48 p = 0; 49 d = 0; 50 w = 0; 51 q[1].num = 0; 52 inque(x1,y1); 53 f[x1][y1] = 1; 54 do 55 { 56 p++; 57 if(p>10000) 58 p = p%10000; 59 x3 = q[p].x; 60 y3 = q[p].y; 61 for(i = 0 ; i < 2 ;i++) 62 { 63 int px = x3+dis[i]; 64 int py = y3+dis[i]; 65 if(!f[px][y3]&&px>0&&px<=r&&(str[px][y3]=='*'||str[px][y3]=='P'||str[px][y3]=='C')) 66 { 67 if(str[px][y3]=='*') 68 { 69 inque(px,y3); 70 q[d].num = q[p].num+cost; 71 f[px][y3] = 1; 72 } 73 else 74 if(str[px][y3]=='P'&&!w) 75 { 76 inque(px,y3); 77 q[d].num = q[p].num; 78 f[px][y3] = 1; 79 for(j = 0 ; j < g ; j++) 80 { 81 inque(a[j],b[j]); 82 f[a[j]][b[j]] = 1; 83 q[d].num = q[p].num; 84 } 85 w = 1; 86 } 87 else 88 if(str[px][y3]=='C') 89 { 90 flag = 1; 91 break; 92 } 93 } 94 if(!f[x3][py]&&py>0&&py<=c&&(str[x3][py]=='*'||str[x3][py]=='P'||str[x3][py]=='C')) 95 { 96 if(str[x3][py]=='*') 97 { 98 inque(x3,py); 99 q[d].num = q[p].num+cost; 100 f[x3][py] = 1; 101 } 102 else 103 if(str[x3][py]=='P'&&!w) 104 { 105 inque(x3,py); 106 q[d].num = q[p].num; 107 f[x3][py] = 1; 108 for(j = 0 ; j < g ; j++) 109 { 110 if(a[j]!=x3||b[j]!=py) 111 { 112 inque(a[j],b[j]); 113 q[d].num = q[p].num; 114 f[a[j]][b[j]] = 1; 115 } 116 } 117 w = 1; 118 } 119 else 120 if(str[x3][py]=='C') 121 { 122 flag = 1; 123 break; 124 } 125 } 126 } 127 if(flag) 128 break; 129 }while(p!=d); 130 if(flag) 131 printf("%ld\n",q[p].num); 132 else 133 printf("Damn teoy!\n"); 134 } 135 return 0; 136 }
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 96 Accepted Submission(s): 34