結果

問題 No.697 池の数はいくつか
ユーザー lunnear
提出日時 2019-01-11 16:04:23
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
MLE  
実行時間 -
コード長 2,192 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 35,712 KB
実行使用メモリ 635,632 KB
最終ジャッジ日時 2024-11-25 14:34:41
合計ジャッジ時間 6,932 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 MLE * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:50:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   50 |     scanf("%d %d", &h, &w);
      |     ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

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