publicstaticvoidmain(String[] args) { Scannersc=newScanner(System.in); N = sc.nextInt(); M = sc.nextInt(); board = newchar[N][M]; vis = newboolean[N][M]; for (inti=0; i < N; i++) { board[i] = sc.next().toCharArray(); } intres=0; for (inti=0; i < N; i++) { for (intj=0; j < M; j++) { if (!vis[i][j] && board[i][j] == 'W') { res++; dfs(i, j); } } } System.out.println(res); }
publicstaticvoiddfs(int x, int y) { vis[x][y] = true; for (inti=0; i < dir.length; i++) { intnx= x + dir[i][0]; intny= y + dir[i][1]; if (nx < 0 || ny < 0 || nx >= N || ny >= M) { continue; } if (!vis[nx][ny] && board[nx][ny] == 'W') { dfs(nx, ny); } } } }
publicstaticintdfs(int x, int y) { vis[x][y] = true; intarea=1; for (inti=0; i < dir.length; i++) { intnx= x + dir[i][0]; intny= y + dir[i][1]; if (nx < 0 || ny < 0 || nx >= n || ny >= m || wall[board[x][y]][i]) { continue; } if (!vis[nx][ny]) { area += dfs(nx, ny); } } return area; } }
classMain { // 2(0010) //1(0001) 4(0100) // 8(1000) //左,上,右,下 staticint[][] dir = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; staticint n, m; staticboolean[][] vis; staticint[][] board; publicstaticvoidmain(String... args)throws Exception { Scannersc=newScanner(newFile("./input.txt")); // Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); vis = newboolean[n][m]; board = newint[n][m]; for (inti=0; i < n; i++){ for (intj=0; j < m; j++) { board[i][j] = sc.nextInt(); } } intcount=0, max = 0; for (inti=0; i < n; i++) { for (intj=0; j < m; j++) { if (!vis[i][j]) { count++; max = Math.max(max, dfs(i, j)); } } } System.out.println(count); System.out.println(max); }
publicstaticintdfs(int x, int y) { vis[x][y] = true; intarea=1; for (inti=0; i < dir.length; i++) { intnx= x + dir[i][0]; intny= y + dir[i][1]; if (nx < 0 || ny < 0 || nx >= n || ny >= m) { continue; } if (!vis[nx][ny] && ((board[x][y]>>i)&1) == 0) { area += dfs(nx, ny); } } return area; } }