Codeforces Beta Round #14 (Div. 2) A. Letter 水题

A. Letter

题目连接:

http://www.codeforces.com/contest/14/problem/A

Description

A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle’s sides should be parallel to the sheet’s sides.

Input

The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob’s sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square.

Output

Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better.

Sample Input

6 7
…….
....
..
….
..
..
..
….
..***..

Sample Output

..

..

Hint

题意

让你找到最小的矩形,使得这个矩形能够包含所有的*

题解:

水题嘛,记录最左上角和最右下角就好了。

代码

#include<bits/stdc++.h>
using namespace std;
string s[55];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    int ix=1e9,iy=1e9,ax=0,ay=0;
    for(int i=0;i<n;i++){
        cin>>s[i];
        for(int j=0;j<m;j++){
            if(s[i][j]=='*')
                ix=min(ix,i),iy=min(iy,j),ax=max(ax,i),ay=max(ay,j);
        }
    }
    for(int i=ix;i<=ax;i++){
        for(int j=iy;j<=ay;j++){
            cout<<s[i][j];
        }
        cout<<endl;
    }
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5767667.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