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-10-26 06:55:15

Codeforces 475C Kamal-ol-molk&#39;s Painting 模拟的相关文章

Codeforces 475C Kamal-ol-molk&#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 <

Codeforces 216D Spider&#39;s Web 树状数组+模拟

题目链接:http://codeforces.com/problemset/problem/216/D 题意: 对于一个梯形区域,如果梯形左边的点数!=梯形右边的点数,那么这个梯形为红色,否则为绿色, 问: 给定的蜘蛛网中有多少个红色. 2个树状数组维护2个线段.然后暴力模拟一下,因为点数很多但需要用到的线段树只有3条,所以类似滚动数组的思想优化内存. #include<stdio.h> #include<iostream> #include<string.h> #in

Codeforces 439C Devu and Partitioning of the Array(模拟)

题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,但是只需要关注该份的和为奇数还是偶数,要求偶数堆的个数为p.输出方案. 解题思路:首先先将数组按照奇偶排序,也可以分开储存.然后先单独分k-p个奇数,然后后面的就将两个奇数当一个偶数分配,分配过程中计算是否满足,比如说奇数是否成对,以及是否分成了k堆. #include <cstdio> #include <cstring>

Codeforces Round #353 (Div. 2) B. Restoring Painting __ map or set 、思维题

B. Restoring Painting time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on

Codeforces Round #599 (Div. 2) C. Tile Painting

Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of nn consecutive tiles, numbered from 11 to nn. Ujan will paint each tile in some color

Codeforces 461C Appleman and a Sheet of Paper(模拟)

题目链接:Codeforces 461C Appleman and a Sheet of Paper 题目大意:就是一个叠被子的过程,穿插着询问一段区间上被子的单位厚度. 解题思路:用前缀和数组模拟即可.因为对于折超过一半的处理为将令一半叠上来,所以需要变量记录当前被子的正反状态.处理好下标关系即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

Codeforces Round #601 (Div. 2)D(蛇形模拟)

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 vector<char>ans; 5 char a[107][107]; 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(NULL); 9 cout.tie(NULL); 10 for(int i='0';i<='9';++i) 11 ans.empla

Codeforces 464C Substitutes in Number 同余定理+模拟

题目链接:点击打开链接 题意: 给定一串数字 下面有n个操作 每行格式形如 d->t d为一位数字,t为任意长度的数字. t的长度和不超过100000 问:最后的结果%1e9+7 思路: 首先我们可以得到一个结论: 同余定理使用后不能再修改数字. 那么为了让同余定理能够使用,我们倒序处理每个数字,这样就能保证能够使用同余定理. 记录每个数字实际代表的数字和实际对应的位数. 然后倒序处理上来即可. #include <stdio.h> #include <string.h> #

POJ 3087 Shuffle&amp;#39;m Up(模拟)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7404   Accepted: 3421 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of