Bayan 2015 Contest Warm Up C题(DP+模拟)

C. Kamal-ol-molk's Painting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Rumors say that one of Kamal-ol-molk's paintings has been altered. A rectangular brush has been moved right and down on the painting.

Consider the painting as a n × m rectangular grid. At the beginning an x × y rectangular brush is placed somewhere in the frame, with edges parallel to the frame, (1 ≤ x ≤ n, 1 ≤ y ≤ m). Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment.

You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush.

Input

The first line of input contains two integers n and m, (1 ≤ n, m ≤ 1000), denoting the height and width of the painting.

The next n lines contain the painting. Each line has m characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting.

Output

Print the minimum area of the brush in a line, if the painting is possibly altered, otherwise print  - 1.

Sample test(s)
input
4 4
XX..
XX..
XXXX
XXXX
output
4
input
4 4
....
.XXX
.XXX
....
output
2
input
4 5
XXXX.
XXXX.
.XX..
.XX..
output
-1

题意:给出一个n*m的图,X是可以被涂色的点,问你能不能找出一个面积最小的刷子给所有X点刷上颜色,刷子每次只能往下或右移动一格

思路:首先用sum[i][j]表示从i到n,从j到m的X的格子个数,这个直接DP求就好了

            然后就是判断的部分,枚举刷子的面积就好了

            对于每个枚举的刷子面积,从图的最左最上的点开始图(一定是这样,要知道任何时刻刷子左上方的点是一定涂不到的)

            然后判断当前能不能刷满,如果能够,就判断刷子下一步是往下还是往右移动(如果右边方向的最上一排和下边方向的最左边一排都同时有格子一定不可以)

            细节看代码吧~

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
        using namespace std; const int MAXN = 1010; char mp[MAXN][MAXN]; int sum[MAXN][MAXN]; int n,m; int getsum(int x1,int y1,int x2,int y2) { return sum[x1][y1]-sum[x2+1][y1]-sum[x1][y2+1]+sum[x2+1][y2+1]; } int ok(int x,int y,int a,int b) { while(1){ if(x+a-1>n || y+b-1>m)return 0; if(getsum(x,y,x+a-1,y+b-1)!=a*b)return 0; if(getsum(x+a,y,n,y)>0 && getsum(x,y+b,x,m)>0)return 0; if(getsum(x+a,y,n,m)==0 && getsum(x,y+b,n,m)==0)return 1; if(getsum(x+a,y,n,y)>0)++x; else ++y; } return 1; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%s",mp[i]+1); } int mx=n+1,my=m+1; for(int i=n;i>=1;i--){ for(int j=m;j>=1;j--){ sum[i][j]=sum[i+1][j]+sum[i][j+1]-sum[i+1][j+1]+(mp[i][j]=='X'); if(mp[i][j]=='X'){ if(mx>i)mx=i; if(my>j)my=j; } } } int ans=m*n+1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(i*j>=ans)continue; if(ok(mx,my,i,j))ans=i*j; } } if(ans==m*n+1)printf("-1\n"); else printf("%d\n",ans); return 0; } 
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值