Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.Scanner;
- public class ccc03s3_2 {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int flooring = s.nextInt();
- int rows = s.nextInt();
- int columns = s.nextInt();
- char[][] floorPlan = new char[rows][columns];
- // read each line in the floor plan
- for (int row = 0; row < rows; row++) {
- String line = s.next();
- // read each letter in the line
- for (int col = 0; col < columns; col++) {
- floorPlan[row][col] = line.charAt(col);
- }
- }
- for (int startRow = 0; startRow < rows; startRow++) {
- for (int startCol = 0; startCol < columns; startCol++) {
- // create the queue
- Queue<Integer> rowQueue = new LinkedList<Integer>();
- Queue<Integer> colQueue = new LinkedList<Integer>();
- rowQueue.add(startRow);
- colQueue.add(startCol);
- int cnt = 0;
- while (!rowQueue.isEmpty()) {
- int currentRow = rowQueue.remove();
- int currentCol = colQueue.remove();
- // check if out of bounds
- if (currentRow < 0 || currentCol < 0) {
- continue;
- }
- if (currentCol >= columns || currentRow >= rows) {
- continue;
- }
- // check if wall
- if (floorPlan[currentRow][currentCol] == 'I') {
- continue;
- }
- // check if already marked
- if (floorPlan[currentRow][currentCol] == 'X') {
- continue;
- }
- // mark it
- floorPlan[currentRow][currentCol] = 'X';
- cnt++;
- // spread to cells beside
- rowQueue.add(currentRow - 1);
- colQueue.add(currentCol);
- rowQueue.add(currentRow + 1);
- colQueue.add(currentCol);
- rowQueue.add(currentRow);
- colQueue.add(currentCol - 1);
- rowQueue.add(currentRow);
- colQueue.add(currentCol + 1);
- }
- if (cnt > 0) {
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- System.out.print(floorPlan[row][col]);
- }
- System.out.println();
- }
- System.out.println();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement