AOJ.866 飞越原野 (三维BFS)

题意分析

点我挑战题目

相比于普通的BFS,要多一维来记录当前剩余的体力。而且还要额外的一层循环来处理,飞过的路程。

代码总览

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define INF 0x3f3f3f3f
#define nmax 200
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
bool visit[nmax][nmax][nmax],isfind = false;
char mp[nmax][nmax];
struct nod{
int x,y,e;
}node[nmax];
int m,n,o,ans;
int spx[] = {0,1,0,-1};
int spy[] = {1,0,-1,0};
bool check(int x, int y, int e)
{
if(x<0 || x >= m || y<0 || y>=n || mp[x][y] =='L' || visit[x][y][e] == true)
return false;
else
return true;
}
void bfs()
{
nod temp;
temp.x = 0; temp.y = 0; temp.e = o;
visit[temp.x][temp.y][temp.e] = true;
queue<nod> q;
if(!q.empty()) q.pop();
q.push(temp);
ans = 0;
while(!q.empty()){
int nowsize = q.size();
while(nowsize--){
nod tep = temp = q.front();
q.pop();
// get final or not
if(tep.x == m-1 && tep.y == n-1){
isfind = true;
break;
}
for(int i = 0 ;i<4;++i){
tep.x =temp.x + spx[i];
tep.y =temp.y + spy[i];
if(check(tep.x,tep.y,tep.e)){
visit[tep.x][tep.y][tep.e] = true;
q.push(tep);
}
}
for(int i = 0; i<4;++i){
for(int j = 1; j<=temp.e;++j){
tep.x = temp.x + spx[i] *j;
tep.y = temp.y + spy[i] *j;
tep.e = temp.e - j;
if(check(tep.x,tep.y,tep.e)){
visit[tep.x][tep.y][tep.e] = true;
q.push(tep);
}
}
}
}
if(isfind) break;
ans++;
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d %d",&m,&n,&o) != EOF){
MEM(visit);
isfind = false;
for(int i = 0; i<m;++i)
scanf("%s",mp[i]);
bfs();
if(isfind) printf("%d\n",ans);
else printf("impossible\n");
}
return 0;
}

最新文章

  1. JS 获取CSS属性值
  2. Windows10更新提示语言不同不能保留程序和设置
  3. 类似title的鼠标跟随事件
  4. BZOJ1976: [BeiJing2010组队]能量魔方 Cube
  5. Android中半透明Activity效果另法
  6. The Building Blocks-Enterprise Applications Part 2- Information Management and Business Analytics
  7. SQL万能语句-经典操作
  8. Java SE 8 流库
  9. width和max-width的用处
  10. MAVEN day04 SSH之分模块开发
  11. Luffy之支付宝支付开发API
  12. 微服务架构与实践3_api
  13. 【codeforces 623E】 Transforming Sequence
  14. Mysql5.6 make 错误以及解决办法
  15. myeclipse 10激活,本人已测试过可行
  16. Spring Security构建Rest服务-0600-SpringSecurity基本原理
  17. Git教程之工作区和暂存区
  18. Java并发(八):AbstractQueuedSynchronizer
  19. MySql在Linux上实现每天自动备份
  20. 使用django rest framework写POST和GET接口

热门文章

  1. Odd CSS syntax. [class^=&#39;icon-&#39;], [class*=&#39; icon-&#39;]
  2. Linux用户切换和密码修改
  3. Qt listwigwt item 加入自定义元素
  4. Linux命令应用大词典-第26章 模块和内核管理
  5. GameplayKit的GKStateMachine用法与实例
  6. 三个线程ABC,交替打印ABC
  7. [Clr via C#读书笔记]Cp12泛型
  8. 《机器学习实战》笔记——决策树(ID3)
  9. facebook演讲
  10. leetcode个人题解——two sum