結果

問題 No.697 池の数はいくつか
ユーザー lunnearlunnear
提出日時 2019-01-11 16:17:58
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,389 bytes
コンパイル時間 1,106 ms
コンパイル使用メモリ 37,116 KB
実行使用メモリ 354,256 KB
最終ジャッジ日時 2023-08-16 22:58:40
合計ジャッジ時間 6,157 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,500 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 72 ms
41,516 KB
testcase_25 AC 71 ms
41,532 KB
testcase_26 AC 69 ms
41,608 KB
testcase_27 AC 69 ms
41,448 KB
testcase_28 AC 68 ms
41,452 KB
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Square
{
    char val = -1;
    bool check = false;
    Square *to[4] = {nullptr};
};

class Field
{
public:
    Field(int w, int h);
    ~Field();
    
private:
    int w, h;
    Square *sq;
    
public:
    Square* Get(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];
}



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

    Field f(w, h);
    
    int zeros = 0;
    for(int y = 0; y < h; y++){
        for(int x = 0; x < w; x++){
            
            Square *s = f.Get(x, y);
            
            s->val = getchar() - '0';

            s->to[0] = (x >= 1)?      f.Get(x - 1, y):nullptr;
            s->to[1] = (x < (w - 1))? f.Get(x + 1, y):nullptr;
            s->to[2] = (y >= 1)?      f.Get(x, y - 1):nullptr;
            s->to[3] = (y < (h - 1))? f.Get(x, y + 1):nullptr;
            
            if(s->val == 0)
                zeros++;
            
            getchar();
        }
    }
    
    int *min = (w < h)? &w:&h;
    if(*min > zeros)
    {
        printf("1\n");
        return 0;
    }
    
    
    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<Square*> history;
            while(1)
            {
                s->check = true;
                
                bool next = false;
                for(int i = 0; i < 4; i++)
                {
                    if(s->to[i] && s->to[i]->val && !s->to[i]->check)
                    {
                        history.push_back(s);
                        s = s->to[i];
                        next = true;
                        break;
                    }
                }
                if(next)
                    continue;
                
                if(history.empty())
                    break;
                
                s = history.back();
                history.pop_back();
            }
            
            count++;
        }
    }
    
    printf("%d\n", count);
    return 0;
}
0