您的位置:首页 > 其它

爆搜 + 模拟 --- codeforces 475C

2017-03-05 17:13 316 查看
 

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

;
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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: