Quão grande é a sua terra?


23

Neste desafio, você calculará o tamanho da sua terra.


Escreva um programa ou função que calcule o tamanho da sua terra, considerando um muro que você construiu. Você recebe uma sequência de entrada não vazia contendo um conjunto de 4 caracteres distintos de sua escolha que representam as quatro direções "para cima", "para baixo", "esquerda" e "direita" (usarei ^ v < >neste desafio). Não é possível fazer curvas de 180 graus ( <>ou ^v), mas você pode atravessar a parede.

A maneira como você "captura" a terra é cercando-a com sua parede. O muro em si também é considerado parte de sua terra. Alguns exemplos tornarão mais claro. Usarei opara terrenos cercados pela parede, xpara a parede em si e Spara o ponto de partida da parede, apenas para ilustrar como a parede é construída. A saída deve ser o tamanho total da sua terra (o número de o, xe Snos casos de teste abaixo).

Input: >>>>
Land: Sxxxx
Output: 5

Input: <<<^^^>>>vv
Land:
xxxx
xoox
xoox
xxxS
Output: 16

Input: <<<^^^>>>v
Land:
xxxx
x  x
x  
xxxS 
Output: 11

Input: <
Land: xS
Output: 2 

Input: >>>>>>vvvvvvvvv<<<<<^^^^>>>>>>>>vvvvvvvvvv<<<<<<<<<<<<<<<^^^^^^^^^>>>vvvvvv<<<<<
Land:
        Sxxxxxx
              x
              x
              x
              x  
         xxxxxxxxx
  xxxx   xoooox  x
  xoox   xoooox  x
  xoox   xoooox  x
  xoox   xxxxxx  x
  xoox           x
  xoox           x
xxxxxx           x
  x              x
  x              x
  xxxxxxxxxxxxxxxx
Output: 101

Input: >>vvvv>>^^<<<<^
Land:
Sxx
xox
xxxxx
  xox
  xxx
Output: 17

Input: <<^^^>>>vv
Land:
xxxx
x  x
x  x
xxS
Output: 11   <- Note, diagonal edges do not close the "loop"

Esclarecimentos:

  • Você não precisa desenhar a parede, a saída deve ser apenas um número inteiro
  • O formato de entrada é opcional. Você pode usar uma string com <>^v, uma lista de dígitos (1, -1, i, -i), uma lista de caracteres ABCDetc.

Este é o portanto o código mais curto em cada idioma vence. Lembre-se, as explicações são importantes, mesmo em idiomas "regulares"!


1
Você deve alterar a descrição para calcular quantos trevos incluiu: P
fəˈnɛtɪk



@MatthewRoh, hmmm .
Stewie Griffin

@ Stewie Ah, sim, isso também está relacionado #
Matthew Roh

Respostas:


6

Python 2 , 385 345 332 bytes

A,I,R=max,min,range
a=b=0
p=[[a,b]]
for i in input():a+=i%2*(2-i);b+=(1-i%2)*(1-i);p+=[a,b],
k,l=zip(*p)
x=A(k)-I(k)+3
y=A(l)-I(l)+3
o=[[1]*y for _ in' '*x]
def g(m,n):
 if 0<o[m][n]and[m+I(k)-1,n+I(l)-1]not in p:o[m][n]=0;[g(i,j)for i in R(A(0,m-1),I(x,m+2))for j in R(A(0,n-1),I(y,n+2))if(i,j)!=(m,n)]
g(0,0)
print sum(map(sum,o))

Experimente online! ou Experimente todos os casos de teste

A entrada é numérica, 0 ~ 3, o índice 0 dos símbolos aqui: >v<^

#starting position
a,b=0
#new list to hold the wall coordinates
p=[[a,b]]

#iterate over the input calculating
#the next coordinate and storing on p
for i in input():
 a=a+i%2*(2-i)
 b=b+(1-i%2)*(1-i)
 p+=[[a,b]]
#i%2*(2-i) and (1-i%2)*(1-i) generate the increment
#of each symbol from last position 
# >/0 : (0,1)
# v/1 : (1,0)
# </2 : (0,-1)
# ^/3 : (-1,0)

#transpose the coordinate list
k,l=zip(*p)
#calculate the difference between the max and min values
#to generate the total land size
#adding a border to avoid dead-ends
x=max(k)-min(k)+3
y=max(l)-min(l)+3

#create a matrix of 1's with the total land size
o=[([1]*y) for _ in ' '*x]

#recursive function that sets a cell to 0
#and call itself again on all surrounding cells
def g(m,n):
 #correct the indexes (like negative ones)
 a,b=m+min(k)-1,n+min(l)-1
 #if this cell contains 1 and don't belong to the wall
 if o[m][n]>0 and (a,b) not in p:
  #sets to 0
  o[m][n]=0
  #call again on surrounding cells
  for i in range(max(0,m-1),min(x,m+2)):
   for j in range(max(0,n-1), min(y,n+2)):
    if (i,j)!=(m,n):g(i,j)

