ACM-icpc---bfs求最短路
来源:互联网 发布:新页进销存软件免费 编辑:程序博客网 时间:2024/05/22 08:20
You are working on the team assisting with programming for the Mars rover. To conserve energy, the rover needs to find optimal paths across the rugged terrain to get from its starting location to its final location. The following is the first approximation for the problem.
N * N square matrices contain the expenses for traversing each individual cell. For each of them, your task is to find the minimum-cost traversal from the top left cell [0][0] to the bottom right cell [N-1][N-1]. Legal moves are up, down, left, and right; that is, either the row index changes by one or the column index changes by one, but not both.
Input
Each problem is specified by a single integer between 2 and 125 giving the number of rows and columns in theN * N square matrix. The file is terminated by the caseN = 0.
Following the specification of N you will find N lines, each containingN numbers. These numbers will be given as single digits, zero through nine, separated by single blanks.
Following the specification of N you will find N lines, each containingN numbers. These numbers will be given as single digits, zero through nine, separated by single blanks.
Output
Each problem set will be numbered (beginning at one) and will generate a single line giving the problem set and the expense of the minimum-cost path from the top left to the bottom right corner, exactly as shown in the sample output (with only a single space after "Problem" and after the colon).
Sample Input
题意:给出一个n阶的矩阵,求从a[0][0]到a[n-1][n-1]的最短距离;
35 5 43 9 13 2 753 7 2 0 12 8 0 9 11 2 1 8 19 8 9 2 03 6 5 1 579 0 5 1 1 5 34 1 2 1 6 5 30 7 6 1 6 8 51 1 7 8 3 2 39 4 0 7 6 4 15 8 3 2 4 8 37 4 8 4 8 3 40
Sample Output
Problem 1: 20Problem 2: 19Problem 3: 36#include<cstdio>#include<queue>#include<cstring>#include<string>#include<algorithm>using namespace std;int dx[]= {-1,0,1,0};int dy[]= {0,1,0,-1};int a[150][150],vis[150][150];int n,ans;struct A{ int x,y; int dis; friend bool operator <(A a,A b) { return a.dis>b.dis; }};void bfs(){ priority_queue<A> Q; A f; f.x=0; f.y=0; f.dis=a[0][0]; vis[0][0]=1; Q.push(f); while(!Q.empty()) { f=Q.top(); Q.pop(); if(f.x==n-1&&f.y==n-1) { ans=f.dis; return; } for(int i=0;i<4; i++) { A p=f; p.x+=dx[i]; p.y+=dy[i]; p.dis+=a[p.x][p.y]; if(p.x>=0&&p.x<n&&p.y>=0&&p.y<n&&!vis[p.x][p.y]) { vis[p.x][p.y]=1; Q.push(p); } } }}int main(){ int cas=1; while(~scanf("%d",&n),n) { memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) for(int j=0; j<n; j++) scanf("%d",&a[i][j]); ans=0; bfs(); printf("Problem %d: %d\n",cas++,ans); } return 0;}
0 0
- ACM-icpc---bfs求最短路
- 魔戒-BFS求最短路
- UVALive7015(bfs求最短路)
- ACM: 方向dfs + bfs求最短路 poj 3…
- hdu 2354(bfs求最短路)
- POJ2243 Knight moves (BFS求最短路)
- poj 2215 bfs求最短路
- hdu 1242 用bfs求最短路
- POJ2251(BFS求最短路)
- POJ 3170(bfs求最短路)
- BFS(广搜)算法与模板 bfs求最短路
- dfs求连通性,bfs求迷宫最短路
- poj 3126 Prime Path bfs求最短路
- hdu 3760(2次bfs求最短路)
- poj3984~用bfs求最短路~同时记录路径
- AC自动机+状态压缩dp+BFS求最短路+hdu3247
- POJ2225 Asteroids! (三维的BFS求最短路)
- HDU 1495 非常可乐 (BFS求最短路)
- Websocket原理
- 王歌:随时随地练习腹部呼吸减肥方法!
- 动态、静态编译以及MD、MDd、MT、MTd编译
- 杭电3699A hard Aoshu Problem DFS 搜索
- UML类图关系(泛化 、继承、实现、依赖、关联、聚合、组合)
- ACM-icpc---bfs求最短路
- mybatis系列四:mybatis 动态sql语句
- mysql组织机构树迭代查询
- (13.1.3.5)PMBOK之三:十大知识领域之质量管理
- c语言总结(二)
- Windows下Vue开发环境搭建及相关问题
- JavaScript初级玩法(4)—两数交换的多种方法
- 生活习惯和身材一起改变,改掉肚子不饿也要吃饭的习惯!
- ffmpeg 从内存中读取数据(或将数据输出到内存)