Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class Main {
  4. static int[][] vis;
  5. static char[][] map;
  6. static int N, M;
  7. public static void main(String[] args) throws IOException {
  8. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  9. StringTokenizer st = new StringTokenizer(br.readLine());
  10. N = Integer.parseInt(st.nextToken());
  11. M = Integer.parseInt(st.nextToken());
  12. vis = new int[N][M];
  13. map = new char[N][M];
  14. for (int i = 0; i < N; i++) map[i] = br.readLine().toCharArray();
  15. int count = 1;
  16. int ans = 0;
  17. for (int i = 0; i < N; i++) {
  18. for (int j = 0; j < M; j++) {
  19. if (vis[i][j] == 0) {
  20. ans += f(i,j,count);
  21. count++;
  22. }
  23. }
  24. }
  25. System.out.println(ans);
  26. }
  27. static int f(int row, int col, int count) {
  28. if (vis[row][col] > 0) {
  29. return (vis[row][col] == count ? 1 : 0);
  30. }
  31. vis[row][col] = count;
  32. if (map[row][col] == 'N') {
  33. return f(row - 1, col, count);
  34. } else if (map[row][col] == 'W') {
  35. return f(row, col - 1, count);
  36. } else if (map[row][col] == 'S') {
  37. return f(row + 1, col, count);
  38. } else {
  39. return f(row, col + 1, count);
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement