比赛的时候是到最后一个点就直接跳出来了,但是这样的做法是适用于求最小步数的时候,但是这里不是。
所以只能是把所有的情况都遍历一遍。
Obstacle Course
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 177 Accepted Submission(s): 101
Problem Description
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 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.
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.
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
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
Sample Output
Problem 1: 20 Problem 2: 19 Problem 3: 36
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
using namespace std;
#define MAXN 130
int n;
int cas=1;
struct node{
int t_use;
int x,y;
};
int mp[MAXN][MAXN];
int mark[MAXN][MAXN];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
void BFS(){
queue<node>q;
node front,rear;
int i;
int xx,yy;
front.x = 0;
front.y = 0;
front.t_use = mp[0][0];
q.push(front);
while(!q.empty()){
front = q.front();
q.pop();
for(i=0;i<4;i++){
xx = front.x + dx[i];
yy = front.y + dy[i];
if(xx<0 || xx>=n || yy<0 || yy>=n){
continue;
}
rear.x = xx;
rear.y = yy;
rear.t_use = front.t_use + mp[xx][yy];
if((rear.t_use<mark[xx][yy]) || (mark[xx][yy]==-1)){
mark[xx][yy] = rear.t_use;
if(xx!=n-1 || yy!=n-1){
q.push(rear);
}
}
}
}
printf("Problem %d: %d\n",cas++,mark[n-1][n-1]);
}
int main(){
int i,j;
int cas = 1;
while(~scanf("%d",&n)){
if(n == 0){
break;
}
memset(mark,-1,sizeof(mark));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&mp[i][j]);
}
}
mark[0][0] = mp[0][0];
BFS();
}
return 0;
}