"Checkerboard DP" "OpenJudge7614" minimum tolls

Source: Internet
Author: User

Minimum toll total time limit: 1000ms memory limit: 65536kB "description"

A businessman walks through a n*n square grid to attend a very important business event. He's going to go from the top left corner of the grid and out the bottom right corner. It takes 1 units of time to cross 1 squares in the middle of each one. The businessman must cross out at (2n-1) a unit time. And in the middle of every small box, you need to pay a certain fee.

The businessman expected to cross out at a minimum cost within the stipulated time. How much do I need at least?

Note: You cannot cross the small squares diagonally (that is, you can only move up and down in four directions and not leave the grid).

The first line of input is an integer representing the width of the square n (1 <= n < 100);
The next n rows, each row n an integer not greater than 100, are the cost of each small square on the grid. Output at least the required cost.

"Sample Input"
"Sample Output"
109
Prompted

In the sample, the minimum value is 109=1+2+5+7+9+12+19+21+33.

"Solution"

Water problem together, because to pass in the time of 2n-1, so each add the top and left of the minimum value. But in the beginning did not take into account the status of the situation is covered, only opened an array WA for a long time. Open two arrays one in the initial state after a storage and transfer state can be. rib[i][j]=min (rib[i-1][j],rib[i][j-1]) +board[i][j].

AC Code:

  

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 Const intINF =2147483647 ;5 intN;6 intboard[ the][ the],rib[ the][ the] ;7 intMain () {8scanf"%d",&N);9      for(intI=1; i<=n;++i)Ten          for(intj=1; j<=n;++j) Onescanf"%d",&board[i][j]); A      for(intI=0; i<=n+1; ++i) {board[i][0]=inf;board[0][i]=inf; rib[i][0]=inf; rib[0][i]=INF;} -      for(intI=1; i<=n;++i) -          for(intj=1; j<=n;++j) { the             if(i==1&&j==1)Continue; -Rib[i][j]+=min (rib[i-1][j],rib[i][j-1])+Board[i][j]; -         } -printf"%d", rib[n][n]+board[1][1]) ; +     return 0; -}

"Checkerboard DP" "OpenJudge7614" minimum tolls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.