CodeForces 370D Broken Monitor

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 2005
char ch[N][N];
int judge(int top, int bottom, int left, int right)
{  
    int flag = 1; 
    for(int i = top+1; i<=bottom-1; i++) 
    { 
        for(int j=left+1; j<=right-1; j++) 
        { 
            if(ch[i][j]=='w') {flag = 0; break;} 
        } 
    } 
    return flag;
} 
int main() 
{ 
    int n,m; 
    int top = 2001, bottom = -1, left = 2001, right = -1, tempi = 0, tempj = 0; 
    scanf("%d%d",&n,&m); 
    getchar(); 
    for(int i=1; i<=n; i++)
    { 
        for(int j=1; j<=m; j++) 
        { 
            ch[i][j] = getchar(); 
            if(ch[i][j]=='w') 
            { 
                top = top > i ? i : top;
                bottom = bottom < i ? i : bottom; 
                left = left > j ? j : left; 
                right = right < j ? j : right;
            } 
        } 
        getchar();
    } 
    int flag = 0, flag1 = 1;
    int temph = right - left + 1, tempv = bottom - top + 1;
    if(temph == tempv) 
    { 
        for(int i=top+1; i<=bottom-1; i++) 
            for(int j=left+1; j<=right-1; j++) 
            { 
                if(ch[i][j]=='w'){flag = 1; break;} 
            } 
    } 
    else 
    { 
        if(temph > tempv) 
        { 
            int temp = temph - tempv; 
            for(int i=top - temp; i<=top; i++) 
            { 
                if(i<1) continue; 
                if(i+temph-1>n) break; 
                if(judge(i,i+temph-1,left,right)) 
                { 
                    top = i; 
                    bottom = i+temph-1; 
                    flag1 = 0; 
                    break; 
                } 
            } 
            flag = flag1; 
        } 
        else if(temph < tempv) 
        { 
            int temp = tempv - temph;
            for(int i=left - temp; i<=left; i++) 
            { 
                if(i<1) continue;
                if(i+tempv-1>m) break; 
                if(judge(top,bottom,i,i+tempv-1)) 
                { 
                    left = i; 
                    right = i + tempv - 1; 
                    flag1 = 0; 
                    break; 
                } 
            } 
            flag = flag1; 
        } 
    } 
    if(flag==1) 
        printf("-1\n"); 
    else 
    { 
        for(int i=top; i<=bottom; i++) 
        { 
            if(ch[i][left]!='w') 
                ch[i][left] = '+'; 
            if(ch[i][right]!='w') 
                ch[i][right]='+'; 
        } 
        for(int i=left; i<=right; i++)
        {
            if(ch[top][i]!='w')
                ch[top][i]='+';
            if(ch[bottom][i]!='w')
                ch[bottom][i]='+';
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
                printf("%c",ch[i][j]);
            printf("\n");
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值