HDU 1078 FatMouse and Cheese

记忆化搜索,第一次做搜索,好好学习下!

dir保存了搜索的四个方向,下右上左

这里还懵懵懂懂的,现将模板记下来。=_=!!

 

 1 //#define LOCAL

 2 #include <iostream>

 3 #include <cstdio>

 4 #include <cstring>

 5 using namespace std;

 6 

 7 const int maxn = 105;

 8 int map[maxn][maxn], dp[maxn][maxn];

 9 int dir[] = {1,0, 0,1, -1,0, 0,-1};

10 int n, k;

11 

12 int dfs(int x, int y)

13 {

14     if(dp[x][y] != -1)

15         return dp[x][y];

16     int xx, yy, mmax = 0;

17     for(int t = 1; t <= k; ++t)

18     {

19         for(int i = 0; i < 8;)

20         {

21             xx = x + t*dir[i++];

22             yy = y + t*dir[i++];

23             if(xx>=1 && yy>=1 && xx<=n && yy<=n 

24                 && map[xx][yy] > map[x][y])

25                 mmax = max(mmax, dfs(xx, yy));

26         }

27     }

28     return dp[x][y] = map[x][y] + mmax;

29 }

30 

31 int main(void)

32 {

33     #ifdef LOCAL

34         freopen("1078in.txt", "r", stdin);

35     #endif

36     

37     while(scanf("%d%d", &n, &k) && (n+k+2))

38     {

39         memset(dp, -1, sizeof(dp));

40         for(int i = 1; i <= n; ++i)

41             for(int j = 1; j <= n; ++j)

42                 scanf("%d", &map[i][j]);

43         printf("%d\n", dfs(1, 1));

44     }

45     return 0;

46 }
代码君

 

你可能感兴趣的:(HDU)