爆搜 + 模拟 --- codeforces 475C

 

Problem's Link:http://codeforces.com/problemset/problem/475/Chttp://codeforces.com/problemset/problem/475/C


 

Mean: 

给你一个网格,每个格子中是'X'或'.',你需要用一个刷子去将这些'X'刷掉,而且不能刷到'.',每次只能往下走或者往右走,让你选一把最小的刷子出来。

analyse:

 爆搜+模拟。

1)我们先遍历两遍,求出最小连续的x和y,那么x和y就是刷子最大能够达到的大小;

2)对1~x,1~y进行枚举判断,找到符合条件的最小刷子面积。

Time complexity:O(n^2)

 

Source code:

 

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-10-08-20.51
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 1010
#define LL long long
using namespace std;
char Map[N][N];
int X_num;
int x_sta,y_sta;
int x,y;
int n,m;
void Get_Min_xy()
{
    x=N,y=N;
    for(int i=1;i<=n;++i)//行
    {
        int cnt=0;
        for(int j=1;j<=m;++j)//列
        {
            if(Map[i][j]=='X')
            {
                cnt++;
            }
            else
            {
                if(cnt)
                    y=min(cnt,y);
                cnt=0;
            }
        }
        if(cnt)
        {
            y=min(cnt,y);
        }
    }
    for(int i=1;i<=m;++i)
    {
        int cnt=0;
        for(int j=1;j<=n;++j)
        {
            if(Map[j][i]=='X')
            {
                cnt++;
            }
            else
            {
                if(cnt)
                    x=min(cnt,x);
                cnt=0;
            }
        }
        if(cnt)
        {
            x=min(cnt,x);
        }
    }
}

bool judge(int x, int y)
{
    int nowx = x_sta, nowy = y_sta;
    if(nowx + x-1 > n || nowy + y-1>m)
        return false;
    for(int i = 0; i < x; i++)
        for(int j = 0; j < y; j++)
            if(Map[i+nowx][j+nowy]!='X')
                return false;
    int cnt = x*y;
    while(1)
    {
        if(nowx + x <= n && Map[nowx+x][nowy] =='X')
        {
            nowx ++;
            for(int i = 0; i < y; i++)
                if(Map[nowx+x-1][i+nowy]!='X')
                    return false;
            cnt += y;
        }
        else if(nowy + y <= m && Map[nowx][nowy+y] == 'X')
        {
            nowy++;
            for(int i = 0; i < x; i++)
                if(Map[i+nowx][nowy+y-1]!='X')
                    return false;
            cnt += x;
        }
        else break;
    }
    return cnt == X_num;
}

int work()
{
    if(x<=0||y<=0)
        return -1;
    int area=N*N;
    for(int i=1;i<=x;i++)
    {
        if(judge(i,y))
        {
            area=i*y;
//            cout<<"x="<<i<<endl;
//            cout<<"y="<<y<<endl;
            break;
        }
    }
    for(int j=1;j<=y&&x*j<area;j++)
    {
        if(judge(x,j))
        {
            area=x*j;
//            cout<<"x1="<<x<<endl;
//            cout<<"y1="<<j<<endl;
            break;
        }
    }
    if(area> n*m) return -1;
    return area;
}
int main()
{
    scanf("%d %d",&n,&m);
    getchar();
    X_num=0;
    bool flag=1;
    for(int i=1;i<=n;++i)
    {
        gets(Map[i]+1);
        for(int j=1;j<=m;++j)
        {
            X_num+=(Map[i][j]=='X');
            if(flag&&Map[i][j]=='X')
            {
                flag=0;
                x_sta=i;
                y_sta=j;
            }
        }
    }
    Get_Min_xy();
    printf("%d\n",work());
    return 0;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值