被这道题坑到了,如果单纯的模拟题目所给的步骤,就会出现同一个位置要走两次的情况。。。所以对于bfs来说会很头痛。

第一个代码是wa掉的代码,经过调试才知道这个wa的有点惨,因为前面的操作有可能会阻止后面的正确操作:

#include"iostream"
#include"stdio.h"
#include"algorithm"
#include"cmath"
#include"string.h"
#include"queue"
#define mx 100
using namespace std;
char castle[mx][mx];
int n,m,t,p,sx,sy,ex,ey;
struct node
{
int x,y;
int magic;
int times;
char flag;//用来标记当前位置是@还是.
friend bool operator<(node a,node b)
{
if(b.times!=a.times) return b.times<a.times;
return a.magic<b.magic;
}
};
int dir[][]={{,},{,-},{,},{-,}};
bool judge(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m&&castle[x][y]!='#') return true;
return false;
}
void bfs()
{
node cur,next;
int i;
cur.x=sx;cur.y=sy;cur.times=;cur.magic=p;cur.flag='.';
priority_queue<node>q;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
// cout<<cur.x<<' '<<cur.y<<' '<<cur.magic<<' '<<cur.times<<' '<<cur.flag<<endl;
if(cur.x==ex&&cur.y==ey&&cur.times<=t)
{
cout<<"Yes, Yifenfei will kill Lemon at "<<cur.times<<" sec."<<endl;
return;
}
if(cur.times>t)
{
cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
return;
} for(i=;i<;i++)
{
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
if(judge(next.x,next.y))
{
if(cur.flag=='@'&&cur.magic>)
{
next.times=cur.times+;
next.magic=cur.magic-;
if(castle[next.x][next.y]=='@')next.flag='@';
else next.flag='.';
castle[next.x][next.y]='#';
q.push(next);
}
else if(cur.flag=='.')
{
if(castle[next.x][next.y]=='@')
{
if(cur.magic>)
{
next.times=cur.times+;
next.magic=cur.magic-;
next.flag='@';
castle[next.x][next.y]='#';
q.push(next);
}
}
else
{
next.times=cur.times+;
next.magic=cur.magic;
next.flag='.';
castle[next.x][next.y]='#';
q.push(next);
if(cur.magic>)
{
next.times=cur.times+;
next.magic=cur.magic-;
q.push(next);
} }
}
}
}
}
cout<<"Poor Yifenfei, he has to wait another ten thousand years."<<endl;
}
int main()
{
int c=;
while(scanf("%d%d%d%d",&n,&m,&t,&p)==)
{
int i,j;
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>castle[i][j];
if(castle[i][j]=='Y') {sx=i;sy=j;castle[i][j]='#';}
else if(castle[i][j]=='L'){ex=i;ey=j;castle[i][j]='.';}
}
cout<<"Case "<<++c<<":"<<endl;
bfs();
}
return ;
}

参考大神的解法后才知道这题在一开始就应该对步骤简化,因为只有前后两步都是’.'是才可以用走的,。其他任何情况下都要用飞的,所以没有必要像之前那样分

很多种情况讨论。果然编程之前还是要多思考思考尽量简化步骤,这样不仅可以提高效率,而且还可以避免发生不必要的错误。

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char g[][];
bool vis[][][];
int n,m,p,t,si,sj,ans;
int dir[][]={{,},{,},{-,},{,-}};
struct node
{
int step,p,x,y;
node(int a=,int b=,int c=,int d=):x(a),y(b),p(c),step(d){}
bool friend operator <(const node a,const node b)
{
return a.step>b.step;
}
};
void BFS()
{
priority_queue<node> Q;
node f=node(si,sj,p,);
Q.push(f);
memset(vis,false,sizeof(vis));
vis[si][sj][p]=true;
node temp;
while(!Q.empty())
{
temp=Q.top();
Q.pop();
if(temp.step>t)
return ;
if(g[temp.x][temp.y]=='L')
{
ans=temp.step;
return ;
}
for(int k=;k<;k++)
{
int i=dir[k][]+temp.x;
int j=dir[k][]+temp.y;
if(i<||i>n- || j< || j>m-||g[i][j]=='#')
continue;
if(temp.p!= && !vis[i][j][temp.p-])
{
vis[i][j][temp.p-]=true;
Q.push(node(i,j,temp.p-,temp.step+));
}
if(g[temp.x][temp.y]!='@' && g[i][j]!='@'&&!vis[i][j][temp.p])
{
vis[i][j][temp.p]=true;
Q.push(node(i,j,temp.p,temp.step+));
}
}
}
return ;
}
int main()
{
int cas=;
while(scanf("%d %d %d %d",&n,&m,&t,&p)==)
{
for(int i=;i<n;i++)
{
scanf("%s",g[i]);
for(int j=;j<m;j++)
if(g[i][j]=='Y')
si=i,sj=j;
}
ans=;
BFS();
printf("Case %d:\n",++cas);
if(ans>t)
printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
else printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
}
return ;
}

