Skip to content

Instantly share code, notes, and snippets.

@acirtautas
Created November 22, 2013 08:56
Show Gist options
  • Save acirtautas/7596910 to your computer and use it in GitHub Desktop.
Save acirtautas/7596910 to your computer and use it in GitHub Desktop.
Square detector @ Facebook hacker cup 2014 qualification round
<?php
/**
* Facebook Hacker Cup 2014 Qualification Round
*
* Square detector
*
* @author Alfonsas Cirtautas
*/
// Read data
$raw = file('square_detector_input.txt');
$cnt = reset($raw);
$rez = '';
// Detect square in each image.
for ($t = 1, $i=1; $i<count($raw); $i++) {
$n = $raw[$i];
$c = array_slice($raw, $i+1, $n);
$s = square($c, $n)?'YES':'NO';
$rez.= "Case #{$t}: {$s}".PHP_EOL;
$i+= $n;
$t++;
}
// Save results
file_put_contents('square_detector_output.txt', $rez);
// Where the magic happens ;)
function square($canvas, $n) {
// Empty canvas ?
if (strpos(implode('', $canvas), '#') === false) {
return false;
}
$rc = $cc = 0;
for($i=0;$i<$n;$i++) {
$canvas = checkCol($canvas, $i, $rc);
$canvas = checkRow($canvas, $i, $cc);
}
// Is square ?
if($rc !== $cc) {
return false;
}
// Has holes ?
if (strpos(implode('', $canvas), '.') !== false) {
return false;
}
// Bingo
return true;
}
// Check free rows
function checkRow($canvas, $n, &$rc) {
if(strpos($canvas[$n],'#') === false) {
$canvas[$n] = str_replace('.', '*', $canvas[$n]);
$rc ++;
}
return $canvas;
}
// Check free cols
function checkCol($canvas, $n, &$cc) {
$c = $canvas;
foreach($canvas as $r => $row) {
if($row[$n] == '#') {
return $canvas;
}
$c[$r][$n] = '*';
}
$cc++;
return $c;
}
5
4
..##
..##
....
....
4
..##
..##
#...
....
4
####
#..#
#..#
####
5
#####
#####
#####
#####
.....
5
#####
#####
#####
#####
#####
Case #1: YES
Case #2: NO
Case #3: NO
Case #4: NO
Case #5: YES
20
20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
..........######....
..........######....
..........######....
..........######....
..........######....
..........######....
....................
10
..........
..........
.##.......
.##.......
..........
..........
......##..
......##..
..........
..........
20
....................
....................
....................
....................
########..#######...
########..########..
##........##....##..
##........##....##..
########..#######...
########..########..
##........##....##..
##........##....##..
##........########..
##........#######...
....................
....................
....................
....................
....................
....................
10
..........
..........
.......###
.......###
.......###
..........
..........
..........
..........
..........
20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
...........#########
...........#########
...........#########
...........#########
...........#########
...........#########
...........#########
...........#########
...........#########
4
..##
..##
....
....
4
####
#..#
#..#
####
20
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
###################.
10
..........
..........
..........
..........
..........
..........
..........
..........
..........
.........#
5
#####
#####
#####
#####
.....
10
..........
.######...
.#####.#..
.######...
.######...
.######...
.######...
..........
..........
..........
10
..........
..........
####......
####......
####......
####......
..........
..........
..........
.........#
20
....................
################....
################....
################....
################....
################....
################....
################....
################....
################....
################....
################....
################....
################....
################....
################....
....................
....................
....................
....................
20
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
####################
4
..##
..##
#...
....
20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
.......##...........
.......##...........
10
..........
..........
..........
...##.....
...##.....
...##.....
..........
..........
..........
..........
4
###.
.###
.###
....
5
#####
#####
#####
#####
#####
20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
.......#####........
.......#####........
.......#####........
.......#####........
....................
....................
....................
....................
....................
Case #1: YES
Case #2: NO
Case #3: NO
Case #4: YES
Case #5: YES
Case #6: YES
Case #7: NO
Case #8: NO
Case #9: YES
Case #10: NO
Case #11: NO
Case #12: NO
Case #13: NO
Case #14: YES
Case #15: NO
Case #16: YES
Case #17: NO
Case #18: NO
Case #19: YES
Case #20: NO
@zerocold2
Copy link

in this testcase
10
..........
..........
.##.......
.##.......
..........
..........
......##..
......##..
..........
..........
should be YES/NO
and could you upload source the third problem Tennison

@jitendratheta
Copy link

NO. Here not all the '#' (Black) are together making one square

@acirtautas
Copy link
Author

Exactly jitendratheta, there was a rule, that ALL #'s must form a square.

Ant Sorry, I had no time to solve Tennison problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment