哈理工OJ 1286迷宫宝藏(这个bfs有点意思)
2016-03-08 17:07
337 查看
飞机票带你看题: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1286
//这里的代码是我之前A的,大家参考一下思路即可,这个题目的关键在于不用标记是否到达过的点,可以重复去走 #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <limits.h> using namespace std; struct node { int x,y,v; }first,now,next; int way[4][3]={{1,0},{0,1},{0,-1},{-1,0}}; int vis[105][105][105]; ///这个是关键 int n,m,value; char map1[105][105]; int bfs(node first); int main() { int T; scanf("%d%*c",&T); //getchar(); while(T--) { scanf("%d %d%*c",&n,&m); //getchar(); n++; m++; int flag=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%c",&map1[i][j]); if(map1[i][j] == '*') { first.x =i; first.y =j; first.v =0; map1[i][j] = '0'; } if(map1[i][j] == '.') map1[i][j] = '0'; } scanf("%*c");///getchar() } scanf("%d",&value); //printf("The first value is %d\n",value); for(int i=0;i<n;i++) for(int j=0;j<m;j++) for(int k=0;k<105;k++) vis[i][j][k] = 10000; ///下面会说明。 if(value == 0) printf("0\n"); else printf("%d\n",bfs(first)); } return 0; } bool judge(int x,int y) { if(x<0||x>=n||y<0||y>=m||map1[x][y] == '#') return true; return false; } int bfs(node first) { queue <node> q; vis[first.x][first.y][first.v] = 0; //printf("First is %d\n",vis[first.x][first.y][first.v]); //first.v = 0; q.push(first); while(!q.empty()) { now = q.front(); q.pop(); for(int i=0;i<4;i++) { next.x = now.x + way[i][0]; next.y = now.y + way[i][1]; if(judge(next.x,next.y)) continue;///以上为BFS的基础内容 next.v = now.v + map1[next.x][next.y]-'0'; ///可以获得的宝物价值 if(next.v == value) return vis[now.x][now.y][now.v]+1;///vis[x][y][v]数组表示的在(x,y)这点获得v价值所需要的步数 if(next.v > value) continue; if(vis[next.x][next.y][next.v] > vis[now.x][now.y][now.v]+1) ///这里我们已经初始化为1000了,如果你多次以v价值访问(x,y)这点就会判断是否值得,只有将这点于大于上一点+1的步数是才可以压入队列。 { vis[next.x][next.y][next.v] = vis[now.x][now.y][now.v]+1; q.push(next); } } } return -1; }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法