#call the recursive function o origin
g(0,0)
#print the sum of the cells
print sum(map(sum,o))

Esta é a matriz resultante:

[0, 0, 0,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 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

3

Oitava, 83 85 83 79 bytes

@(p)nnz(bwfill(accumarray([real(c=cumsum([0;p])) imag(c)]+nnz(p)+1,1),"holes"))

Experimente no Octave Online!

Uma função que recebe como entrada um vetor de coluna contendo (1, -1, i, -i)

Usando a abordagem da resposta do Mathematica do @ lanlock4, adicione o comprimento da entrada às coordenadas para evitar coordenadas não positivas, em vez de subtrair o mínimo de coordenadas delas. 4 bytes salvos.

Resposta anterior:

@(p)nnz(bwfill(accumarray((k=[real(c=cumsum([0;p])) imag(c)])-min(k)+1,1),"holes"))

Experimente no Octave Online!

Alterado para melhor visualização.

Explicação:

%compute position of walls
c= cumsum([0;p]) % p should be top padded with a 0
row = real(c);
col = imag(c);
k = [row col];

%offset positions so all positions become positive
pos = k - min(k) +1;
%create a binary array that is 1 for walls and 0 elsewhere
bin = ~~accumarray(pos,1);

        *******   
              *   
              *   
              *   
              *   
         *********
  ****   *    *  *
  *  *   *    *  *
  *  *   *    *  *
  *  *   ******  *
  *  *           *
  *  *           *
******           *
  *              *
  *              *
  ****************

%use flood fill to fill holes
filled = bwfill(bin, 'holes');

        *******   
              *   
              *   
              *   
              *   
         *********
  ****   ******  *
  ****   ******  *
  ****   ******  *
  ****   ******  *
  ****           *
  ****           *
******           *
  *              *
  *              *
  ****************

%count number of ones in the filled image 
result = nnz(filled) 

2

Haskell, 579 530 bytes

y=length
i=filter
u i e l=take i l++[e]++drop(i+1)l
k v(r,c)g=u r(u c v(g!!r))g
b(r,c)g=g!!r!!c
w(r,c)s g=case s of{""->j;'<':t->w(r,c-1)t j;'>':t->w(r,c+1)t j;'v':t->w(r+1,c)t j;'^':t->w(r-1,c)t j}where j=k 2(r,c)g
e[]v g=g;e(x:d)v g|elem x v||b x g/=1=e d v g|b x g==1=e(d++(i(\x->notElem x v)$i(\(r,c)->r>=0&&c>=0&&r<y g&&c<y(g!!0))$a x))(x:v)(k 0 x g)
a(r,c)=[(r+1,c+1),(r+1,c),(r+1,c-1),(r,c+1),(r,c-1),(r-1,c+1),(r-1,c),(r-1,c-1)]
m s=(y.i(/=0).concat.e[(0,0)][])(w(l+1,l+1)s(map(\_->map(\_->1)q)q))where l=y s;q=[0..2*l+2]

mé a função principal, que assume uma string v^<>e retorna o número inteiro apropriado.

Ungolfed:

import Data.Set hiding (map, filter)

-- Generate a grid full of ones, of width and height 2x+1. We pass the length of
-- the input, and get back a grid that we could never go out of bounds from,
-- even when the input is a straight wall in any direction.
genGrid :: Int  -> [[Int]]
genGrid x = map (\_->map(\_->1) [0..2*x+2]) [0..2*x+2]

-- Update the value of a list l, such that index i now contains the value e
update :: Int -> a -> [a] -> [a]
update i e l = take i l ++ [e] ++ drop (i+1) l

-- scale update to two dimensions
set :: a -> (Int, Int) -> [[a]] -> [[a]]
set val (r,c) g = update r (update c val (g !! r)) g

-- index into a 2D array
at :: (Int, Int) -> [[a]] -> a
at (r,c) g = g !! r !! c

-- Walk the wall path. Replace any 1 we step on with a 2. Start walking from
-- given coordinates, recursively updating the spot we step on as we process
-- the input string.
walk :: (Int, Int) -> String -> [[Int]] -> [[Int]]
walk (r,c) s g = case s of
    "" -> set 2 (r,c) g
    '<':t -> walk (r,c-1) t (set 2 (r,c) g)
    '>':t -> walk (r,c+1) t (set 2 (r,c) g)
    'v':t -> walk (r+1,c) t (set 2 (r,c) g)
    '^':t -> walk (r-1,c) t (set 2 (r,c) g)

-- Given an input string, generate a grid of appropriate size and walk out the
-- wall path starting at the center.
sketch :: String -> [[Int]]
sketch s = let l = length s in walk (l+1,l+1) s (genGrid l)

-- Breadth-first exploration of the 2D grid, but do not pass through walls.
-- Will touch everything that's not part of the land, and mark it as not part
-- of the land. We use a set (a list in the golfed version) to keep track
-- of which coordinates we've already explored.
explore :: [(Int, Int)] -> Set (Int, Int) -> [[Int]] -> [[Int]]
explore [] v g = g
explore (x:cs) v g
    | member x v  = explore cs v g
    | at x g == 2 = explore cs v g
    | at x g == 0 = explore cs v g
    | at x g == 1 =
        explore (cs ++ (filter (\x-> notMember x v) $ filtBound g $ adj x))
            (insert x v) (set 0 x g)

-- Count everything marked as land to get the final total
countLand :: [[Int]] -> Int
countLand = length . filter (/=0) . concat

-- for a given list of coordinates and a 2D grid, filter those coordinates that
-- are within the grid's bounds
filtBound :: [[Int]] -> [(Int, Int)] -> [(Int, Int)]
filtBound g = filter (\(r,c) -> r >= 0 && c >= 0 && r < length g && c < length (g !! 0))

-- Given a coordinate, get all the adjacent coordinates, including diagonally
-- adjacent coordinates.
adj :: (Int, Int) -> [(Int, Int)]
adj (r,c) = [(r+1,c+1),(r+1,c),(r+1,c-1),(r,c+1),(r,c-1),(r-1,c+1),(r-1,c),(r-1,c-1)]

-- The main function
runMain :: String -> Int
runMain = countLand . explore [(0,0)] empty . sketch

-- Print a grid (for debugging & REPL convenience)
printG :: [[Int]] -> String
printG = concat . map ('\n':) . map show

2

Mathematica, 124 bytes

Você provavelmente não ficará surpreso ao saber que o Mathematica possui uma função integrada para medir a área circundada por uma parede. Infelizmente, é muito bytey: ComponentMeasurements[..., "FilledCount", CornerNeighbors -> False].

Com isso em mente, aqui está a minha resposta completa. É uma função que recebe uma lista de 1, i, -1 ou -i:

1/.ComponentMeasurements[SparseArray[{Re@#,Im@#}&/@FoldList[#+#2&,2(1+I)Length@#,#]->1],"FilledCount",CornerNeighbors->1<0]&

Explicação:

  • FoldList[#+#2&,2(1+I)Length@#,#]constrói a parede iniciando na coordenada 2 (1 + i) (comprimento da parede) e adicionando sucessivamente os elementos da lista de entrada. (Temos que começar na coordenada ridiculamente grande 2 (1 + i) (comprimento da parede) para garantir que as coordenadas da parede permaneçam positivas, caso contrário as coisas quebram.)
  • SparseArray[{Re@#,Im@#}&/@...->1] transforma essas coordenadas de números complexos em pares de números inteiros e cria uma matriz com 1s onde a parede está e 0s em outro lugar.
  • 1/.ComponentMeasurements[...,"FilledCount",CornerNeighbors->1<0]& usa magia interna do Mathematica para medir a área cercada pela parede.

"Temos que começar na coordenada ridiculamente grande ..." bom truque!
rahnema1

1

PHP> = 5.6.2, 888 bytes

Versão Online

<?$h=$v=0;
s($v,$h,S);
for($z=0;$z<strlen($i=$_GET[0]);){
2<($b=$i[$z++])?$h--:($b>1?$v++:($b?$h++:$v--));
$e=max($h,$e);
$w=min($h,$w);
$n=min($v,$n);
$s=max($v,$s);
s($v,$h,X);}
$f=($e-$w+1)*($s-$n+1);
ksort($a);
function i($v,$h){global$a;return isset($a[$v][$h])&&$a[$v][$h]==" ";}
function s($v,$h,$l=" "){global$a;$a[$v][$h]=$l;}
function c($v,$h,$n=1){global$a;
foreach($r=range(-1,$n)as$i)
foreach($r as$j)
if(($i+$j)&&i($v+$i,$h+$j)){if($n)s($v,$h);return 1;}return;}
foreach($a as$v=>$z){
foreach(range($w,$e)as$h){
if(!isset($a[$v][$h])){
if(($v==$s)||($v==$n)||($h==$e)||($h==$w)||c($v,$h,0))s($v,$h);
else$c[]=[$v,$h];}
}ksort($a[$v]);}
while($z){$z=0;
foreach($c as$b=>$w){if(c(...$w)){$z++;unset($c[$b]);}}};
foreach($c as$b=>$w)$a[$w[0]][$w{1}]=O;
foreach($a as $k=>$v){ksort($a[$k]);$g.=join($a[$k])."\n";}echo $g;
echo $f-substr_count($g," ");

Você sabe que você só teve que produzir o tamanho da terra, não a própria terra, certo? :)
Stewie Griffin

@StewieGriffin, essa é a saída echoo $ g; `da terra. Encontrei essa abordagem e talvez exista outra maneira mais curta. Se eu encontrasse desta forma posso melhorar este post
Jörg Hülsermann
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.