結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             tenten
                         | 
                    
| 提出日時 | 2020-09-03 11:57:10 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                MLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,283 bytes | 
| コンパイル時間 | 4,898 ms | 
| コンパイル使用メモリ | 76,736 KB | 
| 実行使用メモリ | 756,920 KB | 
| 最終ジャッジ日時 | 2024-11-25 16:32:48 | 
| 合計ジャッジ時間 | 43,023 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 MLE * 2 | 
ソースコード
import java.util.*;
public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int h = sc.nextInt();
    	int w = sc.nextInt();
    	boolean[][] field = new boolean[h + 2][w + 2];
    	boolean[][] used = new boolean[h + 2][w + 2];
    	for (int i = 1; i <= h; i++) {
    	    for (int j = 1; j <= w; j++) {
    	        field[i][j] = (sc.nextInt() == 1);
    	    }
    	}
    	int count = 0;
    	for (int i = 1; i <= h; i++) {
    	    for (int j = 1; j <= w; j++) {
    	        if (field[i][j] && !used[i][j]) {
    	            count++;
    	            setUse(i, j, field, used);
    	        }
    	    }
    	}
    	System.out.println(count);
	}
	
	static void setUse(int r, int c, boolean[][] field, boolean[][] used) {
	    if (field[r][c] && !used[r][c]) {
	        used[r][c] = true;
	        if (field[r - 1][c] && !used[r - 1][c]) {
	            setUse(r - 1, c, field, used);
	        }
	        if (field[r + 1][c] && !used[r + 1][c]) {
	            setUse(r + 1, c, field, used);
	        }
	        if (field[r][c - 1] && !used[r][c - 1]) {
	            setUse(r, c - 1, field, used);
	        }
	        if (field[r][c + 1] && !used[r][c + 1]) {
	            setUse(r, c + 1, field, used);
	        }
	    }
	}
}
            
            
            
        
            
tenten