The shortest path-the apprentice's way down the mountain

Topic: The
Apprentice’s Way Down the Mountain

Finally, when it comes to the day of becoming a teacher, the apprentice needs to find a way down the mountain that takes the least time from the Pure Yang Palace on the top of the mountain.
This mountain is represented by a triangle. From the top of the mountain, there are n levels down. The i-th level has i movable positions, and the exit is on the left of the lowest level. For example, there is a 5-story mountain:

In this example, the exit is the position of the leftmost 1 on the bottom floor. Each number on the mountain represents the time it takes for the student to pass this location, and the total time for the student to descend is the sum of the numbers on the path.

The apprentice can move to the left, right, lower left, and lower right every time. For example, when the apprentice is at the number 5 on the third floor, he can walk left to 4 and right to 6; he can also walk down to 1 to Go down to the right to 7; when the
apprentice is at the number 6 on the third floor, you can go to the left to 5, but not to the right; you can also go to the left and go to 7 and go to the right to 8;

Input format In the
first line, an integer n represents the height of the mountain. In the
next n lines, there are i integers in the i-th line, representing the time it takes for the student to pass through this location (the time spent passing each location does not exceed 10).

Data constraints
For 20% of data, n=4
For 60% of data, n<=10
For 100% of data, n<=1000

Output format
The minimum time it takes for the apprentice to go down the mountain

Sample
input
5
1
3 2
4 5 6
9 1 7 8
1 1 4 5 6
output
11

Idea: At
first I thought it was a simple dynamic programming solution, but it turned out that it was not like this, because you can walk from left to right, and from right to left, here dynamic planning will be processed in a loop. So I can only change my mind.
In fact, this is a simple shortest path problem, so just use the shortest path method to solve it.
I set a dijkstra, but the last three points timed out. Don't know the reason, put the code first.

#include <iostream>
#include <queue>
using namespace std;
int map[1001][1001];
int d[1001][1001];
int dy[4] = {
    
    -1, 1, 0, 1}, dx[4] = {
    
    0, 0, 1, 1};
struct P {
    
    
    int x, y, cost;
    bool operator<(const P &a) const {
    
     return cost < a.cost; }
};
struct cmp {
    
    
    bool operator()(P a, P b) {
    
     return a.cost < b.cost; }
};
int main() {
    
    
    int n;
    cin >> n;
    fill(map[0], map[0] + 1001 * 1001 + 1, 0x3f3f3f3f);
    fill(d[0], d[0] + 1001 * 1001 + 1, 0x3f3f3fff);
    for (int i = 1; i <= n; i++) {
    
    
        for (int j = 1; j <= i; j++) {
    
    
            scanf("%d", &map[i][j]);
        }
    }
    d[1][1] = map[1][1];
    priority_queue<P, vector<P>, cmp> q;
    P tmp;
    tmp.x = 1, tmp.y = 1, tmp.cost = map[1][1];
    q.push(tmp);
    while (q.size()) {
    
    
        P p = q.top();
        q.pop();
        if (d[p.x][p.y] < p.cost)
            continue;
        for (int i = 0; i < 4; i++) {
    
    
            int tx = p.x + dx[i], ty = p.y + dy[i];
            if (d[tx][ty] > map[tx][ty] + p.cost && tx >= 1 && ty >= 1) {
    
    
                d[tx][ty] = map[tx][ty] + p.cost;
                tmp.x = tx, tmp.y = ty, tmp.cost = d[tx][ty];
                q.push(tmp);
            }
        }
    }
    cout << d[n][1] << endl;
    return 0;
}

Put on the code of the boss:

#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
int mapping[1005][1005],dp[1005][1005];
int directX[5]={
    
    0,0,1,1};
int directY[5]={
    
    -1,1,0,1};//方向数组
struct point{
    
    
    int x,y,time;
}x;
int main(){
    
    
    int n,i,j;
    cin>>n;
    for(i=1;i<=n;i++)
    for(j=1;j<=i;j++) scanf("%d",&mapping[i][j]);
    
    x.x=x.y=1;
    x.time=dp[1][1]=mapping[1][1];
    queue<point> que;
    que.push(x);
    while(!que.empty()){
    
    //bfs
        x=que.front();
        que.pop();
        if(x.time>dp[x.x][x.y]) continue;
        for(i=0;i<4;i++){
    
    
            x.x+=directX[i];
            x.y+=directY[i];
            x.time+=mapping[x.x][x.y];//新结点
            if(mapping[x.x][x.y]&&(dp[x.x][x.y]>x.time||!dp[x.x][x.y])){
    
    //(没有越界&&(可以更新||没更新过))
                que.push(x);
                dp[x.x][x.y]=x.time;
            }    
            x.time-=mapping[x.x][x.y];
            x.x-=directX[i];
            x.y-=directY[i];    //还原为原结点
        }
    }

    cout<<dp[n][1]<<endl;
    return 0;
} 

Guess you like

Origin blog.csdn.net/DwenKing/article/details/107884242