地形可达性


12

基于回合制的战术游戏,例如“战前战争”,“战地风云”和“火焰纹章”,是由不同地形的方格组成,具有不同运动类别的单位,每种地形类型需要不同的成本。我们将调查该问题的一部分。

挑战

您的任务是在给定地形成本和移动速度的网格的情况下,确定一个位置是否可以与另一个位置到达。

单位只能正交移动,其中移动到一个正方形上的成本是网格上相应单元格的值(移动是免费的)。例如,从一个值为3的单元格移动到一个值为1的单元格需要1次移动,而从另一个方向移动则需要3个运动。某些正方形可能无法访问。

1 [1] 1  1  1
1  2  2  3  1
2  3  3  3  4
1  3 <1> 3  4

[1]移到<1>至少需要7个移动点,方法是向右移动一个方块,然后向下移动三个方块。因此,如果给定6或小于6的移动速度,则应输出虚假答案。

示例测试用例

这些将使用左上角零索引(行,列)坐标,而不是用括号括起来的单元格作为开始和结束位置,以使解析更加容易。无法访问的单元格将用X

案例1a

1 1 2 1 X
1 2 2 1 1
2 1 1 2 1
X X X 1 2
Speed: 5
From (2, 3) to (0, 1)

Output: True

案例1b

1 1 2 1 X
1 2 2 1 1
2 1 1 2 1
X X X 1 2
Speed: 4
From (2, 3) to (0, 1)

Output: False

案例1c

1 1 2 1 X
1 2 2 1 1
2 1 1 2 1
X X X 1 2
Speed: 5
From (0, 1) to (2, 3)

Output: False

案例2a

3 6 1 1 X 4 1 2 1 X
5 1 2 2 1 1 1 X 1 5
2 1 1 1 2 1 1 1 X 1
2 1 1 3 1 2 3 4 1 2
1 1 2 1 1 4 1 1 1 2
3 2 3 5 6 1 1 X 1 4
Speed: 7
From (3, 4) to (2, 1)

Output: True

案例2b

3 6 1 1 X 4 1 2 1 X
5 1 2 2 1 1 1 X 1 5
2 1 1 1 2 1 1 1 X 1
2 1 1 3 1 2 3 4 1 2
1 1 2 1 1 4 1 1 1 2
3 2 3 5 6 1 1 X 1 4
Speed: 4
From (3, 4) to (2, 1)

Output: False

案例2c

3 6 1 1 X 4 1 2 1 X
5 1 2 2 1 1 1 X 1 5
2 1 1 1 2 1 1 1 X 1
2 1 1 3 1 2 3 4 1 2
1 1 2 1 1 4 1 1 1 2
3 2 3 5 6 1 1 X 1 4
Speed: 7
From (1, 8) to (2, 7)

Output: True

案例3a

2 1 1 2
2 3 3 1
Speed: 3
From (0, 0) to (1, 1)

Output: False

案例3b

2 1 1 2
2 3 3 1
Speed: 3
From (1, 1) to (0, 0)

Output: True

规则,假设和注释

  • 禁止标准漏洞,I / O可以采用任何方便的格式
  • 您可以假设坐标全部在网格上
  • 移动速度永远不会超过100
  • 无法访问的单元格可能用非常大的数字(例如420、9001、100万)或0或null表示,以您最方便的为准。
  • 所有输入将由正整数组成(除非使用null或0表示无法访问的单元格)

1
@LuisfelipeDejesusMunoz“这些将使用左上角零索引(行,列)坐标”
Beefster

您说I / O可以采用任何方便的格式。例如,这是否包括具有维度的列表/数组?我相信通常是允许的,但是与解析字符串相比,它肯定会节省很多字节。
dfeuer

@dfeuer,当然可以
Beefster

我在手机仿真器上下载了高级战争软件... 我很难过,它迫使您执行13个教程级别...我想非常糟糕地重播它,但是我的耐心是薄薄的,很难在旧系统上使用教程。
魔术章鱼缸

Answers:


2

TSQL查询,205个 191字节

输入是表变量@t

@ x =开始xpos,@ y =开始ypos @ i =结束xpos,@ j =结束ypos @ =速度

DECLARE @t table(x int,y int,v int)
INSERT @t
values
(0,0,1),(0,1,1),(0,2,2),(0,3,1),(0,4,null),
(1,0,1),(1,1,2),(1,2,2),(1,3,1),(1,4,1),
(2,0,2),(2,1,1),(2,2,1),(2,3,2),(2,4,1),
(3,0,null),(2,1,null),(2,2,null),(2,3,1),(2,4,2)

DECLARE @x INT=2,@y INT=3,@i INT=0,@j INT=1,@ INT=5;

WITH C as(SELECT @y f,@x r,@ s
UNION ALL
SELECT f+a,r+b,s-v FROM C
JOIN(values(1,0),(0,1),(-1,0),(0,-1))x(a,b)ON
s>0JOIN @t
ON f+a=x and r+b=y)SELECT
max(iif(S>=0and f=@j and r=@i,1,0))FROM c

在线试用非高尔夫版本


0

Python 2,220字节

def f(m,a,w,h,r,c,R,C):
 T=[w*[999]for _ in' '*h];T[r][c]=0;P=[(r,c)];j,k=1,0
 for u,v in P:exec"U,V=u+j,v+k;j,k=-k,j\nif h>U>-1<V<w:q=T[U][V];T[U][V]=min(T[u][v]+m[U][V],q);P+=[(U,V)]*(q>T[U][V])\n"*4
 return a>=T[R][C]

在线尝试!

