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
output
input
output
input
output
题目大意:
给一把刷子,和一个图形,问可以画出这个图形的最小的刷子面积是多少。
其中刷子每次只可以横动和竖动一个单位(实际上从图形上看就是每次可以动多个单位)。
思路&反省:
原本自己想了一种错误的思路,总的是一种分类讨论的思想,就是讨论#刷子可以画出的图像的充要条件#,但是发现会漏情况,
而且这些情况要改很难。
下面是原来错误的代码:
#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)){
//������¶������ߵĵ���������Σ���ô���ͼ�α���Ҫ�
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)){
//������¶������ߵĵ���������Σ���ô���ͼ�α���Ҫ�
相关文章推荐
- Codeforce 475 C. Kamal-ol-molk's Painting
- CF Bayan 2015 Contest Warm Up B. Kamal-ol-molk's Painting
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- Codeforces 475C Kamal-ol-molk's Painting 模拟
- Codeforces Yandex.Algorithm 2011 Qualification 2 / 82A Double Cola (数学规律&模拟)
- Educational Codeforces Round 2 D. Area of Two Circles' Intersection
- Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida's problem(求逆序数对)
- Educational Codeforces Round 2 D.Area of Two Circles' Intersection(计算几何)
- Codeforces Round #327 (Div. 2) (A. Wizards' Duel 简单数学)
- Codeforces Round 313- Gerald's Hexagon
- Codeforces Round 261 Div.2 D Pashmak and Parmida&#39;s problem --树状数组
- Educational Codeforces Round 2 D - Area of Two Circles' Intersection(两圆交面积)
- Codeforces ABBYY Cup 3.0 / 316A1 316A2 Special Task(模拟&贪心&组合数学)
- codeforces A. Bayan Bus(简单模拟)
- Codeforces Round #248 (Div. 2) C - Ryouko's Memory Note
- 【CODEFORCES】 C. Kamal-ol-molk's Painting
- Educational Codeforces Round 39 (Rated for Div. 2)(A-D)(水题 + 模拟 + 贪心模拟 + DP)
- Codeforces RoundC. Ryouko's Memory Note
- Educational Codeforces Round 2D. Area of Two Circles' Intersection(计算几何+圆相交的面积)
- Codeforces Round #323 B Robot's Task