Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Main {
- static int[][] vis;
- static char[][] map;
- static int N, M;
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st = new StringTokenizer(br.readLine());
- N = Integer.parseInt(st.nextToken());
- M = Integer.parseInt(st.nextToken());
- vis = new int[N][M];
- map = new char[N][M];
- for (int i = 0; i < N; i++) map[i] = br.readLine().toCharArray();
- int count = 1;
- int ans = 0;
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- if (vis[i][j] == 0) {
- ans += f(i,j,count);
- count++;
- }
- }
- }
- System.out.println(ans);
- }
- static int f(int row, int col, int count) {
- if (vis[row][col] > 0) {
- return (vis[row][col] == count ? 1 : 0);
- }
- vis[row][col] = count;
- if (map[row][col] == 'N') {
- return f(row - 1, col, count);
- } else if (map[row][col] == 'W') {
- return f(row, col - 1, count);
- } else if (map[row][col] == 'S') {
- return f(row + 1, col, count);
- } else {
- return f(row, col + 1, count);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement