您的位置:首页 > 其它

HDOJ 题目3152 Obstacle Course(BFS,优先队列优化)

2015-02-07 22:12 645 查看

Obstacle Course

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 202 Accepted Submission(s): 122



[align=left]Problem Description[/align]



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.

[align=left]Input[/align]
Each problem is specified by a single integer between 2 and 125 giving the number of rows and columns in the
N * N square matrix. The file is terminated by the case
N = 0.

Following the specification of N you will find N lines, each containing
N numbers. These numbers will be given as single digits, zero through nine, separated by single blanks.

[align=left]Output[/align]
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).

[align=left]Sample Input[/align]

3
5 5 4
3 9 1
3 2 7
5
3 7 2 0 1
2 8 0 9 1
1 2 1 8 1
9 8 9 2 0
3 6 5 1 5
7
9 0 5 1 1 5 3
4 1 2 1 6 5 3
0 7 6 1 6 8 5
1 1 7 8 3 2 3
9 4 0 7 6 4 1
5 8 3 2 4 8 3
7 4 8 4 8 3 4
0


[align=left]Sample Output[/align]

Problem 1: 20
Problem 2: 19
Problem 3: 36


[align=left]Source[/align]
2008 ACM-ICPC Pacific Northwest Region

[align=left]Recommend[/align]
chenrui | We have carefully selected several similar problems for you: 3149 3156 3155 3154 3153
题目大意:从左上角走到右下角,问最小的路径。走过的点可以重复走,
实际上不用优先队列也能过,上边那个是用普通队列的

ac代码
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<iostream>
using namespace std;
int map[220][220],v[220][220];
int n;
struct s
{
int x,y,d;
friend bool operator <(s b1,s b2)
{
return b1.d > b2.d;
}
}a,t;
int dx[4]={0,-1,0,1};
int dy[4]={1,0,-1,0};
int bfs()
{
priority_queue<struct s>q;
//queue<struct s>q;
a.x=0;
a.y=0;
a.d=map[0][0];
q.push(a);
memset(v,-1,sizeof(v));
while(!q.empty())
{
a=q.front();
q.pop();
for(int i=0;i<=4;i++)
{
t.x=a.x+dx[i];
t.y=a.y+dy[i];
if(t.x<0||t.x>=n||t.y<0||t.y>=n)
continue;
t.d=a.d+map[t.x][t.y];
if(t.d<v[t.x][t.y]||v[t.x][t.y]==-1)
{
v[t.x][t.y]=t.d;
if(t.x!=n-1||t.y!=n-1)
q.push(t);
}
}
}
return v[n-1][n-1];
}
int main()
{
//	int n;
int c=0;
while(scanf("%d",&n)!=EOF,n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&map[i][j]);
}
printf("Problem %d: %d\n",++c,bfs());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: