您的位置:首页 > 大数据 > 人工智能

4.2.7 Waiting ten thousand years for Love

2012-12-25 23:10 218 查看
[align=left]Problem Description[/align]
It was ten thousand years, after Demon Lemon caught Yifenfei’s love. In order to revenge and save his love, Yifenfei have been practising sword all day long and his Kongfu skills becomes so powerful that he can kill Demon Lemon immediately. Recently, Yifenfei have found Lemon’s castle, and now he is going to kill Lemon. At the same time, hearing about the terrible news, Demon Lemon is now preparing for escaping...

Now Yifenfei has got the map of the castle.
Here are all symbols of the map:
Only one ‘Y’ Indicates the start position of Yifenfei.
Only one ‘L’ Indicates the position of Demon Lemon.
‘.’ Indicate the road that Yifenfei can walk on it, or fly over it.
‘#’ Indicate the wall that Yifenfei can not walk or flay through it.
‘@’ Indicate the trap that Yifenfei can not walk on it, but can fly over it.

Yifenfei
can walk or fly to one of up, down, left or right four directions each
step, walk costs him 2 seconds per step, fly costs him 1 second per step
and 1 magic power. His magic power will not increased, and if his magic
power is zero, he can not fly any more.

Now Yifenfei asks you
for helping him kill Demon Lemon smoothly. At the same time, Demon Lemon
will Leave the castle Atfer T seconds. If Yifenfei can’t kill Demon
Lemon this time, he have to wait another ten thousand years.

[align=left]Input[/align]

Lots of test cases, please process to end of file. In each test
case, firstly will have four integers N, M, T, P(1 <= N, M, P <=
80, 1 <= T <= 100000), indicates the size of map N * M, the
Lemon’s leaving time(after T seconds, Lemon will disappeared) and
Yifenfei’s magic power. Then an N * M two-dimensional array follows
indicates the map.

[align=left]Output[/align]

For each test case, first Print a line “Case C:”, C indicates the
case number. Then, if Yifenfei can kill Demon Lemon successfully, Print
“Yes, Yifenfei will kill Lemon at T sec.”, T indicates the minimum
seconds he must cost. Otherwise, Print ”Poor Yifenfei, he has to wait
another ten thousand years.”

[align=left]Sample Input[/align]

2 3 2 2
Y@L
###
2 3 4 1
Y@L
###
2 3 4 0
Y.L
###
2 3 3 0
Y.L
###


[align=left]Sample Output[/align]

Case 1:
Yes, Yifenfei will kill Lemon at 2 sec.
Case 2:
Poor Yifenfei, he has to wait another ten thousand years.
Case 3:
Yes, Yifenfei will kill Lemon at 4 sec.
Case 4:
Poor Yifenfei, he has to wait another ten thousand years.


Hint
Hint Case 1: Yifenfei cost 1 second and 1 magic-power fly to ‘@’, but he can not step on it, he must cost another 1 second and 1 magic-power fly to ‘L’ and kill Lemon immediately. Case 2: When Yifenfei Fly to ‘@’, he has no power to fly, and is killed by trap.

思路:BFS,看了别人的AC代码,终于开始用优先队列了!那么优先队列是个什么东东呢?先自己百度,就说可以把队列中的元素按一定顺序排列,好好啊!

有一些细节错误害得我检查了好久!

#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

struct qq
{
int x,y,step,p;
bool friend operator < (const qq &a,const qq &b)
{
if (a.step==b.step)
return a.p<b.p;
return a.step>b.step;
}
} s,ya;

priority_queue<qq> q;

const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
bool f[82][82][82];
char map[82][82];
int power,t,n,m,ax,ay,i,cnt=0,tot=0;

bool pd()
{
if (map[ya.x][ya.y]=='#') return true; //也就是不能到达,直接退出
if (ya.x>=0 && ya.x<n && ya.y>=0 && ya.y<m && not f[ya.x][ya.y][ya.p])
return false;
return true;
}

void judge()
{
if (s.p>0)  //也就是说这一步是可以用能量的
for (i=0;i<4;i++)
{
ya.x=s.x+dx[i];
ya.y=s.y+dy[i];
ya.p=s.p-1; //能量减少1
ya.step=s.step;
if (not pd())
{
f[ya.x][ya.y][ya.p]=true;
ya.step++; //步数增加1
q.push(ya);
}
}
if (map[s.x][s.y]=='@') return; //在@的时候,如果不能移动,则挂了
for (i=0;i<4;i++)
{
ya.x=s.x+dx[i];
ya.y=s.y+dy[i];
ya.p=s.p;
ya.step=s.step; //这一句一定要加上,否则就未清零啊!
if (not pd() && ( map[ya.x][ya.y]=='.' || map[ya.x][ya.y]=='L' ))
{
f[ya.x][ya.y][ya.p]=true; //这句话把我整惨了!
ya.step+=2;
q.push(ya);
}
}
}

void bfs()
{
memset(f,false,sizeof(f));
while (!q.empty())
q.pop();
s.x=ax; s.y=ay; s.p=power; s.step=0;
f[s.x][s.y][s.step]=true;
q.push(s);
while (!q.empty())
{
s=q.top();
q.pop();
if (map[s.x][s.y]=='#') break;
if (map[s.x][s.y]=='L')
{
if (t>=s.step)
printf("Yes, Yifenfei will kill Lemon at %d sec.\n",s.step);
else printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
return;
}
judge();
}
printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
}

void init()
{
while (scanf("%d%d%d%d",&n,&m,&t,&power)!=EOF)
{
cnt++;
printf("Case %d:\n",cnt);
for(int i=0;i<n;i++)
{
scanf("%s",map[i]);
for(int j=0;j<m;j++)
if(map[i][j]=='Y')
{
ax=i;
ay=j;
}
}
//        printf("ax:%d ay:%d\n",ax,ay);
bfs();
}
}

int main ()
{
init();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: