从最左上的点開始枚举长宽....

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=1100;
const int INF=0x3f3f3f3f; char mp[maxn][maxn];
int n,m,sum[maxn][maxn];
int ans=INF; int area(int x1,int y1,int x2,int y2)
{
return sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1];
} int dfs(int x,int y,int wx,int wy)
{
if(area(x,y+1,x+wx-1,y+wy)==wx*wy) return wx+dfs(x,y+1,wx,wy);
if(area(x+1,y,x+wx,y+wy-1)==wx*wy) return wy+dfs(x+1,y,wx,wy);
return wx*wy;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
bool flag=false;
int px=-1,py=-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int t=0;
if(mp[i][j]=='X')
{
if(flag==false)
{
flag=true; px=i; py=j;
}
t=1;
}
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+t;
}
} int temp=py;
for(;temp<=m;temp++) if(mp[px][temp]!='X') break;
int leny=temp-py;
int lenx;
for(int i=px;i<=n;i++)
{
if(mp[i][py]!='X') break;
lenx=i-px+1;
if(dfs(px,py,lenx,leny)==sum[n][m])
{
ans=min(ans,lenx*leny);
}
}
temp=px;
for(;temp<=n;temp++) if(mp[temp][py]!='X') break;
lenx=temp-px;
for(int i=py;i<=m;i++)
{
if(mp[px][i]!='X') break;
leny=i-py+1;
if(dfs(px,py,lenx,leny)==sum[n][m])
{
ans=min(ans,lenx*leny);
}
} if(ans==INF) ans=-1;
printf("%d\n",ans);
return 0;
}

Codeforce 475 C. Kamal-ol-molk&#39;s Painting的更多相关文章

  1. Codeforces 475C Kamal-ol-molk&#39;s Painting 模拟

    主题链接:点击打开链接 意甲冠军:特定n*m矩阵 X代表色 .代表无色 随着x*y形刷子去涂色. 刷子每次能够→或↓移动随意步. 若可以染出给定的矩阵,则输出最小的刷子的面积 若不能输出-1 思路: ...

  2. HTML学习(二)进阶篇

    在博客园中有许多大神对HTML超文本标记语言写了很多内容,总结了很多知识,这里对我看到的博客文章, 所学到的知识,做一个总结.  一)列表和表格 dl→definition list(定义列表),见备 ...

  3. 一些unity资源

    雨凇解包 http://www.xuanyusong.com/archives/3618 http://www.cnblogs.com/lixiang-share/p/5840444.html u3d ...

  4. 【Nutch2.2.1基础教程之6】Nutch2.2.1抓取流程 分类: H3_NUTCH 2014-08-15 21:39 2530人阅读 评论(1) 收藏

    一.抓取流程概述 1.nutch抓取流程 当使用crawl命令进行抓取任务时,其基本流程步骤如下: (1)InjectorJob 开始第一个迭代 (2)GeneratorJob (3)FetcherJ ...

  5. 【转】39个让你受益的HTML5教程

    闲话少说,本文作者为大家收集了网上学习HTML5的资源,期望它们可以帮助大家更好地学习HTML5. 好人啊! 不过,作者原来说的40个只有39个,因为第5个和第8个是重复的. 原文在此! 1. 五分钟 ...

  6. C#开发微信门户及应用(39)--使用微信JSSDK实现签到的功能

    随着微信开逐步开放更多JSSDK的接口,我们可以利用自定义网页的方式来调用更多微信的接口,实现我们更加丰富的界面功能和效果,例如我们可以在页面中调用各种手机的硬件来获取信息,如摄像头拍照,GPS信息. ...

  7. CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果

    CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...

  8. 抱歉!15:44-16:39阿里云RDS故障造成全站不能正常访问

    非常非常抱歉!2016年3月7日15:44-16:39,由于阿里云RDS(云数据库)故障,造成全站不能正常访问,给您带来了很大很大的麻烦,恳请您的谅解! 故障是在15:44开始出现的,应用日志中出现大 ...

  9. grep-2.26 sed-4.2.2 awk-4.1.4 wget-1.18 pcregrep-8.39 pcre2grep-10.22 for windows 最新版本静态编译

    -------------------------------------------------------------------------------------------- grep (G ...

随机推荐

  1. gpg: symbol lookup error

    今天使用sudo apt-get 安装包的时候,出现gpg错误,如下: gpg: symbol lookup error: /usr/local/lib/libreadline.so.6: undef ...

  2. iOS9新特性

    本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...

  3. [JNA系列]Java调用Delphi编写的Dll之Delphi与JAVA基本数据类型对比

    Delphi与JAVA基本数据类型对比 类型 Delphi关键字 JAVA关键字 字节 备注 范围 整型 Shortint byte 1 有符号8位 -128..127 Byte 1 无符号8位 0 ...

  4. 浅谈配置文件:spring-servlet.xml(spring-mvc.xml) 与 applicationContext.xml

    在搭建 spring mvc 的框架时,会有2个配置文件必不可少: spring-servlet.xml 和applicationContext.xml.第一次接触spring mvc的工程师可能会对 ...

  5. github 修改项目默认语言

    我们在提交到github上的项目有时候被识别成了其它的语言,非我们使用的语言,这个时候可以采取以下措施来强制将语言改成我们需要的语言 在项目中创建一个文件 .gitattributes 打开.gita ...

  6. linux学习笔记34--命令rcp和scp

    rcp代表“remote file copy”(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝到另一个目录中. 1 ...

  7. maven将依赖打入jar包并制定main方法

    <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId& ...

  8. hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙

    /** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...

  9. 用e2fsck修复受损的linux文件系统

    今天想尝试直接从linux deepin拷贝一些文件到windows 10而不重启电脑,所以就安装Ext2Mgr了并加载了linux的几个磁盘: / /home 再次重启系统想进入linux的时发现系 ...

  10. C++ 函数模板一(函数模板定义)

    //函数模板定义--数据类型做参数 #include<iostream> using namespace std; /* 函数模板声明 1.函数模板定义由模板说明和函数定义组成,并且一个模 ...