File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class boj16724 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+
8+ static boolean [][] visited, finished;
9+ static int answer;
10+ static int [][] board;
11+ static int [] dr = {- 1 ,1 ,0 ,0 };
12+ static int [] dc = {0 ,0 ,- 1 ,1 };
13+ public static void main (String [] args ) throws Exception {
14+ StringTokenizer st = new StringTokenizer (br. readLine());
15+
16+ int N = Integer . parseInt(st. nextToken());
17+ int M = Integer . parseInt(st. nextToken());
18+
19+ board = new int [N ][M ];
20+ visited = new boolean [N ][M ];
21+ finished = new boolean [N ][M ];
22+ answer = 0 ;
23+
24+ for (int i= 0 ;i< N ;i++ ) {
25+ String line = br. readLine();
26+ for (int j= 0 ;j< M ;j++ ) {
27+ int c = line. charAt(j);
28+ if (c == ' U' ) board[i][j] = 0 ;
29+ else if (c == ' D' ) board[i][j] = 1 ;
30+ else if (c == ' L' ) board[i][j] = 2 ;
31+ else if (c == ' R' ) board[i][j] = 3 ;
32+ }
33+ }
34+
35+ for (int i= 0 ;i< N ;i++ ) {
36+ for (int j= 0 ;j< M ;j++ ) {
37+ if (! visited[i][j]) dfs(i,j);
38+ }
39+ }
40+ System . out. println(answer);
41+ }
42+
43+ public static void dfs (int r , int c ) {
44+
45+ visited[r][c] = true ;
46+
47+ int nr = r + dr[board[r][c]];
48+ int nc = c + dc[board[r][c]];
49+
50+ if (! visited[nr][nc]) {
51+ dfs(nr,nc);
52+ }else {
53+ if (! finished[nr][nc]) answer++ ;
54+ }
55+ finished[r][c] = true ;
56+ }
57+ }
58+
59+ ```
You can’t perform that action at this time.
0 commit comments