(UVA) Fire - Solução
import java.io.*;
import java.util.*;
class Main {
public static boolean foundExit(char[][] matrix, int r, int c, int numRows, int numCols) {
// if Joe (J) is in the border of the maze and the spot is '.'
if (matrix[r][c] != '#' && matrix[r][c] != 'F' && (r == 0 || c == 0 || r == numRows-1 || c == numCols-1)) {
return true;
}
return false;
}
public static void spreadFire(char[][] matrix, int numRows, int numCols, Queue<Coord> fire) {
int[] vi = {-1,0,1,0};
int[] vj = {0,-1,0,1};
int currSize = fire.size();
for (int i = 0; i < currSize; i++) {
Coord f = fire.poll();
for (int j = 0; j < 4; j++) { // spread fire (4 positions: up, down, left, right - if it is possible)
int nextRow = f.r+vi[j];
int nextCol = f.c+vj[j] ;
if (nextRow >= 0 && nextCol >= 0 && nextRow < numRows && nextCol < numCols && matrix[nextRow][nextCol] == '.') {
matrix[nextRow][nextCol] = 'F';
Coord c = new Coord(nextRow, nextCol);
fire.add(c);
}
}
}
}
public static int bfs(Coord start, char[][] matrix, int numRows, int numCols, Queue<Coord> fire) {
Queue<Element> queue = new ArrayDeque<Element>();
Element e = new Element(start, 1);
queue.add(e);
int[][] addedToQueue = new int[numRows][numCols];
addedToQueue[start.r][start.c] = 1;
int[] posR = {-1,1,0,0};
int[] posC = {0,0,-1,1};
int prevDist = 0;
while (queue.size() > 0) {
Element currPos = queue.poll();
int currPosR = currPos.coord.r;
int currPosC = currPos.coord.c;
int currDist = currPos.dist;
if (foundExit(matrix, currPosR, currPosC, numRows, numCols)) {
return currPos.dist;
}
if (prevDist != currDist) {
spreadFire(matrix, numRows, numCols, fire);
}
for (int i = 0; i < 4; i++) { // check up, down, left, and right positions
int nextRow = currPosR+posR[i];
int nextCol = currPosC+posC[i];
if (nextRow >= 0 && nextRow < numRows && nextCol >= 0 && nextCol < numCols) { // if the position is valid
char pos = matrix[nextRow][nextCol];
if (pos == '.' && addedToQueue[nextRow][nextCol] == 0) { // if the position is '.' and it was not visited yet
e = new Element(new Coord(nextRow, nextCol), currDist+1);
queue.add(e);
addedToQueue[nextRow][nextCol] = 1;
}
}
}
prevDist = currDist;
}
return -1;
}
public static Coord readMatrix(BufferedReader br, char[][] matrix, Queue<Coord> fire, int numRows, int numCols) throws NumberFormatException, IOException {
Coord start = new Coord();
for (int j = 0; j < numRows; j++) {
String s = br.readLine();
for (int k = 0; k < numCols; k++) {
matrix[j][k] = s.charAt(k);
if (matrix[j][k] == 'J') {
start = new Coord(j, k);
}
if (matrix[j][k] == 'F') {
Coord c = new Coord(j, k);
fire.add(c);
}
}
}
return start;
}
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numTests = reader(br);
for (int i = 0; i < numTests; i++) {
int numRows = reader(br);
int numCols = reader(br);
Queue<Coord> fire = new ArrayDeque<Coord>();
char[][] matrix = new char[numRows][numCols];
Coord start = readMatrix(br, matrix, fire, numRows, numCols);
int ans = bfs(start, matrix, numRows, numCols, fire);
if (ans == -1) {
System.out.println("IMPOSSIBLE");
}
else {
System.out.println(ans);
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Coord {
int r;
int c;
public Coord() {
}
public Coord(int r, int c) {
this.r = r;
this.c = c;
}
}
class Element {
Coord coord;
int dist;
public Element(Coord coord, int dist) {
this.coord = coord;
this.dist = dist;
}
}
import java.util.*;
class Main {
public static boolean foundExit(char[][] matrix, int r, int c, int numRows, int numCols) {
// if Joe (J) is in the border of the maze and the spot is '.'
if (matrix[r][c] != '#' && matrix[r][c] != 'F' && (r == 0 || c == 0 || r == numRows-1 || c == numCols-1)) {
return true;
}
return false;
}
public static void spreadFire(char[][] matrix, int numRows, int numCols, Queue<Coord> fire) {
int[] vi = {-1,0,1,0};
int[] vj = {0,-1,0,1};
int currSize = fire.size();
for (int i = 0; i < currSize; i++) {
Coord f = fire.poll();
for (int j = 0; j < 4; j++) { // spread fire (4 positions: up, down, left, right - if it is possible)
int nextRow = f.r+vi[j];
int nextCol = f.c+vj[j] ;
if (nextRow >= 0 && nextCol >= 0 && nextRow < numRows && nextCol < numCols && matrix[nextRow][nextCol] == '.') {
matrix[nextRow][nextCol] = 'F';
Coord c = new Coord(nextRow, nextCol);
fire.add(c);
}
}
}
}
public static int bfs(Coord start, char[][] matrix, int numRows, int numCols, Queue<Coord> fire) {
Queue<Element> queue = new ArrayDeque<Element>();
Element e = new Element(start, 1);
queue.add(e);
int[][] addedToQueue = new int[numRows][numCols];
addedToQueue[start.r][start.c] = 1;
int[] posR = {-1,1,0,0};
int[] posC = {0,0,-1,1};
int prevDist = 0;
while (queue.size() > 0) {
Element currPos = queue.poll();
int currPosR = currPos.coord.r;
int currPosC = currPos.coord.c;
int currDist = currPos.dist;
if (foundExit(matrix, currPosR, currPosC, numRows, numCols)) {
return currPos.dist;
}
if (prevDist != currDist) {
spreadFire(matrix, numRows, numCols, fire);
}
for (int i = 0; i < 4; i++) { // check up, down, left, and right positions
int nextRow = currPosR+posR[i];
int nextCol = currPosC+posC[i];
if (nextRow >= 0 && nextRow < numRows && nextCol >= 0 && nextCol < numCols) { // if the position is valid
char pos = matrix[nextRow][nextCol];
if (pos == '.' && addedToQueue[nextRow][nextCol] == 0) { // if the position is '.' and it was not visited yet
e = new Element(new Coord(nextRow, nextCol), currDist+1);
queue.add(e);
addedToQueue[nextRow][nextCol] = 1;
}
}
}
prevDist = currDist;
}
return -1;
}
public static Coord readMatrix(BufferedReader br, char[][] matrix, Queue<Coord> fire, int numRows, int numCols) throws NumberFormatException, IOException {
Coord start = new Coord();
for (int j = 0; j < numRows; j++) {
String s = br.readLine();
for (int k = 0; k < numCols; k++) {
matrix[j][k] = s.charAt(k);
if (matrix[j][k] == 'J') {
start = new Coord(j, k);
}
if (matrix[j][k] == 'F') {
Coord c = new Coord(j, k);
fire.add(c);
}
}
}
return start;
}
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numTests = reader(br);
for (int i = 0; i < numTests; i++) {
int numRows = reader(br);
int numCols = reader(br);
Queue<Coord> fire = new ArrayDeque<Coord>();
char[][] matrix = new char[numRows][numCols];
Coord start = readMatrix(br, matrix, fire, numRows, numCols);
int ans = bfs(start, matrix, numRows, numCols, fire);
if (ans == -1) {
System.out.println("IMPOSSIBLE");
}
else {
System.out.println(ans);
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Coord {
int r;
int c;
public Coord() {
}
public Coord(int r, int c) {
this.r = r;
this.c = c;
}
}
class Element {
Coord coord;
int dist;
public Element(Coord coord, int dist) {
this.coord = coord;
this.dist = dist;
}
}
Comments
Post a Comment