Codeforces 475C Kamal-ol-molk's Painting 模拟

题目链接:点击打开链接

题意:给定n*m的矩阵

X代表有色 .代表无色

用一个x*y的矩阵形刷子去涂色。

刷子每次可以→或↓移动任意步。

若能够染出给定的矩阵,则输出最小的刷子的面积

若不能输出-1

思路:

先找到连续最小的x,y

因为至少一个边界和x或y相等,所以枚举(x,i) 和 (i,y)就可以了。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define N 1010
int top;
char s[N][N];
int n, m, stx, sty;
int H(int h, int l, int r){//扫一行 -1表示全是. 1表示全是X 0表示都有
    if(h>n || r>m) return -2;
    int cnt = 0;
    for(int i = l; i <= r; i++)
        if(s[h][i]=='X')cnt++;
    if(cnt==0)return -1;
    if(cnt==r-l+1)return 1;
    return 0;
}
int L(int l, int S, int X){
    if(l>m || X>n) return -2;
    int cnt = 0;
    for(int i = S; i <= X; i++)
        if(s[i][l]=='X')cnt++;
    if(cnt==0)return -1;
    if(cnt==X-S+1)return 1;
    return 0;
}
bool ok(int x, int y){
  //  printf(" ++++++(%d,%d)\n", x,y);
    int nowx = stx, nowy = sty;
    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(s[i+nowx][j+nowy]!='X')return false;
 //   puts("---");put(); puts("-----");
    int cnt = x*y;
    while(1){
        if(nowx + x <= n && s[nowx+x][nowy] =='X')
        {
            nowx ++;
            for(int i = 0; i < y; i++)
                if(s[nowx+x-1][i+nowy]!='X')
                return false;
            cnt += y;
        }
        else if(nowy + y <= m && s[nowx][nowy+y] == 'X')
        {
            nowy++;
            for(int i = 0; i < x; i++)
                if(s[i+nowx][nowy+y-1]!='X')
                return false;
            cnt += x;
        }
        else break;
    //    puts("******");put();
    }
    return cnt == top;
}
int hehe; int feifei;
int x, y;
void find_xy(){
    x = N, y = N;
    for(int i = 1; i <= m; i++) {
        int cnt = 0;
        for(int j = 1; j <= n; j++)
        {
            if(s[j][i]=='.')
            {
                if(cnt) x = min(x, cnt);
                cnt = 0;
            }
            else
                cnt++;
        }
        if(cnt) x = min(x, cnt);
    }
    for(int i = 1; i <= n; i++) {
        int cnt = 0;
        for(int j = 1; j <= m; j++)
        {
            if(s[i][j]=='.')
            {
                if(cnt) y = min(y, cnt);
                cnt = 0;
            }
            else cnt++;
        }
        if(cnt) y = min(y, cnt);
    }
}
int solve(){
    int ans = N*N;
    for(int i = 1; i <= x; i++)
        if(ok(i,y)) {
            ans = i*y;
            break;
        }
    for(int i = 1; i <= y && x*i<ans; i++)
        if(ok(x,i))
    {
        ans = x*i;
        break;
    }
    if(ans > n*m) return -1;
    return ans;
}
void input(){
    stx = -1;
    top = 0;
    for(int i = 1; i <= n; i++)
    {
        scanf("%s", s[i]+1);
        for(int j = 1; j <= m; j++) {
            if(stx==-1 && s[i][j]=='X'){
              stx = i, sty = j;
            }
            top += s[i][j]=='X';
        }
    }
}
int main(){
     while(cin>>n>>m){
        input();
        find_xy();
        printf("%d\n", solve());
     }
     return 0;
}

Codeforces 475C Kamal-ol-molk's Painting 模拟

时间: 2024-11-04 23:01:59

Codeforces 475C Kamal-ol-molk's Painting 模拟的相关文章

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

主题链接:点击打开链接 意甲冠军:特定n*m矩阵 X代表色 .代表无色 随着x*y形刷子去涂色. 刷子每次能够→或↓移动随意步. 若可以染出给定的矩阵,则输出最小的刷子的面积 若不能输出-1 思路: 先找到连续最小的x,y 由于至少一个边界和x或y相等,所以枚举(x,i) 和 (i,y)就能够了. #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <ios

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces 848B Rooter&#39;s Song(分类+模拟)

题目链接 Rooter's Song 题意  有n个舞者站在x轴上或y轴上,每个人有不同的出发时间.x轴上的舞者垂直x轴正方向移动,y轴上的舞者垂直y轴正方向移动. 当x轴的舞者和y轴的舞者相遇时,他们会互换运动轨迹.求每个舞者的最后位置. 把所有会发生碰撞的舞者塞到一起,按照坐标大小升序排序. 对于某个在x轴上的舞者, 计算他右边的舞者个数,和他上面的舞者个数(即y轴会和他碰撞的所有舞者个数) 对于某个在y轴上的舞者, 计算他上面的舞者个数,和他右边的舞者个数(即x轴会和他碰撞的所有舞者个数)

递归解Codeforces Round #256 (Div. 2)C. Painting Fence

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

Codeforces Round #256 (Div. 2) C. Painting Fence

C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no

codeforces 493C Vasya and Basketball (枚举+模拟+思维)

C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 point

Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

题目链接:http://codeforces.com/problemset/problem/448/C ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces 758C:Unfair Poll(思维+模拟)

http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点,当点完第n列的名之后,接着点第n-1列的名.以此类推,就是从列上来看的话:1,2,3,4,……,n,n-1,n-2,……,1 ,2,…….这样的顺序点名.老师上课总共点k次名,问该课堂最多可以点同一个同学多少次,最少可以点同一个同学多少次,点了位置为(x,y)的同学多少次名. 思路:一遇到这种题目

Codeforces 730A:Toda 2(multiset模拟)

http://codeforces.com/problemset/problem/730/A 题意:有n个人打天梯,想让这n个人的分数相同,每场比赛必须有2-5个人参赛,参赛的人会降低一分,问一个合理方案让所有人的分数相同. 思路:不限制比赛场数,那么只用考虑2-3个人参赛的情况(因为4和5可以由2和3组成).但是这个时候就不知道什么时候用3什么时候用2了.. 看别人代码.只有最大的三个数是一样的时候才用3,其他时候都用2. 然后很暴力地用multiset来模拟比赛. 1 #include <b