Saving Princess claire_
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2933 Accepted Submission(s): 1068
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.
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.
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
思路:从王子的位置向公主的位置找最短路径,先把所有的传送门存起来,然后bfs,我的写法内存开销极大。。。。
代码:
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 1000000007
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn = 5000 + 1 , inf = 0x3f3f3f;
char G[maxn][maxn];
bool vis[maxn][maxn];
struct pos{
int x,y,cnt;
};
pos ss[500+5];
int r,c,cost,k,Start[2],End[2];
int dr[4]={1,0,-1,0};
int dc[4]={0,1,0,-1};
bool is_can(int x,int y){
if(0<=x&&x<r&&0<=y&&y<c&&G[x][y]!='#'&&!vis[x][y]) return true;
return false;
}
pos temp,now;
int xx,yy,cntt;
int bfs(){
memset(vis,false,sizeof(vis));
queue<pos>q;
temp.x=Start[0];temp.y=Start[1];temp.cnt=0;
vis[Start[0]][Start[1]] = true;
q.push(temp);
while(!q.empty()){
now = q.front();q.pop();
if(now.x==End[0]&&now.y==End[1]) return now.cnt - 1;
for(int i = 0 ; i < 4 ; i++ ){
xx = now.x+dr[i];
yy = now.y+dc[i];
cntt = now.cnt;
if(!is_can(now.x+dr[i],now.y+dc[i])) continue;
vis[xx][yy]=true;
if(G[xx][yy]=='P'){
for(int j = 0 ; j < k ; j++){
if(ss[j].x==xx&&ss[j].y==yy) continue;
temp.x=ss[j].x;
temp.y=ss[j].y;
temp.cnt = cntt;
q.push(temp);
}
continue;
}
temp.x=xx;
temp.y=yy;
temp.cnt = cntt + 1;
q.push(temp);
}
}
return -1;
}
int main(){
while(scanf("%d %d %d",&r,&c,&cost)!=EOF){
k=0;
for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++){
scanf(" %c",&G[i][j]);
if(G[i][j]=='Y'){
Start[0]=i;
Start[1]=j;
vis[i][j]=1;
}
else if(G[i][j]=='C'){
End[0]=i;
End[1]=j;
}
else if(G[i][j]=='P'){
ss[k].x = i;
ss[k].y = j;
ss[k++].cnt = 0;
}
}
int ans = bfs();
if(ans == -1) printf("Damn teoy!\n");
else printf("%d\n",ans*cost);
}
return 0;
}