結果

問題 No.157 2つの空洞
ユーザー lunnearlunnear
提出日時 2019-01-18 15:15:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,734 bytes
コンパイル時間 390 ms
コンパイル使用メモリ 40,528 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 07:49:30
合計ジャッジ時間 1,297 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 WA -
testcase_19 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:184:32: warning: ‘v1’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     ans += abs((*v0).y - (*v1).y);
                          ~~~~~~^
main.cpp:184:22: warning: ‘v0’ may be used uninitialized in this function [-Wmaybe-uninitialized]
     ans += abs((*v0).y - (*v1).y);
                ~~~~~~^

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <list>

class Vector2
{
public:
    Vector2(short x = 0, short y = 0):x(x), y(y){}
    short x, y;
    
    float LengthSq(){return (x * x + y * y);}
    
    Vector2 operator -(Vector2 &a){return Vector2(x - a.x, y - a.y);}
};

struct Square
{
    char val = -1;
    bool check = false;
};

class Field
{
public:
    Field(int w, int h);
    ~Field();
    
private:
    int w, h;
    Square *sq;
    
public:
    Square* Get(int x, int y);
    bool isField(int x, int y);
};



Field::Field(int w, int h)
{
    this->w = w;
    this->h = h;
    
    sq = new Square[w * h];
}

Field::~Field()
{
    delete[] sq;
}

Square* Field::Get(int x, int y)
{
    return &sq[x + y * w];
}

bool Field::isField(int x, int y)
{
    if(x < 0)
        return false;
    
    if(y < 0)
        return false;
    
    if(x >= w)
        return false;
    
    if(y >= h)
        return false;
    
    return true;
}


int main()
{
    int w, h;
    scanf("%d %d", &w, &h);
    getchar();

    Field f(w, h);
    
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            
            Square *s = f.Get(x, y);
            
            s->val = (getchar() == '#')? 0:1;
        }
        getchar();
    }
    
    std::list<Vector2> holl[2];
    
    int count = 0;
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            
            Square *s = f.Get(x, y);
            
            if(s->check)
                continue;
            
            if(s->val != 1)
                continue;
            
            std::list<Vector2> history;
            while(1)
            {
                s->check = true;
                
                bool next = false;
                const Vector2 d[4] = {
                    {-1, 0},
                    { 1, 0},
                    { 0, -1},
                    { 0,  1}
                };
                for(int i = 0; i < 4; i++)
                {
                    if(!f.isField(x + d[i].x, y + d[i].y))
                        continue;
                    
                    Square *to = f.Get(x + d[i].x, y + d[i].y);
                    
                    if(to->val && !to->check)
                    {
                        Vector2 v;
                        v.x = x; v.y = y;
                        history.push_back(v);
                        s = to;
                        x += d[i].x; y += d[i].y;
                        next = true;
                        break;
                    }
                }
                if(next)
                    continue;
                
                holl[count].push_back(Vector2(x, y));
                
                if(history.empty())
                    break;
                

                Vector2 v = history.back();
                s = f.Get(v.x, v.y);
                x = v.x; y = v.y;
                history.pop_back();
            }
            
            count++;
            if(count == 2)
                break;
        }
        if(count == 2)
            break;
    }
    
    
    Vector2 *v0, *v1;
    float min = 999;
    for(auto i = holl[0].begin(); i != holl[0].end(); i++)
    {
        for(auto j = holl[1].begin(); j != holl[1].end(); j++)
        {
            Vector2 v = (*i) - (*j);
            float l = v.LengthSq();
            
            if(l < min)
            {
                v0 = &(*i);
                v1 = &(*j);
                min = l;
                
                if(min == 4)//  2^2
                    break;
            }
        }
    }
    
    int ans = -1;
    ans += abs((*v0).x - (*v1).x);
    ans += abs((*v0).y - (*v1).y);
    
    printf("%d\n", ans);
    return 0;
}
0