接受一个m整数数组,其'X'值大于100;一个速度am其宽度w和高度h;然后返回,我们可以从零索引的行/列单元格开始,(r,c)然后到达最后一个单元格(R,C)

该算法是一种改进的洪水填充。略有偏离的代码:

def f(m,a,w,h,r,c,R,C):
 T = [w*[999]for _ in ' '*h] # make an array same size as m, with all 
                             #   values 999, whose values will represent
                             #   the cost of getting to each cell.
 T[r][c] = 0                 # set the starting location to a cost of 0
 P = [(r,c)]                 # initialize a set of cells whose neighbors'
                             #   cost might need to be be updated
 j,k = 1,0                   # and also j,k which will take on values:
                             #  (1,0), (0,1), (-1,0), (0,1), used to 
                             #  probe orthogonal neighbors
 for u,v in P:               # scan the cells in P
    for _ in '1234':         # look at each of 4 orthogonal positions
        U,V = u+j,v+k        # U and V get the indexes of a neighbor 
                             #   of the current cell.
        j,k = -k,j           # this 'rotates' the j,k pairing.
        if h>U>-1<V<w:       # if the coordinates are in bounds...
            q = T[U][V]      # save the current 'cost' of getting to cell (U,V)
                             # see if we can reduce that cost, which is calculated 
                             #   by taking the cost of the currently scanned cell 
                             #   + the value from m for the neighbor cell. 
            T[U][V] = min(T[u][v]+m[U][V] ,q)
                             # if we can reduce the cost, add the neighbor
                             #   to P because **it's** neighbors might,
                             #   in turn, need updating.
            P += [(U,V)]*(q>T[U][V])
 return a>=T[R][C]           # return if speed is enough for the cost.

0

的JavaScript(ES7), 116个  113字节

(matrix)([endRow, endCol])(speed, startRow, startCol)01

m=>e=>o=g=(s,y,x)=>m.map((r,Y)=>r.map((v,X)=>r[s<v|(x-X)**2+(y-Y)**2-1||g(s-v,Y,X,r[X]=1/0),X]=v),o|=y+[,x]==e)|o

在线尝试!

已评论

m =>                        // m[] = matrix
e =>                        // e[] = target coordinates
  o =                       // o   = success flag, initialized to a non-numeric value
  g = (                     // g   = recursive depth-first search function taking:
    s,                      //   s    = speed
    y, x                    //   y, x = starting coordinates
  ) =>                      //
    m.map((r, Y) =>         // for each row r[] at position Y in m[]:
      r.map((v, X) =>       //   for each value v at position X in r[]:
        r[                  //     this statement ultimately updates r[X]:
          s < v |           //       abort if s is less than v
          (x - X) ** 2 +    //       or the quadrance between (x, y)
          (y - Y) ** 2 - 1  //       and (X, Y) is not equal to 1
          || g(             //       otherwise, do a recursive call to g:
               s - v,       //         subtract v from s
               Y, X,        //         pass (Y, X) as the new coordinates
               r[X] = 1 / 0 //         temporarily make this cell unreachable
             ),             //       end of recursive call 
          X                 //       restore r[X] ...
        ] = v               //     ... to its original value
      ),                    //   end of inner map()
      o |= y + [, x] == e   //   set the flag o if (y + ',' + x) matches (e + '')
    ) | o                   // end of outer map(); return o

0

果冻,59个字节

+2¦µ_2ịæ.ؽœị$Ʋ+5ịƲ$4¦01Ñḣ3Ḋ⁼/Ɗ?ḣ2=/ẸƊoF<0ẸƊƊ?
çⱮØ.,U$;N$¤Ẹ

在线尝试!

不太快 尝试所有路径,直到速度单位用尽,甚至重新追踪其速度。但是,这避免了检查是否已访问空间的需要。输入提供为[nrows, ncols],[start_row, start_col],[end_row, end_col],speed,flattened matrix column-major

说明

助手链接

+2¦                                       | add the right argument to the second item in the left argument (current location)
   µ                                      | start a new monadic chain with the modified left argument
                    4¦                    | for the fourth item (speed)...
    _                                     |   subtract...
                 ịƲ$                      |     the item located at...
     2ịæ.ؽœị$Ʋ                           |       the dot product of the current position and (number of columns,
                                          |       right-padded with 1)
               +5                         |       plus five
                                        ? | Now, if...
                                       Ɗ  |   next three as a monad
                           ḣ2=/ẸƊ         |   either current row or current column are equal to nrows/ncolumns respectively
                                 o        | or
                                  F<0ẸƊ   |   any value is negative
                 0                        | return zero
                          ?               | else if...
                   ḣ3Ḋ⁼/Ɗ                 | the second and third items (current and end pos) are equal
                  1                       | return 1
                   Ñ                      | else pass the current list back to the main link

主链接

ç             | call the helper link with the current list...
 Ɱ            |   and each of
  Ø.,U$;N$¤   |   [0,1],[1,0],[0,-1],[-1,0]
           Ẹ  | Check if any are true

0

果冻,38个字节

ạƝṢ€Ḅ’¬Ạ
ŒṪ’ḟŒPŒ!€ẎW€j¥@€ÇƇḊ€‘œị⁸§Ṃ’<⁵

一个效率极低的完整程序,它接受地形(看不见的区域为101),然后是起点和终点,然后是速度。

在线尝试!(尝试大多数测试用例没有多大意义!)

怎么样?

创建“除起点和终点之外的所有地形位置”的每个幂集的所有置换的列表,将每个起点和终点包围,对仅进行距离为1的正交移动的那些进行过滤,丢弃起点从每一个中,索引回地形,将每一个相加,取最小值,减去一个,然后测试这是否小于速度。

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.