hdu Waiting ten thousand years for Love的更多相关文章

  1. HDU 2653 - Waiting ten thousand years for Love

    首先,对于一个 '@' 飞上去,飞下来都要耗1点魔力,所以是两点= = 然后站在同一格 魔力可能不同,所以要增加一维. 还有当前搜到的不一定是最小. 别的也没啥. #include <iostr ...

  2. hdu 4541 Ten Googol

    http://acm.hdu.edu.cn/showproblem.php?pid=4541 打表找规律 #include <cstdio> #include <cstring> ...

  3. hdu-2619 Love you Ten thousand years

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2619 题目大意: 求出小于n的数的个数,满足ki mod n,1≤i≤n是模n的完全剩余系 解题思路 ...

  4. HDU2653 BFS+优先队列

    Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/3 ...

  5. hdu2653之BFS

    Waiting ten thousand years for Love Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  7. AssetBundle loading failed because.....已解决

    http://blog.csdn.net/ldghd/article/details/9632455 *****************************      一      ******* ...

  8. 【英语魔法俱乐部——读书笔记】 3 高级句型-简化从句&倒装句(Reduced Clauses、Inverted Sentences) 【完结】

    [英语魔法俱乐部——读书笔记] 3 高级句型-简化从句&倒装句(Reduced Clauses.Inverted Sentences):(3.1)从属从句简化的通则.(3.2)形容词从句简化. ...

  9. sentence patterns

    第四部分     推理题 1.世界上每个角落的每个人都有立场,都有背景,都有推理性,能推理出一个人语言的真意,才成就了真正的推理能力: 2.换言之,如果你能通过一个人的说话推理出其身份职业,你的推理能 ...

随机推荐

  1. android.mk文件里的通配符

    比方你有如下目录,要编译Classes目录和Code目录下所有cpp src |-android.mk |-Classes |-A.cpp |-B.cpp |-....cpp |-Code |-E.c ...

  2. [SVN(ubuntu)] ubuntu使用svn

    转载自:http://lee2013.iteye.com/blog/1058047 SVN作为日常开发中不可缺少的工具,Ubuntu下的SVN安装十分简单,sudo apt-get install s ...

  3. hadoop启动后jps没有namenode(转)

    hadoop启动后jps没有namenode 一般都是由于两次或两次以上格式化NameNode造成的,有两种方法可以解决: 1.删除DataNode的所有资料 2.修改每个DataNode的names ...

  4. jar包和war包的区别

    jar包和war包的区别: jar包就是别人已经写好的一些类,然后将这些类进行打包,你可以将这些jar包引入你的项目中,然后就可以直接使用这些jar包中的类和属性了,这些jar包一般都会放在lib目录 ...

  5. 使用javascript的stack数据结构,实现进制转换

    function Stack() { var items = []; this.push = function(element){ items.push(element); } this.pop = ...

  6. ASP.NET 数据库访问通用工具

    在工作中,有很多项目已上线后,很多项目的数据库服务器都不会对外开放的,外网想直接访问客户数据库服务器时,可能会出现困难. 这时就需要一个可以查询,更新数据库操作的页面了: 本来用sql语句直接操作数据 ...

  7. BC#32 1002 hash

    代码引用kuangbin大神的,膜拜 第一次见到hashmap和外挂,看来还有很多东西要学 维护前缀和sum[i]=a[0]-a[1]+a[2]-a[3]+…+(-1)^i*a[i] 枚举结尾i,然后 ...

  8. WAP网页输入框的默认键盘类型控制

    最近有用户反映手机网的输入框不够人性化,比如手机号.卡号输入框应该默认显示数字键盘,邮箱输入框应该默认显示邮箱键盘. 百度上对这样的资料介绍很多,基本上都和这个页面是一个意思 http://www.w ...

  9. 在C# 6中实践模式匹配

    模式匹配(Pattern Matching)是F#中非常好用的一种语言特性.估计很多人都希望在C#中能用到这样的特性. 一句话解释一下模式匹配就是:创建一个函数可以接受和处理不同类型的表达式(包括不同 ...

  10. C语言中,宏和全局变量的区别是什么?

    全局变量 是可以在程序中任何地方使用 而且是可以修改的 宏定义也可以在任何地方使用 但是不能在之后修改 数据类型没有限制的 宏的例子:#define 宏名 宏体 #define PI 3.141592 ...