Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created September 22, 2016 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chermehdi/62974e8aa78049c05d206506876d6817 to your computer and use it in GitHub Desktop.
Save chermehdi/62974e8aa78049c05d206506876d6817 to your computer and use it in GitHub Desktop.
Kattis Fire Solution
package com.algorithms;
/**
* @Author Mehdi Maick
* Created on 22/09/2016.
*/
import java.awt.*;
import java.util.*;
import java.io.*;
public class Fire {
static int[] dx = {1, -1, 0, 0};
static int[] dy = {0, 0, 1, -1};
static char[][] map;
static int[][] dist, fdist;
static int sx, sy;
public static void solve(FastReader fs, PrintWriter pw) {
int t = fs.nextInt();
while (t-- > 0) {
int m = fs.nextInt();
int n = fs.nextInt();
map = new char[n][m];
dist = new int[n][m];
for (int i = 0; i < n; i++) {
Arrays.fill(dist[i], -1);
}
Queue<Cell> q = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
map[i] = fs.nextLine().trim().toCharArray();
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == '@') {
q.add(new Cell(i, j, '@'));
dist[i][j] = 0;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == '*') {
q.add(new Cell(i, j, '*'));
}
}
}
int ans = -1;
while (!q.isEmpty()) {
Cell node = q.poll();
if (node.type == '@' && map[node.x][node.y] == '*')
continue;
if (node.type == '@' && (node.y == 0 || node.y == m - 1 || node.x == 0 || node.x == n - 1)) {
ans = dist[node.x][node.y] + 1;
break;
}
for (int i = 0; i < 4; i++) {
int x = node.x + dx[i];
int y = node.y + dy[i];
if (y < 0 || y >= m || x < 0 || x >= n)
continue;
if (map[x][y] == '#' || map[x][y] == '*')
continue;
if (node.type == '@' && dist[x][y] == -1) {
dist[x][y] = dist[node.x][node.y] + 1;
q.add(new Cell(x, y, '@'));
}
if (node.type == '*') {
q.add(new Cell(x, y, '*'));
map[x][y] = '*';
}
}
}
if (ans == -1) {
pw.println("IMPOSSIBLE");
} else {
pw.println(ans);
}
}
}
static class Cell {
int x, y;
char type;
public Cell(int x, int y, char type) {
this.x = x;
this.y = y;
this.type = type;
}
}
public static void main(String[] args) throws Exception {
FastReader fs = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(fs, pw);
fs.close();
pw.close();
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nextLong();
}
return arr;
}
int[] nextIntArrayOneBased(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArrayOneBased(int n) {
long[] arr = new long[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextLong();
}
return arr;
}
void close() {
try {
reader.close();
} catch (IOException e) {
System.err.println("There's been an error trying closing the reader ");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment