java practice simple DFS POJ-1979 Red and Black

Keywords: Java

Title address

https://vjudge.net/problem/POJ-1979

Title Description

There is a room with only red and black bricks on the floor.

With a certain black brick as the starting point, it can move to four adjacent tiles (up, down, left and right). However, instead of stepping on red bricks, you can only step on black bricks.

Output the maximum number of black bricks that can be stepped on.

Black brick
Red brick
'@' starting point (unique, black brick)

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
0 0

  

thinking

Simple dfs, find the starting point @, search from the starting point in four directions, if it is black brick, dfs again. Change the searched brick to red as a marker, count + +.

Code

 1 import java.util.Scanner;
 2 
 3 
 4 /**
 5  * @author vastian
 6  */
 7 public class Main {
 8 
 9     public static void main(String[] args) {
10 
11         while (true) {
12             col = SC.nextInt();
13             row = SC.nextInt();
14 
15             // Eat up int Wrap after
16             SC.nextLine();
17             if (col == 0 && row == 0) {
18                 break;
19             }
20 
21             count = 0;
22             // Read in map
23             inputMap();
24 
25             // Find the starting point@
26             findStart();
27 
28             // Find black bricks from the beginning
29             dfs(sx, sy);
30 
31             System.out.println(count);
32         }
33 
34     }
35 
36     /**
37      * DX,DY It is the moving vector in four directions up: (0, - 1) down: (0,1) left: (- 1,0) right: (1,0)
38      */
39     private static final int[] DX = {0, 0, -1, 1};
40     private static final int[] DY = {-1, 1, 0, 0};
41     private static final Scanner SC = new Scanner(System.in);
42 
43 
44     /**
45      * count Record the number of black bricks
46      * sx, sy Coordinates corresponding to the starting point @ (sx,sy)
47      * row, rol Number of rows and columns corresponding to map size
48      * map Save map information
49      */
50     private static int count = 0;
51     private static int sx, sy, row, col;
52     private static char[][] map;
53 
54     public static void inputMap() {
55         // Read map
56         map = new char[row][];
57         for (int i = 0; i < row; i++) {
58             map[i] = SC.nextLine().toCharArray();
59         }
60     }
61 
62     public static void findStart() {
63         // Traverse the map to find a unique starting point@Coordinates of (sx,sy)
64         for (int i = 0; i < row; i++) {
65             for (int j = 0; j < col; j++) {
66                 if (map[i][j] == '@') {
67                     sx = i;
68                     sy = j;
69                     return;
70                 }
71             }
72         }
73     }
74 
75     public static void dfs(int r, int c) {
76 
77         // Mark black bricks as red bricks
78         map[r][c] = '#';
79         count++;
80         // ergodic DX,DY Get four directions
81         for (int i = 0; i < 4; i++) {
82             // tr,tc For the next possible step
83             int tr = r + DX[i];
84             int tc = c + DY[i];
85             // Within the scope of the map, only black bricks can keep going
86             if (0 <= tr && tr < row && 0 <= tc && tc < col && map[tr][tc] == '.') {
87                 dfs(tr, tc);
88             }
89         }
90     }
91 
92
93 
94 }

Posted by mrherman on Wed, 22 Apr 2020 09:45:28 -0700