Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #define CR 12
- #define CC 102
- #define MAX 2147483646
- int map[CR][CC];
- int R,C;
- int S[CR][CC];
- int Path[CR][CC];
- void ini(void){
- int i,j;
- for(i = 0; i < CR;i++)
- for(j =0; j < CC;j++){
- S[i][j] = -1;
- Path[i][j] = -1;
- }
- }
- int Go[3][2] = {{0,+1},{+1,+1},{-1,+1}};
- int Dp(int r,int c,int p){
- int i;
- int cr = MAX;
- if(S[r][c] != -1) return S[r][c];
- int cost = map[r][c];
- bool t = true;
- for(i = 0; i < 3; i++){
- int nr = Go[i][0] + r;
- int nc = Go[i][1] + c;
- if(nr < 0 || nr >= R || nc < 0 || nc >= C) continue;
- t = false;
- int nrr = cost + Dp(nr,nc,c);
- if(nrr < cr) cr = nrr;
- }
- if(r == 0){
- t = false;
- int ct = cost + Dp(R,c+1,c);
- if(ct < cr) cr = ct;
- }
- else if(r == R-1){
- t = false;
- int ct = cost + Dp(0,c+1,c);
- if(ct < cr) cr = ct;
- }
- if(t) {
- cr = cost;
- }
- S[r][c] = cr;
- return S[r][c];
- }
- int main(){
- scanf("%d %d",&R,&C);
- int i,j;
- ini();
- for(i = 0; i < R; i++)
- for(j = 0; j < C; j++)
- scanf("%d",&map[i][j]);
- int inf = MAX;
- for(i = 0; i < R; i++){
- int RE = Dp(i,0,i);
- if(RE < inf)
- inf = RE;
- }
- printf("%d\n",inf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement