CodeForces 370D Broken Monitor

来源:互联网 发布:手机淘宝店铺关注链接 编辑:程序博客网 时间:2024/05/23 23:37
#include <cstdlib>#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>using namespace std;#define N 2005char 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