/*
分析:
BFS+优先队列。
代码写的比较烂,1000MS过的- -III(虽然这题limit是2000MS吧。。。)
对于'.'和'@',都要记录到其的step,不同的是前者可以用走的或飞的往
四周推、而后者只能用飞的往四周推。另外要开一个三维的数组,第三维来记
录还有多少蓝~
如果用上类似dancinglinks那样的,当某个点作为过中心后把它删除掉的
话,赶脚速度会飞快提升的,不过好麻烦囧~~
可以建边求最短路,不过一些边可以走是建立在“pre节点有足够蓝”的前提
下的,所以麻烦程度。。。
2013-03-20
*/
#include"iostream"
#include"queue"
using namespace std;
const int N=85;
int n,m,T,p;
int s_x,s_y,e_x,e_y;
char map[N][N];
int flag[N][N][N],Temp[N][N][N];
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
struct node{
int x,y,p,step;
bool friend operator<(node n1,node n2)
{
return n2.step<n1.step;
}
};
int BFS()
{
int i;
priority_queue<node>q;
node now,next;
memset(flag,0,sizeof(flag));
memset(Temp,127,sizeof(Temp));
now.x=s_x;
now.y=s_y;
now.step=0;
now.p=p;
q.push(now);
Temp[now.x][now.y][now.p]=0;
while(!q.empty())
{
now=q.top();
q.pop();
if(flag[now.x][now.y][now.p]) continue;
flag[now.x][now.y][now.p]=1;
if(now.x==e_x && now.y==e_y) return now.step;
for(i=0;i<4;i++)
{
//走的
if(map[now.x][now.y]=='.')
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.p=now.p;
next.step=now.step+2;
if(!(next.x<0 || next.x>=n || next.y<0 || next.y>=m))
if(map[next.x][next.y]=='.')
if(!flag[next.x][next.y][now.p])
if(next.step<Temp[next.x][next.y][next.p])
{
q.push(next);
Temp[next.x][next.y][next.p]=next.step;
}
}
//飞的
if(!now.p) continue;
next.x=now.x;
next.y=now.y;
next.p=now.p;
next.step=now.step;
while(1)
{
next.x+=dir[i][0];
next.y+=dir[i][1];
next.p--;
next.step++;
if(next.x<0 || next.x>=n || next.y<0 || next.y>=m) break;
if(next.p<0) break;
if(map[next.x][next.y]=='#') break;
if(flag[next.x][next.y][next.p]) continue;
if(map[next.x][next.y]=='@')
{
if(next.step<Temp[next.x][next.y][next.p])
{
q.push(next);
Temp[next.x][next.y][next.p]=next.step;
}
continue;
}
if(next.step>=Temp[next.x][next.y][next.p]) continue;
q.push(next);
Temp[next.x][next.y][next.p]=next.step;
}
}
}
return -1;
}
int main()
{
int Case=1;
int i,l;
while(scanf("%d%d%d%d",&n,&m,&T,&p)!=-1)
{
for(i=0;i<n;i++)
{
cin>>map[i];
for(l=0;l<m;l++)
{
if(map[i][l]=='Y') {s_x=i;s_y=l;map[i][l]='.';}
if(map[i][l]=='L') {e_x=i;e_y=l;map[i][l]='.';}
}
}
int ans=BFS();
cout<<"Case "<<Case++<<":"<<endl;
if(ans==-1 || ans>T) cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
else cout<<"Yes, Yifenfei will kill Lemon at "<<ans<<" sec."<<endl;
}
return 0;
}