Hdu1078 memory-based search

Source: Internet
Author: User

Hdu1078 memory-based search

Question:

For n * n maps, each grid has a value that can only go from a small grid to a large grid. k can go up, down, and up to k steps each time to ask you about the maximum number of grids you have taken. what is

Dp [I] [j] indicates the maximum value that can be obtained starting from (I, j) and then deeply searches for the solution.

#include
 
  #include
  
   #include
   
    using namespace std
    ;int n
    ,k
    ,dp
    [110
    ][110
    ],cost
    [110
    ][110
    ];int dir
    [4
    ][2
    ]={0
    ,1
    ,0
    ,-1
    ,1
    ,0
    ,-1
    ,0
    };int mark
    [110
    ][110
    ];int max
    (int a
    ,int b
    ){    return a
    >b
    ?a
    :b
    ;}int dfs
    (int x
    ,int y
    ){    int i
    ,j
    ,sum
    =0
    ;    for(i
    =1
    ;i
    <=k
    ;i
    ++)    {        for(j
    =0
    ;j
    <4
    ;j
    ++)        {            int xx
    =x
    +dir
    [j
    ][0
    ]*i
    ;            int yy
    =y
    +dir
    [j
    ][1
    ]*i
    ;            if(xx
    <0
    ||xx
    >=n
    ||yy
    <0
    ||yy
    >=n
    ) continue;            if(cost
    [xx
    ][yy
    ]<=cost
    [x
    ][y
    ]) continue;            if(mark
    [xx
    ][yy
    ])            {                sum
    =max
    (sum
    ,dp
    [xx
    ][yy
    ]);            }            else             {                mark
    [xx
    ][yy
    ]=1
    ;                sum
    =max
    (sum
    ,dfs
    (xx
    ,yy
    ));            }        }    }    dp
    [x
    ][y
    ]=cost
    [x
    ][y
    ]+sum
    ;    return dp
    [x
    ][y
    ];    }int main(){    int i
    ,j
    ;    while(~scanf
    ("%d%d"
    ,&n
    ,&k
    ))    {        if(n
    <0
    ) break;        for(i
    =0
    ;i
    <n
    ;i
    ++)        for(j
    =0
    ;j
    <n
    ;j
    ++)        {            scanf
    ("%d"
    ,&cost
    [i
    ][j
    ]);        }        memset
    (dp
    ,0
    ,sizeof(dp
    ));        memset
    (mark
    ,0
    ,sizeof(mark
    ));        mark
    [0
    ][0
    ]=1
    ;        printf
    ("%d\n"
    ,dfs
    (0
    ,0
    ));    }    return 0
    ;}
   
  
 


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.