你的土地有多大?


23

在这项挑战中,您将计算出自己的土地面积。


给定已建好的墙,编写一个程序或函数来计算土地的大小。您将获得一个非空的输入字符串,其中包含一组由您选择的4个不同的字符组成,它们代表“上”,“下”,“左”和“右”四个方向(我将^ v < >在此挑战中使用)。不可能转180度(<>^v),但您可能会越过墙壁。

您“捕获”土地的方式是用墙壁将其包围。隔离墙本身也被视为您土地的一部分。一些示例将使其更加清晰。我将使用o被墙壁环绕的土地,x墙壁本身以及墙壁S的起点,仅用于说明如何建造墙壁。输出应该是你的土地的总规模(数量ox并且S在测试情况下)。

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"

说明:

  • 您无需绘制墙,输出仅应为整数
  • 输入格式是可选的。您可以使用<>^v,,数字列表、、(1, -1, i, -i)字符列表等字符串ABCD

这是因此每种语言中最短的代码将获胜。请记住,即使使用“常规”语言,解释也很重要!


1
您应该更改描述,以便它计算出您封闭的三叶草数量:P
fəˈnɛtɪk



@MatthewRoh,
Stewie Griffin

@Stewie哦,是的,这也有关
Matthew Roh

Answers:


6

Python 2中385个 345 332字节

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))

在线尝试!尝试所有测试用例

输入为数字,0〜3,此处符号的0索引: >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))

这是结果矩阵:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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函数将包含以下内容的列向量作为输入 (1, -1, i, -i)

使用@ lanlock4的Mathematica回答的方法将输入的长度添加到坐标以避免非正坐标,而不是从坐标中减去最小值。保存了4个字节。

先前的答案:

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

在Octave Online上尝试!

进行了更改,以实现更好的可视化。

说明:

%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字节

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是主要函数,它在上接受一个字符串v^<>,并返回适当的整数。

取消高尔夫:

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个字节

了解Mathematica具有用于测量墙围的面积的内置功能,您可能不会感到惊讶。不幸的是,它非常笨拙:ComponentMeasurements[..., "FilledCount", CornerNeighbors -> False]

考虑到这一点,这是我的完整答案。该函数采用1,i,-1或-i的列表:

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

说明:

  • FoldList[#+#2&,2(1+I)Length@#,#]通过从坐标2(1 + i)(墙的长度)开始并连续添加输入列表的元素来构建墙。(我们必须从荒谬的大坐标2(1 + i)(墙的长度)开始,以确保墙的坐标保持正值,否则会损坏。)
  • SparseArray[{Re@#,Im@#}&/@...->1] 将这些坐标从复数转换为整数对,并创建一个壁为1的数组,其他位置为0。
  • 1/.ComponentMeasurements[...,"FilledCount",CornerNeighbors->1<0]& 使用内置的Mathematica魔术来测量墙包围的区域。

“我们必须从荒谬的大坐标开始……”好招!
rahnema17年

1

PHP> = 5.6.2,888字节

在线版本

<?$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," ");

您知道您只需要输出土地的大小,而不是土地本身吧?:)
Stewie Griffin

@StewieGriffin这是土地的输出“ echo $ g;”。我发现了这种方法,也许它存在另一种较短的方法。如果我发现这个方法,我可以改善这个职位
约尔格Hülsermann
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.