# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
410865 | GhOsT16790 | 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.
import java.util.*;
import java.io.*;
public class boi2013 {
static final int[] dx = {0, 0, -1, 1};
static final int[] dy = {-1, 1, 0, 0};
static int N = 1, H, W;
static int[][] grid;
static boolean[][] vist;
public static void main(String[] args) throws IOException {
Kattio io = new Kattio();
H = io.nextInt();
W = io.nextInt();
grid = new int[H][W];
for (int i = 0; i < H; i++) {
String line = io.next();
for (int j = 0; j < W; j++) {
grid[i][j] = (line.charAt(j) == 'F')?1:(line.charAt(j) == 'R')?2:-1;
}
}
// for(int[] row : grid) {
// for (int col : row) {
// System.out.print(col + "\t");
// }
// System.out.println();
// }
// System.out.println();
bfs(0,0, grid[0][0]);
// for(int[] row : grid) {
// for (int col : row) {
// System.out.print(col + "\t");
// }
// System.out.println();
// }
// System.out.println();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if(grid[i][j] >= 1) {
bfs(i, j, grid[i][j]);
N++;
// for(int[] row : grid) {
// for (int col : row) {
// System.out.print(col + "\t");
// }
// System.out.println();
// }
// System.out.println();
}
}
}
io.println(N);
io.close();
}
private static void bfs(int x, int y, int t) {
vist = new boolean[H][W];
Queue<Point> q = new LinkedList<>();
q.add(new Point(x, y));
vist[x][y] = true;
while (!q.isEmpty()) {
Point curr = q.poll();
if(grid[curr.x][curr.y] >= 1) grid[curr.x][curr.y] = 0;
for (int i = 0; i < 4; i++) {
int nx = curr.x + dx[i];
int ny = curr.y + dy[i];
if(nx < 0 || ny < 0 || nx >= H || ny >= W) continue;
if(vist[nx][ny]) continue;
if(grid[nx][ny] == -1) continue;
if(grid[nx][ny] != t && grid[nx][ny] != 0) continue;
vist[nx][ny] = true;
q.add(new Point(nx, ny));
}
}
}
private static class Point {
int x, y;
public Point(int a, int b) {
x = a;
y = b;
}
public String toString() {
return x + " " + y;
}
}
private static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in,System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName+".out"));
r = new BufferedReader(new FileReader(problemName+".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) {}
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}