您的位置:首页 > 大数据 > 人工智能

CodeForces #Bayan# -- C Kamal-ol-molk's Painting 模拟

2014-10-07 16:55 295 查看
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 <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 1005

using namespace std;
struct P{
int x;
int y;
};
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0}; //�£��ң��ϣ���
bool visit[maxn][maxn];
bool grid[maxn][maxn];
int n,m;
vector<P> l,r;

bool bfs(int sx,int sy)
{
int countt=0;
P sp;
sp.x=sx,sp.y=sy;
queue<P> q;
q.push(sp);
while(!q.empty()){
P p;
p=q.front();
q.pop();
for(int i=0;i<4;i+=1){
int x=p.x+dx[i],y=p.y+dy[i];
if(x>=0&&x<n&&y>=0&&y<m&&grid[x][y]&&!visit[x][y]){
if(((y-1>=0&&!grid[x][y-1])||y-1<0)&&((x+1<n&&!grid[x+1][y])||x+1>=n)&&
(y+1<n&&grid[x][y+1])&&(x-1>=0&&grid[x-1][y])){
P tmpp;
tmpp.x=x,tmpp.y=y;
l.push_back(tmpp);
//cout<<"push l "<<x<<" "<<y<<" "<<endl;
}
if((x+1<n&&grid[x+1][y])&&(y+1<n&&grid[x][y+1])&&
(x-1>=0&&grid[x-1][y])&&(y-1>=0&&grid[x][y-1])&&(y+1<n&&x-1>=0&&!grid[x-1][y+1])){
//�������Ҷ���#��������.
P tmpp;
tmpp.x=x,tmpp.y=y;
r.push_back(tmpp);
//cout<<"push r"<<x<<" "<<y<<" "<<endl;
}
visit[x][y]=true;
P tmpp;
tmpp.x=x,tmpp.y=y;
q.push(tmpp);
if(((x+1<n&&!grid[x+1][y])||x+1>=n)&&((y+1<n&&!grid[x][y+1])||y+1>=n)){
//������¶������ߵĵ���������Σ���ô���ͼ�α���Ҫ�
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: