結果
| 問題 |
No.697 池の数はいくつか
|
| ユーザー |
|
| 提出日時 | 2023-11-26 10:58:29 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,788 bytes |
| コンパイル時間 | 3,098 ms |
| コンパイル使用メモリ | 86,176 KB |
| 実行使用メモリ | 370,108 KB |
| 最終ジャッジ日時 | 2024-09-26 11:34:32 |
| 合計ジャッジ時間 | 24,273 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 TLE * 2 -- * 8 |
ソースコード
import java.util.*;
import java.io.*;
public class Main {
private static int[] dy = {0,1,0,-1};
private static int[] dx = {1,0,-1,0};
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
try {
int[] h_w = readIntArray();
int h = h_w[0];
int w = h_w[1];
int[][] field = new int[h][w];
for(int i = 0 ; i < h ; i++){
int[] terrains = readIntArray();
field[i] = terrains;
}
ArrayDeque<Pair> todo = new ArrayDeque<>();
int ans = 0;
for(int i = 0 ; i < h ; i++){
for(int j = 0 ; j < w ; j++){
int currentH = i;
int currentW = j;
if(field[currentH][currentW] == 0){
continue;
}
Pair p = new Pair(currentH , currentW);
field = bfs(field , p , h , w , todo);
ans++;
}
}
System.out.println(ans);
} catch (Exception e) {
System.out.println("エラー");
}
}
private static int[][] bfs(int[][] field , Pair p , int h , int w , ArrayDeque<Pair> todo){
todo.clear();
todo.add(p);
while(!todo.isEmpty()){
Pair pair = todo.remove();
int currentH = pair.getY();
int currentW = pair.getX();
field[currentH][currentW] = 0;
for(int i = 0 ; i < 4 ; i++){
int nextH = currentH + dy[i];
int nextW = currentW + dx[i];
if (nextH < 0 || nextW < 0 || nextH >= h || nextW >= w) {
continue;
}
if(field[nextH][nextW] == 0){
continue;
}
Pair np = new Pair(nextH , nextW);
todo.add(np);
}
}
return field;
}
private static int[] readIntArray() throws Exception{
int[] intArray = Arrays.stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
return intArray;
}
}
class Pair{
int y , x;
public Pair(int y , int x){
this.y = y;
this.x = x;
}
public int getY(){
return y;
}
public int getX(){
return x;
}
}