问题 A: Appalling Architecture

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

You have recently been hired as an architect for the BAPC (Bureau of Architecture and Promising Constructions), responsible for top-quality buildings such as the Tower of Pisa. However, in the past couple of weeks, some of the constructions that the BAPC has made have collapsed! It is up to you to figure out whether any other constructions are in danger.

After some research it seems like the x-coordinate of the center of gravity of some of the constructions is off: if this is too much to the left or to the right, the construction will fall over. Hence, you decide to check all the blueprints and see whether the constructions are stable or not.

Given is an up to 100 by 100 grid of characters in .#/_|-. The . characters denote empty space, while each other character represents a completely filled 1 × 1 box (any difference in symbols used is due to the artistic freedom of the other architects), whose center of mass is at the center of the box.

Every construction forms a single connected component that touches the ground, i.e. the bottom layer of the grid.

The construction falls to the left if the x-coordinate of the center of gravity is less than the x-coordinate of the leftmost point of the construction that touches the ground, and it falls to the right if the x-coordinate of the center of gravity is larger than the x-coordinate of the rightmost point of the construction that touches the ground. It is guaranteed that the center of gravity is never exactly above the leftmost or rightmost point where the building touches the ground.

Given a blueprint, is the construction balanced, does it fall to the left, or does it fall to the right?

输入

The first line has 1 ≤ h ≤ 100 and 1 ≤ w ≤ 100, the height and width of the grid.

Then follow h lines with l characters each. Each character is either ., indicating empty space, or one of #/_|-, indicating a filled 1 × 1 box .

输出

Print a single line containing left, balanced, or right.

样例输入

3 3

/-\

|.|

#.#

样例输出

balanced

#include <bits/stdc++.h>
 
using namespace std;
 
string ans1="balanced";
string ans2="left";
string ans3="right";
 
char e[111][111];
double pi[111];
int main () {
    int h,w;
    cin>>h>>w;
    string s;
    int m=0,cnt=0;
    for(int i=1;i<=h;i++) {
        cin>>s;
        int l=s.length ();
        for(int j=0;j<l;j++) {
            if(s[j]!='.') {
                m=m+j+1;
                cnt++;
            }
        }
    }
    double p=m*1.0/cnt;
    double x1=0,x2=0;
    for(int i=0;i<s.length ();i++) {
        if(s[i]!='.') {
            x1=i+1;
            break;
        }
    }
 
    for(int i=s.length ()-1;i>=0;i--) {
        if(s[i]!='.') {
            x2=i+1;
            break;
        }
    }
    x1=x1-0.5;
    x2=x2+0.5;
    if(p>x1&&p<x2) cout<<ans1<<endl;
    else if(p<x1) cout<<ans2<<endl;
    else cout<<ans3<<endl;
    return 0;
}

留下评论

通过 WordPress.com 设计一个这样的站点
从这里开始