Waiting ten thousand years for Love
来源:互联网 发布:淘宝装修页面图片 编辑:程序博客网 时间:2024/06/10 21:36
最后一道搜索题,关键是划分状态。分析这道题:
如果以(x,y)为状态,无法唯一确定状态。因为(‘.’ Indicate the road that Yifenfei can walk on it, or fly over it.)
所以必须要(x,y,p)为状态区分。
本题采用bfs+优先队列
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.
2 3 2 2Y@L###2 3 4 1Y@L###2 3 4 0Y.L###2 3 3 0Y.L###
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.HintHintCase 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.
#include<iostream>
#include<queue>
using namespace std;
typedef struct node{
int i;
int j;
int t;
int p;
}Node;
struct qcmp
{
bool operator() (const Node a,const Node b)
{
return a.t>b.t;
}
};
char field[81][81];
int N,M,T,P;
bool isvisit[81][81][81];
bool cross(int a,int b)
{
if(a>=0&&a<N&&b>=0&&b<M&&field[a][b]!='#')
{
return true;
}
return false;
}
int dir[][2]={{0,1},{1,0},{-1,0},{0,-1}};
void bfs(int a,int b)
{
bool isfind=false;
int count=0;
Node start={a,b,0,P};
isvisit[a][b][P]=true;
priority_queue<Node,vector<Node>,qcmp> myqueue;
myqueue.push(start);
while(!myqueue.empty())
{
Node pre=myqueue.top();
myqueue.pop();
if(pre.t<=T&&field[pre.i][pre.j]=='L')
{
isfind=true;
count=pre.t;
break;
}
if(pre.t>T)
break;
for(int i=0;i<4;i++)
{
int curi=pre.i+dir[i][0];
int curj=pre.j+dir[i][1];
int curp=pre.p;
int curt=pre.t;
if(cross(curi,curj)&&pre.t<T)
{
if(field[pre.i][pre.j]!='@'&&field[curi][curj]!='@'&&!isvisit[curi][curj][curp])
{
isvisit[curi][curj][curp]=true;
int tempt=curt;
tempt+=2;
Node cur1={curi,curj,tempt,curp};
myqueue.push(cur1);
}
if(!isvisit[curi][curj][curp-1]&&curp>0)
{
isvisit[curi][curj][curp-1]=true;
curt+=1;
curp-=1;
Node cur2={curi,curj,curt,curp};
myqueue.push(cur2);
}
}
}
}
if(isfind)
{
printf("Yes, Yifenfei will kill Lemon at %d sec.\n",count);
}
else
{
printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
}
}
int main()
{
//freopen("in.txt","r",stdin);
int icase=0;
int i,j;
while(scanf("%d%d%d%d",&N,&M,&T,&P)!=EOF)
{
int s1,s2;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
cin>>field[i][j];
if(field[i][j]=='Y')
{
s1=i;
s2=j;
}
}
}
memset(isvisit,false,sizeof(isvisit));
cout<<"Case "<<++icase<<":"<<endl;
bfs(s1,s2);
}
return 0;
}
- Waiting ten thousand years for Love
- HDU 2653 Waiting ten thousand years for Love
- HDU 2653 Waiting ten thousand years for Love
- HDU 2653Waiting ten thousand years for Love
- hdoj 2653 Waiting ten thousand years for Love(优先队列+BFS)
- Hdu 2579 Dating with girls(2) && hdu 2653 Waiting ten thousand years for Love【Bfs】
- HDU2653 Waiting ten thousand years for Love (三维广搜+优先队列)
- HDU 2619 - Love you Ten thousand years (数论)
- hdu 2619 Love you Ten thousand years(数论,待解决)
- The Star Zoo——5、Waiting for fifteen thousand years
- ten beautiful sentences for love
- Concurrent Ten Thousand Connections.
- A thousand years
- Ten years. One vision
- 十年,ten years
- The Recent Ten Years
- lessen ten years
- 十年 Ten years
- C++头文件中不应该放哪些东西?
- HTTP Headers 入门
- 链表头插法详解
- Xcode 4.1~4.6 + iOS 5、iOS 6免证书(iDP)开发+真机调试+生成IPA全攻略
- charAt
- Waiting ten thousand years for Love
- 抽象层次
- mysql分表的3种方法[转]
- STL——map用法
- 包装malloc的一些方法
- 开源Android构建工具Buck
- Struts标签学习
- 如何快速的了解某个技术是干什么的
- iOS的View之间的数据传