鸣人与佐助 poj
来源:互联网 发布:线切割如何编程链轮 编辑:程序博客网 时间:2024/05/01 06:37
典型的bfs
技巧在于设置了G[x][y]来进行剪枝,减少了运行时间与空间。即如果在某一点,已经有大于当前的查克拉的路线,就不再继续走这一点。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <algorithm>using namespace std;int const maxn=205;int G[maxn][maxn];char g[maxn][maxn];int beginx,beginy,endx,endy;int dx[4]={1,0,-1,0};int dy[4]={0,1,0,-1};struct Point{ int x,y,chak,times; Point(int x_,int y_,int chak_,int times_):x(x_),y(y_),chak(chak_),times(times_){}};int main(){ int m,n,total; cin>>m>>n>>total; cin.get(); for(int i=0;i<m;i++){ cin.getline(g[i],maxn); for(int j=0;j<n;j++){ if(g[i][j]=='@') { beginx=i; beginy=j; } if(g[i][j]=='+'){ endx=i; endy=j; } G[i][j]=-1; } } G[beginx][beginy]=total; queue<Point> p; p.push(Point(beginx,beginy,total,0)); int ans=1<<30; while(!p.empty()){ Point temp=p.front(); if(temp.x==endx&&temp.y==endy){ ans=temp.times; break; } for(int i=0;i<4;i++){ int tx=temp.x+dx[i]; int ty=temp.y+dy[i]; if(tx>=0 && tx<m && ty>=0 && ty<n && temp.chak>G[tx][ty]){ if(g[tx][ty]=='#' && temp.chak>0){ p.push(Point(tx,ty,temp.chak-1,temp.times+1)); G[tx][ty]=temp.chak-1; } else if(g[tx][ty]=='*' || g[tx][ty]=='+'){ p.push(Point(tx,ty,temp.chak,temp.times+1)); G[tx][ty]=temp.chak; } } } p.pop(); } if(ans!=1<<30) cout<<ans; else cout<<-1; }
0 0
- 鸣人与佐助 poj
- POJ 鸣人和佐助
- poj 4115:鸣人和佐助
- poj 2593 与 poj 2479
- POJ 1004 与 iomanip
- poj 2424 餐厅与客人
- POJ 题目分类与计划
- poj 3471 整除与剩余
- poj 2816 红与黑
- POJ 1308 与 HDU 1325
- POJ 题目分类与计划
- POJ 2703:骑车与走路
- POJ 题目分类与计划
- hdu与poj题目分类
- 最短路中汇率问题 POJ 1860 与 POJ 2240
- poj 2318 与 poj 2398计算几何 叉乘积运用
- POJ 1012 Joseph题解与心得
- 双连通分支与桥 POJ 3694
- CSDN安家落户
- test
- 最新2017(Android)安卓面试题级答案(精选版)
- 1001
- 携带结果的任务callable与future
- 鸣人与佐助 poj
- Evaluate Division
- Linux_C笔记day05
- iOS 安全模型浅析(二) ---- 沙盒机制
- 烤鸡
- HTTP协议GET和POST请求对比
- EXCEL大数据导出(100W条)
- CodeForces
- LeetCode之Remove Duplicates from Sorted Array II