# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
410034 | PikaChu999 | Tracks in the Snow (BOI13_tracks) | Java | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
import java.util.*;
import java.io.*;
public class Main {
public static int height;
public static int width;
public static String[][] grid;
public static int res = 0;
public static int[] xCoors = {0,0,-1,1};
public static int[] yCoors = {1,-1,0,0};
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer details = new StringTokenizer(br.readLine());
height = Integer.parseInt(details.nextToken());
width = Integer.parseInt(details.nextToken());
grid = new String[height][width];
for(int x = 0; x < height; x++){
String row = br.readLine();
for(int y = 0; y < width; y++){
grid[x][y] = String.valueOf(row.charAt(y));
}
}
floodfill();
System.out.println(res);
br.close();
}
public static void floodfill(){
boolean[][] seen = new boolean[height][width];
for(int a = 0; a < height; a++){
for(int b = 0; b < width; b++){
if(!seen[a][b] && !grid[a][b].equals(".")){
res++;
String animal = grid[a][b];
boolean otherAnimal = false;
Queue<Coord> tree = new LinkedList<Coord>();
tree.add(new Coord(a, b));
while(!tree.isEmpty()){
Coord current = tree.poll();
if(!seen[current.x][current.y]){
if(!grid[current.x][current.y].equals(animal)) otherAnimal = true;
System.out.println(current + " " + grid[current.x][current.y]);
seen[current.x][current.y] = true;
for(int c = 0; c < 4; c++){
int xCoord = current.x + xCoors[c];
int yCoord = current.y + yCoors[c];
if(xCoord < 0 || yCoord < 0 || xCoord >= height || yCoord >= width || grid[xCoord][yCoord].equals(".")) continue;
tree.add(new Coord(xCoord, yCoord));
}
}
}
if(otherAnimal) res++;
}
}
}
}
}
class Coord{
int x;
int y;
public Coord(int xC, int yC){
x = xC;
y = yC;
}
public String toString(){
return x + " " + y;
}
}