• [luoguP1736] 创意吃鱼法(DP)


    传送门

    f[i][j][0] 表示从右下角到左上角,以(i,j)为起点能延伸的最大值

    f[i][j][1] 表示从左下角到右上角,以(i,j)为起点能延伸的最大值

    up[i][j] 表示(i,j)上面有多少个0

    left[i][j] 表示(i,j)左边有多少个0

    right[i][j] 表示(i,j)右边有多少个0

    f[i][j][0] = min(f[i - 1][j - 1][0], up[i][j], left[i][j]) + 1

    f[i][j][1] = min(f[i - 1][j + 1][1], up[i][j], right[i][j]) + 1

    代码

    #include <cstdio>
    #include <iostream>
    #define N 2510
    #define max(x, y) ((x) > (y) ? (x) : (y))
    #define min(x, y) ((x) < (y) ? (x) : (y))
    
    int n, m, ans;
    unsigned short up[N][N], left[N][N], right[N][N], f[N][N][2];
    bool a[N][N];
    
    inline int read()
    {
        int x = 0, f = 1;
        char ch = getchar();
        for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
        for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
        return x * f;
    }
    
    int main()
    {
    	int i, j;
    	n = read();
    	m = read();
    	for(i = 1; i <= n; i++)
    		for(j = 1; j <= m; j++)
    		{
    			f[i][j][0] = f[i][j][1] = a[i][j] = read();
    			up[i][j] = a[i - 1][j] ? 0 : up[i - 1][j] + 1;
    			left[i][j] = a[i][j - 1] ? 0 : left[i][j - 1] + 1;
    		}
    	for(i = 1; i <= n; i++)
    		for(j = m; j >= 1; j--)
    			right[i][j] = a[i][j + 1] ? 0 : right[i][j + 1] + 1;
    	for(i = 1; i <= n; i++)
    		for(j = 1; j <= m; j++)
    		{
    			if(!a[i][j]) continue;
    			f[i][j][0] = min(f[i - 1][j - 1][0], min(up[i][j], left[i][j])) + 1;
    			f[i][j][1] = min(f[i - 1][j + 1][1], min(up[i][j], right[i][j])) + 1;
    			ans = max(ans, max(f[i][j][0], f[i][j][1]));
    		}
    	printf("%d
    ", ans);
    	return 0;
    }
    

      

  • 相关阅读:
    MySql优化-你的SQL命中索引了吗
    php根据两点经纬度算距离
    二维数组排序
    php 求对数
    socket,websocket,socketio之间的关系
    PHP获取两个时间戳间的所有日期
    通过动画理解Raft公式算法
    Fabric创建通道、组织加入通道
    查看进程线程的方法
    Fabric添加节点
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7061732.html
Copyright © 2020-2023  润新知