Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define CR 12
  4. #define CC 102
  5. #define MAX 2147483646
  6.  
  7. int map[CR][CC];
  8. int R,C;
  9.  
  10. int S[CR][CC];
  11. int Path[CR][CC];
  12.  
  13. void ini(void){
  14.     int i,j;
  15.     for(i = 0; i < CR;i++)
  16.         for(j =0; j < CC;j++){
  17.             S[i][j] = -1;
  18.             Path[i][j] = -1;
  19.         }
  20. }
  21.  
  22. int Go[3][2] = {{0,+1},{+1,+1},{-1,+1}};
  23.  
  24.  
  25.  
  26. int Dp(int r,int c,int p){
  27.     int i;
  28.     int cr = MAX;
  29.     if(S[r][c] != -1) return S[r][c];
  30.     int cost = map[r][c];
  31.     bool t = true;
  32.     for(i = 0; i < 3; i++){
  33.         int nr = Go[i][0] + r;
  34.         int nc = Go[i][1] + c;
  35.         if(nr < 0 || nr >= R || nc < 0 || nc >= C) continue;
  36.         t = false;
  37.         int nrr = cost +  Dp(nr,nc,c);
  38.         if(nrr < cr) cr = nrr;
  39.     }
  40.     if(r == 0){
  41.         t = false;
  42.         int ct = cost + Dp(R,c+1,c);
  43.         if(ct < cr) cr = ct;
  44.     }
  45.     else if(r == R-1){
  46.         t = false;
  47.         int ct = cost + Dp(0,c+1,c);
  48.         if(ct < cr) cr = ct;
  49.     }
  50.  
  51.  
  52.     if(t) {
  53.         cr = cost;
  54.     }
  55.     S[r][c] = cr;
  56.     return S[r][c];
  57.  
  58. }
  59.  
  60. int main(){
  61.     scanf("%d %d",&R,&C);
  62.     int i,j;
  63.     ini();
  64.     for(i = 0; i < R; i++)
  65.         for(j = 0; j < C; j++)
  66.             scanf("%d",&map[i][j]);
  67.         int inf = MAX;
  68.         for(i = 0; i < R; i++){
  69.             int RE = Dp(i,0,i);
  70.             if(RE < inf)
  71.                 inf = RE;
  72.         }
  73.         printf("%d\n",inf);
  74.  
  75.        
  76.  
  77.    
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement