Example #1
0
function resolve($content)
{
    $output = "";
    $rows = explode("\n", trim($content));
    $casesCount = array_shift($rows);
    for ($i = 1; $i <= $casesCount; $i++) {
        $params = explode(" ", array_shift($rows));
        $boardLong = $params[0];
        $piecesRequired = $params[1];
        $board = array_splice($rows, 0, $boardLong);
        $R = array();
        $B = array();
        foreach ($board as $y => $line) {
            $slots = str_split($line);
            $emptySlots = array();
            for ($x = count($slots) - 1; $x >= 0; $x--) {
                if ($slots[$x] == ".") {
                    $emptySlots[] = $x;
                } else {
                    if (!empty($emptySlots)) {
                        $rightmostEmptySlot = array_shift($emptySlots);
                        ${$slots[$x]}[] = $rightmostEmptySlot . "#" . $y;
                        $emptySlots[] = $x;
                    } else {
                        ${$slots[$x]}[] = $x . "#" . $y;
                    }
                }
            }
        }
        _rotate($R, $boardLong);
        _rotate($B, $boardLong);
        $Rwin = _check4directions($R, $piecesRequired);
        $Bwin = _check4directions($B, $piecesRequired);
        if ($Rwin && $Bwin) {
            $output .= "Case #{$i}: Both\n";
        } else {
            if ($Rwin && !$Bwin) {
                $output .= "Case #{$i}: Red\n";
            } else {
                if (!$Rwin && $Bwin) {
                    $output .= "Case #{$i}: Blue\n";
                } else {
                    $output .= "Case #{$i}: Neither\n";
                }
            }
        }
    }
    return trim($output);
}
Example #2
0
 /**
  * Recursively serializes adjacent bnode combinations.
  *
  * @param s the serialization to update.
  * @param iri the IRI of the bnode being serialized.
  * @param siri the serialization name for the bnode IRI.
  * @param mb the MappingBuilder to use.
  * @param dir the edge direction to use ('props' or 'refs').
  * @param mapped all of the already-mapped adjacent bnodes.
  * @param notMapped all of the not-yet mapped adjacent bnodes.
  */
 public function serializeCombos($s, $iri, $siri, $mb, $dir, $mapped, $notMapped)
 {
     // handle recursion
     if (count($notMapped) > 0) {
         // copy mapped nodes
         $mapped = _clone($mapped);
         // map first bnode in list
         $mapped->{$mb->mapNode($notMapped[0]->s)} = $notMapped[0]->s;
         // recurse into remaining possible combinations
         $original = $mb->copy();
         $notMapped = array_slice($notMapped, 1);
         $rotations = max(1, count($notMapped));
         for ($r = 0; $r < $rotations; ++$r) {
             $m = $r === 0 ? $mb : $original->copy();
             $this->serializeCombos($s, $iri, $siri, $m, $dir, $mapped, $notMapped);
             // rotate not-mapped for next combination
             _rotate($notMapped);
         }
     } else {
         $keys = array_keys((array) $mapped);
         sort($keys);
         $entry = new stdClass();
         $entry->i = $iri;
         $entry->k = $keys;
         $entry->m = $mapped;
         $mb->adj->{$siri} = $entry;
         $this->serializeMapping($mb);
         // optimize away mappings that are already too large
         if ($s->{$dir} === null or _compareSerializations($mb->s, $s->{$dir}->s) <= 0) {
             // recurse into adjacent values
             foreach ($keys as $i => $k) {
                 $this->serializeBlankNode($s, $mapped->{$k}, $mb, $dir);
             }
             // update least serialization if new one has been found
             $this->serializeMapping($mb);
             if ($s->{$dir} === null or _compareSerializations($mb->s, $s->{$dir}->s) <= 0 and strlen($mb->s) >= strlen($s->{$dir}->s)) {
                 $s->{$dir} = new stdClass();
                 $s->{$dir}->s = $mb->s;
                 $s->{$dir}->m = $mb->mapping;
             }
         }
     }
 }