Facebook Hacker Cup 2014 – Qualification Round Problem 1


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Here’s the first problem of the qualification round of the Facebook Hacker Cup 2014:

You want to write an image detection system that is able to recognize different geometric shapes. In the first version of the system you settled with just being able to detect filled squares on a grid.

You are given a grid of N×N square cells. Each cell is either white or black. Your task is to detect whether all the black cells form a square shape.

Input
The first line of the input consists of a single number T, the number of test cases.

Each test case starts with a line containing a single integer N. Each of the subsequent N lines contain N characters. Each character is either “.” symbolizing a white cell, or “#” symbolizing a black cell. Every test case contains at least one black cell.

Output
For each test case i numbered from 1 to T, output “Case #i: “, followed by YES or NO depending on whether or not all the black cells form a completely filled square with edges parallel to the grid of cells.

Constraints
1 ≤ T ≤ 20
1 ≤ N ≤ 20

Example
Test cases 1 and 5 represent valid squares. Case 2 has an extra cell that is outside of the square. Case 3 shows a square not filled inside. And case 4 is a rectangle but not a square.

Sample Input
5
4
..##
..##
….
….
4
..##
..##
#…
….
4
####
#..#
#..#
####
5
#####
#####
#####
#####
…..
5
#####
#####
#####
#####
#####

Output
Case #1: YES
Case #2: NO
Case #3: NO
Case #4: NO
Case #5: YES

My Solution

Not the most concise of codes, but it worked.


#include <stdio.h>
#include <string.h>

int hasSquare(char mat[][21], int grid){
  int i,j;
  int found = 0;
  int size;
  int row;
  int dotfound = 0;
  int res;

  for(i=0;i<grid;i++){
    for(j=0;j<grid;j++){
      if(found){
        if(dotfound){
          if(mat[i][j]=='#')
            return 0;
        }
        else{
          if(mat[i][j]=='#'){
            size++;
          }  
          else{
            dotfound = 1;
          }
        }  
      }
      else if(mat[i][j]=='#'){
        found = 1;    
        size = 1;
      }  
    }
    if(found){
      row = i;
      break;
    }
  }
  
  if(found==0)
    return 0;

  if(size>grid-row)
    return 0;
  
  if(size==1){
    for(i=row+1;i<grid;i++){
      res = strcmp(mat[i],"....");
      if(res!=0)
        return 0;  
    }
    return 1;
  }

  for(i=row+1;i<grid;i++){
    if(i<row+size){
      res = strcmp(mat[i],mat[row]);
      if(res!=0)
        return 0;
    }
    else{
      for(j=0;j<grid;j++)
        if(mat[i][j]=='#')
          return 0;
    }
  }

  return 1;
  
}

int main(){
  char mat[20][21];
  int i,j;
  int k,n;
  int grid;
  char c;
  
  scanf("%d",&n);

  for(k=0;k<n;k++){
    scanf("%d",&grid);
    scanf("%c",&c);
    for(i=0;i<grid;i++){  
      for(j=0;j<grid;j++){
        scanf("%c",&mat[i][j]);
      }
      mat[i][j] = '\0';
      scanf("%c",&c);
    }
    if(k>0)
      printf("\n");
    if(hasSquare(mat,grid))
      printf("Case #%d: YES",k+1);
    else
      printf("Case #%d: NO",k+1);
  }
  
  return 0;
}

One thought on “Facebook Hacker Cup 2014 – Qualification Round Problem 1

  1. Bipul Ranjan

    #include
    using namespace std;
    int main(){
    int T;
    cin >> T;
    for(int t=1; t> N;
    for(int i=0; i<N; i++){
    for(int j=0; j> c;
    if(!square)
    continue;
    switch(mode){
    case 0:
    if(c==’#’){
    mode = 1;
    rows = i;
    cols = j;
    }
    break;
    case 1:
    if(c==’.’){
    mode = 2;
    cole = j-1;
    rowe = rows+(cole-cols);
    }
    break;
    case 2:
    if(c==’#’ && (i>rowe || jcole))
    square = false;
    else if(c==’.’ && i=cols && j<=cole)
    square = false;
    }
    }
    if(mode==1){
    mode = 2;
    cole = N-1;
    rowe = rows+(cole-cols);
    }
    }
    if(square)
    cout << "Case #" << t << ": YES" << endl;
    else
    cout << "Case #" << t << ": NO" << endl;
    }
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *