题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2868

用暴力也过了:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<set>
#define LL long long
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b) using namespace std; int a[1050][1050];
int n,m;
int ans = 0; int test(int x, int y, int len)
{
    for(int i = x; i<x+len; i++)
    for(int j = y; j<y+len; j++)
    {
        if(a[i][j]==0) return 0;
    }
    return 1;
} int main()
{
    while(scanf("%d%d",&n,&m) && (m||n))
    {
        ans = 0;
        for(int i = 1; i<=n; i++)
        for(int j = 1; j<=m; j++)
            scanf("%d",&a[i][j]);         for(int i = 1; i<=n; i++)
        for(int j = 1; j<=m; j++)
        {
            if(a[i][j])
            for(int len = 1; j+len-1<=m && i+len-1<=n; len++)
            {
                if(len<=ans)//这一步很重要,如果长度都比ans小,那再去比较也无意义了。少了这一句就少时了。
                    continue;                 if(test(i,j,len))
                    ans = MAX(ans,len);                 else break;
            }
        }
        printf("%d\n",ans);     }
    return 0;
}

然后使用dp,画图找找规律。dp[i][j]为当前格子作为右下角能达到的最大正方形边长,可知dp[i][j] 只与dp[i-1][j] ,dp[i][j-1] ,dp[i-1][-1j]有关。所以可以得到状态转移方程:dp[i][j] = 1 + MMIN(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<string>
#include<set>
#define LL long long
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
#define MMIN(a,b,c) (MIN(a,b)<c?MIN(a,b):c) using namespace std; int a[1050][1050];
int dp[1050][1050]; int n,m;
int ans = 0; int main()
{
while(scanf("%d%d",&n,&m) && (m||n))
{
ans = 0;
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
{
dp[i][j] = 0;
scanf("%d",&a[i][j]);
if(a[i][j])
{
dp[i][j] = 1 + MMIN(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]);
} ans = MAX(dp[i][j],ans);
}
printf("%d\n",ans);
}
return 0;
}

UVALive - 4867 —— dp的更多相关文章

  1. UVALive 4867 Maximum Square 贪心

    E - Maximum Square Time Limit:4500MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  2. UVAlive 6131 dp+斜率优化

    这道题和06年论文<从一类单调性问题看算法的优化>第一道例题很相似. 题意:给出n个矿的重量和位置,这些矿石只能从上往下运送,现在要在这些地方建造m个heap,要使得,sigma距离*重量 ...

  3. UVALive 4764 dp

    DES: 这是一个新的游戏.给你一套牌.编号从1到100000.正常来说.你手中的牌和这次翻的牌是一样的,就会加一分.但是.如果是999的话.加三分.所以问你最大的分是多少. 貌似是简单的DP吧.(D ...

  4. UVALive - 6952 DP 分段/隔板

    题意:商品总价按四舍五入计算,n个物品最多可分\(d+1\)段,求最小代价 \(dp[i][j]\):\(j\)个物品分\(i\)段 注意一个技巧是只在需要分出新的段时才四舍五入(旧段结算),这样就避 ...

  5. UVALive 4731 dp+贪心

    这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i ...

  6. 大白第一章第四节dp例题

    入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...

  7. Todolist

    UValive 6041(KD tree) UValive 6042(DP) UValive 6044(图论)

  8. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  9. 状压DP uvalive 6560

    // 状压DP uvalive 6560 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值 // 思路: // dp[i][j]:第i行j状态下的值 ...

随机推荐

  1. REBXOR

    题面 Description 给定一个含N个元素的数组A,下标从1开始.请找出下面式子的最大值. (A[l1]xorA[l2+1]xor-xorA[r1])+(A[l2]xorA[l2+1]xor-x ...

  2. Could not change executable permissions on the application

    I could solve it erasing an application that I had previously uploaded using the same Bundle Identif ...

  3. install nfs and share file

    介绍一下NFS的安装,以及共享文件 NFS(Net File System),通过使用NFS,可以像使用本地文件一样访问远程文件. 它主要解决了数据共享的问题,可以备份容灾. 安装配置 1.以linu ...

  4. mysql查询处理顺序

    http://zhangzhaoaaa.iteye.com/blog/1689412参考:<MYSQL技术内幕SQL编程> select distinct <selectlist&g ...

  5. 3D打印技术之切片引擎(4)

    [此系列文章基于熔融沉积( fused depostion modeling, FDM )成形工艺] 这一篇文章我讲一下多边打印的问题,多边打印是切片引擎的一项关键的技术. 图1 双边打印 首先.它能 ...

  6. 【每日Scrum】第三天(4.24) TD学生助手Sprint2站立会议

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 今天主要看了多事件处理的内容然后改了下界面, 和小楠重写架构,使代码更加简洁,并增加了几个界面 架构太难,数据库字段总出问题 Y 刘静 添加事 ...

  7. Allegro封装的制作

    过孔封装的层次分析: 1.阻焊层Solder Mask:又称绿油层,是PCB上的非布线层,用于制成丝网漏印板,将不需要的焊接的地方涂上阻焊剂.由于焊接PCB时焊锡在高温下的流动性,所以必须在不需要焊接 ...

  8. C语言--函数篇

      1-1.函数简单调用 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 int ...

  9. openshifit 安装 redis

    http://blog.csdn.net/lsx991947534/article/details/48860537 http://blog.csdn.net/aguangg_6655_la/arti ...

  10. ScrollView滑动的监听

    ScrollView滑动的监听 有时候我们须要监听ScrollView的滑动事件.来完毕业务需求. 第一种: 能够直接实现OnTouchListener接口.在这里面写你所须要的操作 scrollVi ...