Monday, November 2, 2015

Valentine's Day Gift

Roman loved diamonds. Monica decided to give him a beautiful gift on Valentine's Day. Her idea of diamonds was different though. She lit up all the windows of her rectangular building with N floors and M windows on each floor, with 2 shapes - / or \ . According to her, a diamond was made when such a shape was created:

/ \ 
\ /
Given the shape of lights in all the windows, help Roman count the number of diamonds formed. Note: The components of the diamond should be adjacent to each other. Input: First line contains T - the number of testcases. For each testcase, First line contains 2 space-separated positive integers - N and M - the no. of floors in the building and the no. of windows on each floor respectively. N lines follow - each line contains M space-separated characters. Every character is either \ or / . Output: Print a single integer - the total no. of diamonds formed. Constraints: 1 ≤ T ≤ 5 2 ≤ N, M ≤ 1000 Sample Input(Plaintext Link)
 1
2 4
/ \ / \
\ / \ /

Output:
2

Solutions
 public class Solution {  
   public static void main(String[] args) throws IOException {  
     Scanner in = new Scanner(System.in);  
     int t;  
     t = in.nextInt();  
     int m, n;  
     for (int i = 0; i < t; i++) {  
       n = in.nextInt();  
       m = in.nextInt();  
       String[][] st1 = new String[n][m];  
       in.nextLine();  
       for (int j = 0; j < n; j++) {  
         st1[j] = in.nextLine().split(" ");  
       }  
       int count = 0;  
       for (int j = 0; j < n - 1; j++) {  
         for (int k = 0; k < m - 1; k++) {  
           if (st1[j][k].equals("/") && st1[j][k + 1].equals("\\")) {  
             if (st1[j + 1][k].equals("\\") && st1[j + 1][k + 1].equals("/")) {  
               count++;  
             }  
           }  
         }  
       }  
       System.out.println(count);  
     }  
   }  
 }  
Comments are welcome.

No comments:

Post a Comment