CDUTCM OJ——1293Building

本文介绍了一个动态规划问题,目标是在一块包含障碍物的土地上找到能够建造的最大面积的正方形房屋。通过动态规划算法,文章提供了详细的解决方案和代码实现。

CDUTCM OJ 1293Building
1293: Building
时间限制: 1 Sec 内存限制: 128 MB
提交: 6 解决: 4

题目描述
CTJ team were going to build a house which needed a square land. Now they are inspecting a large area of land, but there are pits or large rocks on the ground, which could not be covered by house. So you are giving the terrain. Can you help them find the largest area of the house?
输入
The first line has two integers n, m (1<=n, m<=1000),for the entire land surface. Followed by n lines, each line of m numbers separated by spaces. 0 means that the block of land has pits or rocks, and 1 means that the block of land is intact.
输出
An integer, the length of the largest square.
样例输入
4 4
0 1 1 1
1 1 1 0
0 1 1 0
1 1 0 1
样例输出
2
PS:额,真的忍不住骂脏话,这sb题 ,就一很简单的动态规划,但乍一看还以为是搜索,但一看数据范围1000*1000,就想到了动态规划,但到最后也没想出转移方程,哎,还是太菜。直接看代码吧。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1005][1005],b[1005][1005];
int n,m;
int main(){
	int ans,i,j;
	while(cin>>n>>m){
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		ans=0;
		for(i=1;i<=n;i++){
			for(j=1;j<=m;j++){
				scanf("%d",&a[i][j]);
				if(a[i][j]==0){
					b[i][j]=0;
				}
				else if(a[i][j]==1){
					if(b[i-1][j-1]==0){
						b[i][j]=1;
					}
					else{
						b[i][j]=min(b[i-1][j],b[i][j-1])+1;
					}
				}
				ans=max(b[i][j],ans);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
} 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值