Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:07:09+00:00 2026-06-04T13:07:09+00:00

We have NXM grid. One square of the grid is source and one is

  • 0

We have NXM grid. One square of the grid is source and one is destination. Each square of grid including source and destination have some elevation(an integer from value 0-9). We have to find a minimum cost path from source to destination satisying following restrictions:

  1. The path must be continuous i.e. between adjacent squares only (not diagonal adjacency).
  2. One can go from higher elevation to lower elevation only.

The elevation of any square can be increased or decreased. The amount by which elevation changes is counted as cost. If elevation does not change, it is taken as zero cost.
So total cost of path from source to destination is the change in elevations of the squares that comes in the path. Moreover elevation of source can’t be changed but that of destination can be.

I tried to apply some algorithm like Djikstra and APSP but could not reach any solution. Please help me in this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:07:10+00:00Added an answer on June 4, 2026 at 1:07 pm

    This is an example of simple shortest distance problem with another dimension, try to formulate the problem like this,
    cost[n][m][max_height] = {INFINITY};

    cost[srcX][srcY][ height[srcX][srcY] ] = 0;

    now cost[x+1][y][ht] = min(cost[x+1][y][ht], cost[x][y][q_ht] + (q_ht – ht) ) for q_ht varies from max_height to ht. The idea is to reach at (x+1,y,ht) with least cost from any allowable heights(ie height >= ht). This again we need to calculate for for all ht(0 to max_height). The full implementation is down here:-

    #define HIGHVAL 100000000
    #define XI (x + a[i])
    #define YI (y + b[i])
    
    int n,m;
    
    bool isvalid(int x,int y)
    {
        return (x>=0 && y>=0 && x<n && y<m);
    }
    
    int main()
    {
    
        int pondX, pondY;
        int farmX, farmY;
    
        cin>>n>>m>>pondX>>pondY>>farmX>>farmY;
        pondX--, pondY--, farmX--, farmY--;
    
        int height[n][m];
        string s;
    
        for(int i=0; i<n; i++)
        {
            cin>>s;
            for(int j=0; j<m; j++)
                height[i][j] = (int)(s[j] - '0');
        }
    
        int ht = height[pondX][pondY];
        int cost[n][m][ht+1];
        bool visited[n][m];
    
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                for(int k=0; k<ht+1; k++)
                    cost[i][j][k] = HIGHVAL;
    
        cost[pondX][pondY][ht] = 0;
        int a[4]= {0,0,1,-1};
        int b[4]= {1,-1,0,0};
    
        int ci = pondX, cj = pondY;
        queue<int> qx;
        queue<int> qy;
    
        visited[pondX][pondY] = 1;
    
        memset(visited, 0, sizeof(visited));
        qx.push(pondX);
        qy.push(pondY);
    
        while(qy.size())
        {
            int x = qx.front();
            int y = qy.front();
    
            qx.pop();
            qy.pop();
    
            for(int i=0; i<4; i++)
            {
                int temp = 0;
                if(isvalid(XI, YI))
                {
                    if(!visited[XI][YI])
                    {
                        qx.push(XI);
                        qy.push(YI);
                        visited[XI][YI] = 1;
                        temp = 1;
                    }
    
                    for(int j=ht; j>=0; j--)
                    {
                        int q = HIGHVAL;
                        for(int k=ht; k>=j; k--)
                        {
                            q = min(q, cost[x][y][k] + abs(j - height[XI][YI]));
                        }
    
                        if(cost[XI][YI][j] > q)
                        {
                            cost[XI][YI][j] = q;
    
                            if(visited[XI][YI] && !temp)
                            {
                                qx.push(XI);
                                qy.push(YI);
                            }
                        }
                    }
                }
    
            }
        }
    
        int ans=HIGHVAL;
        for(int i=0; i<=ht; i++)
            ans = min(ans, cost[farmX][farmY][i]);
    
        cout<<ans;
        //cout<<" "<<n<<m;
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have NxM matrix with integer elements, greater or equal than 0. From any
here's what we have today: * NxM grid of points in 3D * we
I have multiple NxM matrices and want to select one of them by a
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have some runtime defined schedule for one week, for example: <schedule> <s day=Monday, time=1:00:00></s>
I have two abstract classes called Robot and GeometricElement . Each one of them
I have a nxm matrix and I need to find the maximum of sum
I have a problem in MATLAB. I have a NxM logical array called L
Have deployed numerous report parts which reference the same view however one of them
I have an NxM matrix in MATLAB that I would like to reorder in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.