Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- echo "<pre>";
- $numeroDeCidades = 9;
- $rangeDasTorres = 2;
- $cidadesComTorres = [0, 1, 1, 1, 1, 0, 1, 0, 0];
- $cidades = [];
- $numeroDeMovimentacoesDasTorres = 0;
- for($i=0;$i<$numeroDeCidades;$i++){
- $cidade = (new Cidade())->setIndex($i);
- $cidadesComTorres[$i]===1 ? $cidade->setTorre(new Torre()) : null;
- $cidades[] = $cidade;
- }
- list($cidades, $numeroDeMovimentacoesDasTorres) = colocarTorresNasCidadesDasExtremidades($cidades, $numeroDeMovimentacoesDasTorres);
- list($cidades, $numeroDeMovimentacoesDasTorres) = calcularMovimentacaoDasTorres($cidades, $rangeDasTorres, $numeroDeMovimentacoesDasTorres);
- debugCidades($cidades);
- class Cidade{
- private $index = null;
- private $torre = null;
- public function getIndex(){
- return $this->index;
- }
- public function setIndex($index){
- $this->index = $index;
- return $this;
- }
- public function getTorre(){
- return $this->torre;
- }
- public function setTorre(Torre $torre=null){
- $this->torre = $torre;
- return $this;
- }
- }
- class Torre{}
- function colocarTorresNasCidadesDasExtremidades($cidades, $numeroDeMovimentacoesDeTorres){
- print "\nNúmero de movimentações inicial: " . $numeroDeMovimentacoesDeTorres;
- if(is_null($cidades[0]->getTorre())){
- for($i=1;$i<count($cidades);$i++){
- if(!is_null($cidades[$i]->getTorre())){
- $cidades[0]->setTorre($cidades[$i]->getTorre());
- $cidades[$i]->setTorre(null);
- $numeroDeMovimentacoesDeTorres++;
- break;
- }
- }
- }
- if(is_null($cidades[count($cidades)-1]->getTorre())){
- for($i=count($cidades)-1;$i>0;$i--){
- if(!is_null($cidades[$i]->getTorre())){
- $cidades[count($cidades)-1]->setTorre($cidades[$i]->getTorre());
- $cidades[$i]->setTorre(null);
- $numeroDeMovimentacoesDeTorres++;
- break;
- }
- }
- }
- print "\nNovo número de movimentações: " . $numeroDeMovimentacoesDeTorres;
- return [$cidades, $numeroDeMovimentacoesDeTorres];
- }
- function calcularMovimentacaoDasTorres($cidades, $rangeDasTorres, $numeroDeMovimentacaoDasTorres){
- foreach($cidades as $key=>$cidade){
- $precisaMovimentarTorre = false;
- //Verificar se a cidade está energizada
- for($i=1;$i<=$rangeDasTorres*2;$i++){
- if(is_null($cidade->getTorre())){
- if(
- isset($cidades[$key-$i]) AND is_null($cidades[$key-$i]->getTorre()) OR
- isset($cidades[$key+$i]) AND is_null($cidades[$key+$i]->getTorre())
- ){
- $precisaMovimentarTorre = true;
- } else {
- $precisaMovimentarTorre = false;
- }
- }
- }
- echo "\nIndex: " . $cidade->getIndex() . ' - ';
- echo "Precisa movimentar: " . (int)$precisaMovimentarTorre;
- }
- return [$cidades, $numeroDeMovimentacaoDasTorres];
- }
- function debugCidades($cidades){
- foreach($cidades as $cidade){
- echo "\nCidadeIndex: " . $cidade->getIndex();
- echo "\nTorre: " . (string)!is_null($cidade->getTorre());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement