zoukankan      html  css  js  c++  java
  • zoj 1107 FatMouse and Cheese(记忆化搜索)

    题目链接:点击链接

    题目大意:老鼠从(0,0)出发,每次在同一个方向上最多前进k步,且每次到达的位置上的数字都要比上一个位置上的数字大,求老鼠经过的位置上的数字的和的最大值

    #include<stdio.h>
    #include<string.h>
    #define max(a,b) a>b?a:b
    int n;
    int k;//前进的步数
    int map[105][105];
    int ans[105][105];//记忆化搜索,保存中间搜索结果
    int search(int x,int y)
    {
        int dx,dy;//要去的下一个位置
        int i,maxx = 0;
        if(ans[x][y] != -1) return ans[x][y];//已经搜索过,直接返回结果
        for(i = 1 ; i <= k ; i ++)//向前走的步数
        {
            dx = x - i;//向上
            if(dx >= 0 && dx < n && map[dx][y] > map[x][y])
            {
                ans[dx][y] = search(dx,y);
                maxx = max(maxx,ans[dx][y]);
            }
            dx = x + i;//向下
            if(dx >= 0 && dx < n && map[dx][y] > map[x][y])
            {
                ans[dx][y] = search(dx,y);
                maxx = max(maxx,ans[dx][y]);
            }
            dy = y - i;//向左
            if(dy >= 0 && dy < n && map[x][dy] > map[x][y])
            {
                ans[x][dy] = search(x,dy);
                maxx = max(maxx,ans[x][dy]);
            }
            dy = y + i;//向右
            if(dy >= 0 && dy < n && map[x][dy] > map[x][y])
            {
                ans[x][dy] = search(x,dy);
                maxx = max(maxx,ans[x][dy]);
            }
        }
        return maxx + map[x][y];
    }
    int main()
    {
        int i,j;
        while(scanf("%d%d",&n,&k) && (n != -1 && k != -1))
        {
            for(i = 0 ; i < n ; i ++)
             for(j = 0 ; j < n ; j ++)
              scanf("%d",&map[i][j]);
            memset(ans,-1,sizeof(ans));
            printf("%d
    ",search(0,0));
        }
        return 0;
    }


  • 相关阅读:
    [SCOI2009] Windy数
    [P1361] 小M的作物
    Wannafly Camp 2020 Day 2E 阔力梯的树
    2017百越杯反序列化writeup
    大美西安writeup
    Thinkphp的SQL查询方式
    Thinkphp的CURD
    记一次拿webshell踩过的坑(如何用PHP编写一个不包含数字和字母的后门)
    ThinkPHP的输出和模型使用
    ThinkPHP的运行流程-2
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3194083.html
Copyright © 2011-2022 走看看