HDU 3152Obstacle Course (BFS ,记忆化???)

Obstacle Course

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 312    Accepted Submission(s): 187


Problem Description

HDU 3152Obstacle Course (BFS ,记忆化???)_第1张图片
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.

HDU 3152Obstacle Course (BFS ,记忆化???)_第2张图片
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.
 

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
 

Source
2008 ACM-ICPC Pacific Northwest Region

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3152

题意:根据一个n*n的矩阵,找出一条从左上角开始,到达右下角的路,是的经过的数字和最小.

思路:原来用DFS超时,网上说用优先队列+BFS,优先队列不会,直接BFS

AC代码:

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 150
int n;
int dp[maxn][maxn];
int a[maxn][maxn];
int dir[4][2]={0,1,1,0,0,-1,-1,0};
struct node
{
    int x,y;
};
bool OK(int x,int y)
{
    if(x<0||x>=n||y<0||y>=n)
        return false;
    return true;
}
void BFS()
{
    queue<node>q;
    node start,mid;
    dp[0][0]=a[0][0];
    start.x=start.y=0;
    q.push(start);
    while(!q.empty())
    {
        start=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            mid.x=start.x+dir[i][0];
            mid.y=start.y+dir[i][1];
            if(OK(mid.x,mid.y)&&dp[mid.x][mid.y]>dp[start.x][start.y]+a[mid.x][mid.y])
            {
                dp[mid.x][mid.y]=dp[start.x][start.y]+a[mid.x][mid.y];
                q.push(mid);
            }
        }
    }
}
int main()
{
     int kase=0;
    while(cin>>n&&n)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            cin>>a[i][j];
        memset(dp,INF,sizeof(dp));
        BFS();
        cout<<"Problem "<<++kase<<": "<<dp[n-1][n-1]<<endl;
    }
    return 0;
}



你可能感兴趣的:(Course,HDU3152,Obstacle)