主题链接:点击打开链接

意甲冠军:特定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&#39;s Painting 模拟的更多相关文章

  1. codeforces 373 A - Efim and Strange Grade(算数模拟)

    codeforces 373 A - Efim and Strange Grade(算数模拟) 原题:Efim and Strange Grade 题意:给出一个n位的实型数,你可以选择t次在任意位进 ...

  2. Codeforces Round #353 (Div. 2) B. Restoring Painting 水题

    B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...

  3. codeforces 459D - Pashmak and Parmida&#39;s problem【离散化+处理+逆序对】

    题目:codeforces 459D - Pashmak and Parmida's problem 题意:给出n个数ai.然后定义f(l, r, x) 为ak = x,且l<=k<=r, ...

  4. Codeforce 475 C. Kamal-ol-molk&#39;s Painting

    从最左上的点開始枚举长宽.... C. Kamal-ol-molk's Painting time limit per test 2 seconds memory limit per test 256 ...

  5. Codeforces Round #461 (Div. 2) C. Cave Painting

    C. Cave Painting time limit per test 1 second memory limit per test 256 megabytes Problem Descriptio ...

  6. Codeforces Round #599 (Div. 1) A. Tile Painting 数论

    C. Tile Painting Ujan has been lazy lately, but now has decided to bring his yard to good shape. Fir ...

  7. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  8. 爆搜 + 模拟 --- codeforces 475C

    Problem's Link:http://codeforces.com/problemset/problem/475/Chttp://codeforces.com/problemset/proble ...

  9. Codeforces Round #313 C. Gerald&#39;s Hexagon(放三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. USACO2016 January Gold Angry Cows

    Angry Cows 题目描述:给出数轴上的\(n\)个整点\((a[i])\),现在要在数轴上选一个位置\(x\)(可以是实数),以及一个半径\(R\),在以\(x\)为中心,半径为\(R\)的范围 ...

  2. hibernate连接时指定编码方式 hibernate中文乱码问题

    <property name="connection.url">jdbc:mysql://localhost:3306/cms?useUnicode=true& ...

  3. error C2065: 'assert' : undeclared identifier

    F:\VC6.0 : error C2065: 'assert' : undeclared identifier 导入#include <assert.h>

  4. c++崩溃错误2

    使用了内联函数: 在头文件中声明和定义内联函数是正确的 但是在头文件中声明内联函数,而在.cpp文件中定义了内联函数会导致崩溃的 .h class stu{ inline void str(): } ...

  5. 天气情况(思维,dp思想)

    天气情况 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. C++ 顶层 const

    我的主力博客:半亩方塘 本文的主要參考来源来自于:C++ Primer 中文版(第 5 版) 第 57 面至第 58 面 1. 顶层 const 与底层 const 概念 我们知道,指针本身是一个对象 ...

  7. HDU 5781 ATM Mechine

    题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将 ...

  8. android系统将普通应用升级为系统应用

    作为一名程序员,有的时候并不是使用软件,而是去改造软件,不仅仅只是会编程而已,还要满足客户的需求.这样,才能开发出符合客户需求的应用,在关于到涉及到android底层的应用的时候,手机就需要root了 ...

  9. dom操作例子

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. PhoneGap Xcode iOS教程

    http://mobile.51cto.com/web-334924.htmhttp://phonegap.com/install/http://www.phonegap100.com/jiaoche ...