SHARE
TWEET

Untitled

a guest Mar 24th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4.  
  5. public class ccc03s3 {
  6.  
  7.     static int bfs(int rowStart, int colStart, char[][] floorPlan) {
  8.         int filled = 0;
  9.  
  10.         // list of places we need to check
  11.         Queue<Integer> row = new LinkedList<Integer>();
  12.         Queue<Integer> col = new LinkedList<Integer>();
  13.  
  14.         // add first location to check
  15.         row.add(rowStart);
  16.         col.add(colStart);
  17.  
  18.         // keep on looping until queue becomes empty
  19.         while (!row.isEmpty()) {
  20.             // get the next place to check
  21.             int currentRow = row.remove();
  22.             int currentCol = col.remove();
  23.  
  24.             // check if we already checked that place
  25.             if (floorPlan[currentRow][currentCol] == 'X') {
  26.                 continue;
  27.             }
  28.  
  29.             // check if there is a wall
  30.             if (floorPlan[currentRow][currentCol] == 'I') {
  31.                 continue;
  32.             }
  33.  
  34.             // fill location with water
  35.             floorPlan[currentRow][currentCol] = 'X';
  36.             filled++;
  37.  
  38.             // spread to adjacent cells
  39.  
  40.             // above
  41.             row.add(currentRow - 1);
  42.             col.add(currentCol);
  43.  
  44.             // below
  45.             row.add(currentRow + 1);
  46.             col.add(currentCol);
  47.  
  48.             // left
  49.             row.add(currentRow);
  50.             col.add(currentCol - 1);
  51.  
  52.             // right
  53.             row.add(currentRow);
  54.             col.add(currentCol + 1);
  55.         }
  56.  
  57.         return filled;
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.         Scanner s = new Scanner(System.in);
  62.         int flooring = s.nextInt();
  63.         int rows = s.nextInt();
  64.         int cols = s.nextInt();
  65.  
  66.         // create 2d array with size rows x cols
  67.         char[][] floorPlan = new char[rows][cols];
  68.  
  69.         for (int row = 0; row < rows; row++) {
  70.             String line = s.next();// read in one line
  71.             for (int col = 0; col < cols; col++) {
  72.                 floorPlan[row][col] = line.charAt(col);
  73.             }
  74.         }
  75.  
  76.         System.out.println(bfs(1, 1, floorPlan));
  77.  
  78.         for (int row = 0; row < rows; row++) {
  79.             for (int col = 0; col < cols; col++) {
  80.                 System.out.print(floorPlan[row][col]);
  81.             }
  82.             System.out.println();
  83.         }
  84.  
  85.     }
  86.  
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top