結果

問題 No.697 池の数はいくつか
ユーザー lunnearlunnear
提出日時 2019-01-11 16:04:23
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,192 bytes
コンパイル時間 457 ms
コンパイル使用メモリ 37,284 KB
実行使用メモリ 635,104 KB
最終ジャッジ日時 2023-08-16 22:58:32
合計ジャッジ時間 9,437 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 77 ms
41,908 KB
testcase_25 AC 77 ms
41,472 KB
testcase_26 AC 78 ms
41,576 KB
testcase_27 AC 77 ms
41,524 KB
testcase_28 AC 78 ms
41,536 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);
    
    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;
            
            getchar();
        }
    }
    
    
    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", count);
    return 0;
}
0