HDU 1937 -——Finding Seats(尺取+二维前缀和)

参考博客

A group of K friends is going to see a movie. However, they are too late to get good tickets, so they are looking for a good way to sit all nearby. Since they are all science students, they decided to come up with an optimization problem instead of going on with informal arguments to decide which tickets to buy. 

The movie theater has R rows of C seats each, and they can see a map with the currently available seats marked. They decided that seating close to each other is all that matters, even if that means seating in the front row where the screen is so big it’s impossible to see it all at once. In order to have a formal criteria, they thought they would buy seats in order to minimize the extension of their group. 

The extension is defined as the area of the smallest rectangle with sides parallel to the seats that contains all bought seats. The area of a rectangle is the number of seats contained in it. 

They’ve taken out a laptop and pointed at you to help them find those desired seats. 

Input

Each test case will consist on several lines. The first line will contain three positive integers R, C and K as explained above (1 <= R,C <= 300, 1 <= K <= R × C). The next R lines will contain exactly C characters each. The j-th character of the i-th line will be ‘X’ if the j-th seat on the i-th row is taken or ‘.’ if it is available. There will always be at least K available seats in total. 
Input is terminated with R = C = K = 0. 

Output

For each test case, output a single line containing the minimum extension the group can have.

Sample Input

3 5 5
...XX
.X.XX
XX...
5 6 6
..X.X.
.XXX..
.XX.X.
.XXX.X
.XX.XX
0 0 0

Sample Output

6
9

题意: 给你一个n,m,k然后给你一个n*m的矩阵,其中’.’代表空座,’X’代表已占,让你求最小的矩阵面积使得矩阵中空座的个数大于等于k个

这里的大体思路就是,先枚举两个行 i , j  在尺取两个列 l , r ,然后就是看点 (i,l) 和 点(j,r) 之间 '.' 的个数

#include<iostream>
#include<cstring>
using namespace std;
int n,m,k;
int pre[1005][1005];
int get(int x1,int y1,int x2,int y2){
	return pre[x2][y2]-pre[x2][y1-1]-pre[x1-1][y2]+pre[x1-1][y1-1];//计算二维前缀和的值 
}
int main(){
	while(cin>>n>>m>>k&&(n+m+k)){
		char cp;
		memset(pre,0,sizeof(pre));
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>cp;
				pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+((cp=='.')?1:0);//二维前缀和 
			}
		}
		int ans=999999;
		for(int i=1;i<=n;i++){
			for(int j=i;j<=n;j++){//枚举 行 
				int l=1;
				int r=1;//尺取 列 
				while(true){
					while(get(i,l,j,r)<k&&r<m)//!!! 注意 r 的边界 
					    r++;//!!!!!
					if(get(i,l,j,r)<k)//根本不可能有矩形 大于 k 
					   break;
					if(get(i,l,j,r)>=k)//满足条件 
					    ans=min(ans,(r-l+1)*(j-i+1));
					l++; //!!!!!
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值