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 {
- static int bfs(int rowStart, int colStart, char[][] floorPlan) {
- int filled = 0;
- // list of places we need to check
- Queue<Integer> row = new LinkedList<Integer>();
- Queue<Integer> col = new LinkedList<Integer>();
- // add first location to check
- row.add(rowStart);
- col.add(colStart);
- // keep on looping until queue becomes empty
- while (!row.isEmpty()) {
- // get the next place to check
- int currentRow = row.remove();
- int currentCol = col.remove();
- // check if we already checked that place
- if (floorPlan[currentRow][currentCol] == 'X') {
- continue;
- }
- // check if there is a wall
- if (floorPlan[currentRow][currentCol] == 'I') {
- continue;
- }
- // fill location with water
- floorPlan[currentRow][currentCol] = 'X';
- filled++;
- // spread to adjacent cells
- // above
- row.add(currentRow - 1);
- col.add(currentCol);
- // below
- row.add(currentRow + 1);
- col.add(currentCol);
- // left
- row.add(currentRow);
- col.add(currentCol - 1);
- // right
- row.add(currentRow);
- col.add(currentCol + 1);
- }
- return filled;
- }
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- int flooring = s.nextInt();
- int rows = s.nextInt();
- int cols = s.nextInt();
- // create 2d array with size rows x cols
- char[][] floorPlan = new char[rows][cols];
- for (int row = 0; row < rows; row++) {
- String line = s.next();// read in one line
- for (int col = 0; col < cols; col++) {
- floorPlan[row][col] = line.charAt(col);
- }
- }
- System.out.println(bfs(1, 1, floorPlan));
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < cols; col++) {
- System.out.print(floorPlan[row][col]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement