結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             lunnear
                         | 
                    
| 提出日時 | 2019-01-11 16:17:58 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,389 bytes | 
| コンパイル時間 | 541 ms | 
| コンパイル使用メモリ | 36,352 KB | 
| 実行使用メモリ | 354,540 KB | 
| 最終ジャッジ日時 | 2024-11-25 14:34:47 | 
| 合計ジャッジ時間 | 6,042 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| 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);
      |     ~~~~~^~~~~~~~~~~~~~~~~
            
            ソースコード
#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;
}
            
            
            
        
            
lunnear