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

题意:给你一个矩阵让你在这个矩阵里面找一个矩形使他的'.'的数量大于给的人的数量

思路:这个题测试的时候没做,是一个尺取,二维的,横向一个,纵向一个;

code:

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cmath>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int a[555][555];
char mp[555][555];
int main()
{
    int R,C,K;
    while(cin>>R>>C>>K)
    {
        if(C==0&& R==0 && K==0)
        {
            break;
        }
        for(int i=1;i<=R;i++)
        {
            scanf("%s",mp[i]+1);
        }
        for(int i=1;i<=R;i++)
        {   int x=0;
            for(int j=1;j<=C;j++)
            {
                    if(mp[i][j]== '.') x++;
                    a[i][j]=a[i-1][j]+x;
            }
        }
        int mininum=999999999;
        for(int i=1;i<=C;i++)
        {
            for(int j=i;j<=C;j++)
            {
                int p=1;
                for(int w=1;w<=R;w++)
                {
                    while(a[w][j]-a[w][i-1]-a[p-1][j]+a[p-1][i-1]>=K)
                    {
                        mininum=min(mininum,(j-i+1)*(w-p+1));
                        p++;
                    }
                }
            }
        }
        cout<<mininum<<endl;
        memset(a,0,sizeof(a));
        memset(mp,0,sizeof(mp));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40941611/article/details/81269927