ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: maze

maze

์•„์ด์Šค ๊ณจํ”„ ์ฑŒ๋ฆฐ์ง€ ๋ฐฐ์—ด, 2 ์ฐจ์› ๋ฌธ์ž

์ด ๋„์ „์˜ ๋ชฉํ‘œ ๋Š” ์ฃผ์–ด์ง„ ๊ณผ์ •์„ ์™„๋ฃŒํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ์˜ ํŒŒ์—…์„ ๋ฐ˜ํ™˜ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‚˜ ๊ธฐ๋Šฅ ์„ ์ž‘์„ฑ ํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

์ž…๋ ฅ

  • ์ฝ”์Šค์˜ ๋ ˆ์ด์•„์›ƒ์€ ์›ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ฝ˜์†”์—์„œ ์ฝ๊ธฐ, ์ž…๋ ฅ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ, ํŒŒ์ผ ๋˜๋Š” ๊ธฐํƒ€ ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด, ๋ฌธ์ž์—ด ๋ฐฐ์—ด, 2 ์ฐจ์› ๋ฌธ์ž / ๋ฐ”์ดํŠธ ๋ฐฐ์—ด์—์„œ ์ฝ์Œ)
  • ๊ณต์˜ ์‹œ์ž‘ ์œ„์น˜์™€ ๊ตฌ๋ฉ๋„ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ž…๋ ฅ์—์„œ ํŒŒ์‹ฑ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์‚ฌ๋ก€์—์„œ๋Š” ์‹ค์ œ ์œ„์น˜์™€ ํ˜ผ๋™๋˜์ง€ ์•Š๋„๋ก ์ฝ”์Šค์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ž…๋ ฅ ๋ฌธ์ž๊ฐ€ ์—ฌ์ „ํžˆ ๊ณ ์œ  ๋ฌธ์ž (์˜ˆ : ์ธ์‡„ ๊ฐ€๋Šฅํ•œ ASCII ๋ฌธ์ž)๋กœ ์ธ์‹๋˜๋Š” ํ•œ ์ž…๋ ฅ ๋ฌธ์ž๋ฅผ ๋‹ค๋ฅธ ๊ฒƒ์œผ๋กœ ๋‹ค์‹œ ๋งตํ•‘ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‚ฐ์ถœ

  • ํ”„๋กœ๊ทธ๋žจ์€ ํ˜„๋ช…ํ•œ ํ˜•์‹ (๊ฒฐ๊ณผ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ฌธ์ž์—ด, ์ •์ˆ˜, ๋ถ€๋™ ๋˜๋Š” ํ•˜์ด์ฟ )์œผ๋กœ ์ž…๋ ฅ์œผ๋กœ ์ „๋‹ฌ ๋œ ์ฝ”์Šค์— ๋Œ€ํ•ด ์ตœ์ € ์ ์ˆ˜ (ํ™€์— ๋„๋‹ฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ์ŠคํŠธ๋ผ์ดํฌ)๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ•์ขŒ๋ฅผ ์ด๊ธธ ์ˆ˜์—†๋Š” ๊ฒฝ์šฐ ๊ท€๊ตญ -1(๋˜๋Š” ์„ ํƒ ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ๊ฐ•๋ฐ• ํ•œ ๊ฐ€์น˜๋Š” ์ด๊ธธ ์ˆ˜์—†๋Š” ๊ฐ•์ขŒ์— ๋Œ€ํ•ด์„œ๋Š” ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์Œ).

์˜ˆ:

์ด ์˜ˆ์ œ์—์„œ ์œ„์น˜๋Š” 0 ๊ธฐ๋ฐ˜, X / Y, ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ, ์œ„์—์„œ ์•„๋ž˜๋กœ ํ‘œ๊ธฐ๋˜์ง€๋งŒ ๊ฒฐ๊ณผ๋Š” ํ˜•์‹์— ๋…๋ฆฝ์ ์ด๋ฏ€๋กœ ์›ํ•˜๋Š” ํ˜•์‹์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…๋ ฅ:

###########
#     ....#
#      ...#
#  ~    . #
# ~~~   . #
# ~~~~    #
# ~~~~    #
# ~~~~  o #
# ~~~~    #
#@~~~~    #
###########

Ball (Start-Position): 1/9
Hole (End-Position):   8/7

์‚ฐ์ถœ:

8

์ฝ”์Šค ์˜ˆ

๊ทœ์น™๊ณผ ํ•„๋“œ

์ด ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•„๋“œ๋กœ ๊ตฌ์„ฑ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • '@' ๊ณต -์ฝ”์Šค ์‹œ์ž‘
  • 'o' ๊ตฌ๋ฉ -์ฝ”์Šค์˜ ๋ชฉํ‘œ
  • '#' ๋ฒฝ -๋ฒฝ์— ๋‹ฟ์œผ๋ฉด ๊ณต์ด ๋ฉˆ ์ถฅ๋‹ˆ ๋‹ค
  • '~' ๋ฌผ -ํ”ผํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค
  • '.' ๋ชจ๋ž˜ -๊ณต์ด ๋ชจ๋ž˜ ์œ„์—์„œ ์ฆ‰์‹œ ๋ฉˆ ์ถฅ๋‹ˆ ๋‹ค
  • ' ' ์–ผ์Œ -๊ณต์ด ๋ฌด์–ธ๊ฐ€์— ๋‹ฟ์„ ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋ฏธ๋„๋Ÿฌ์ง‘๋‹ˆ๋‹ค.

๊ฒŒ์ž„์˜ ๊ธฐ๋ณธ ๊ทœ์น™ ๋ฐ ์ œํ•œ ์‚ฌํ•ญ :

  • ๊ณต์€ ๋Œ€๊ฐ์„ ์œผ๋กœ, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ์œ„์•„๋ž˜๋กœ ์›€์ง์ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • ๊ณต์€ ๋ฌผ ์•ž์—์„œ, ๋ฒฝ ์•ž์—์„œ, ๋ชจ๋ž˜ ์œ„์—์„œ ๊ทธ๋ฆฌ๊ณ  ๊ตฌ๋ฉ์—์„œ ๋ฉˆ์ถ”์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ๋ฌผ ์†์œผ๋กœ์˜ ์ƒท์ด ์œ ํšจํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
    • ๊ณต์€ ์–ผ์Œ ์œ„์—์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ฑด๋„ˆ ๋›ฐ์ง€ ์•Š๊ณ  ๊ตฌ๋ฉ์— ๋‚จ์•„์žˆ๊ฒŒ๋ฉ๋‹ˆ๋‹ค.
  • ์ฝ”์Šค๋Š” ํ•ญ์ƒ ์ง์‚ฌ๊ฐํ˜•์ž…๋‹ˆ๋‹ค.
  • ์ด ์ฝ”์Šค๋Š” ํ•ญ์ƒ ๋ฌผ์ด๋‚˜ ๋ฒฝ์œผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค (๊ฒฝ๊ณ„ ํ™•์ธ ํ•„์š” ์—†์Œ).
  • ํ•ญ์ƒ ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๊ณต๊ณผ ํ•˜๋‚˜์˜ ๊ตฌ๋ฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์ฝ”์Šค๊ฐ€ ์ด๊ธธ ์ˆ˜์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค.
  • ๋™์ผํ•œ (๊ฐ€์žฅ ๋‚ฎ์€) ์ ์ˆ˜๋ฅผ ์–ป๋Š” ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ๊ฐ€์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ—ˆ์ ๊ณผ ์Šน๋ฆฌ ์กฐ๊ฑด

  • ํ‘œ์ค€ ํ—ˆ์  ์€ ๊ธˆ์ง€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค
  • ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒ๋˜์–ด์•ผํ•ฉ๋‹ˆ๋‹ค
  • ์ถ”๊ฐ€ ๊ทœ์น™์„ ๊ตฌ์„ฑ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค (๊ณต์„ ๋„ˆ๋ฌด ์„ธ๊ฒŒ ์น˜๋ฉด ๋ฌผ ์œ„๋กœ ๊ฑด๋„ˆ ๋›ฐ๊ฑฐ๋‚˜ ๋ฒฝ์—์„œ ํŠ€์–ด ๋‚˜์˜ค๊ฑฐ๋‚˜ ๋ชจ๋ž˜๋ฐญ ์œ„๋กœ ๋›ฐ์–ด ์˜ค๋ฆ„, ๋ชจ์„œ๋ฆฌ ์ฃผ์œ„์˜ ๊ณก์„  ๋“ฑ)
  • ์ด๊ฒƒ์€ ์ด๋ฏ€๋กœ ๋ฌธ์ž ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ ์€ ์†”๋ฃจ์…˜์ด ์Šน๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ์†”๋ฃจ์…˜์€ ์ œ๊ณต๋œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์‚ฌ๋ก€๋ฅผ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ โ€‹โ€‹์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ๋œ ์–ธ์–ด์˜ ์ œํ•œ์œผ๋กœ ์ธํ•ด ์ด๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ๋‹ต๋ณ€์—์ด๋ฅผ ์ง€์ •ํ•˜์‹ญ์‹œ์˜ค.

ํ…Œ์ŠคํŠธ ์‚ฌ๋ก€

์ฝ”์Šค # 1 (2 ํŒŒ์—…)

####
# @#
#o~#
####

์ฝ”์Šค # 2 (๋ถˆ๊ฐ€๋Šฅ)

#####
#@  #
# o #
#   #
#####

์ฝ”์Šค # 3 (3 ํšŒ)

~~~
~@~
~.~
~ ~
~ ~
~ ~
~ ~
~.~
~o~
~~~

์ฝ”์Šค # 4 (2 ํŒŒ์—…)

#########
#~~~~~~~#
#~~~@~~~#
##  .  ##
#~ ~ ~ ~#
#~. o .~#
#~~~ ~~~#
#~~~~~~~#
#########

์ฝ”์Šค # 5 (๋ถˆ๊ฐ€๋Šฅ)

~~~~~~~
~...  ~
~.@.~.~
~...  ~
~ ~ ~.~
~ . .o~
~~~~~~~

๋” ๋งŽ์€ ํ…Œ์ŠคํŠธ ์‚ฌ๋ก€ :

https://pastebin.com/Azdyym00



๋‹ต๋ณ€

์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ (ES6), 174 ๋ฐ”์ดํŠธ

curling currying syntax ์—์„œ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค ([x, y])(a). ์—ฌ๊ธฐ์„œ x ์™€ y ๋Š” ์‹œ์ž‘ ์œ„์น˜์˜ 0- ์ธ๋ฑ์Šค ์ขŒํ‘œ์ด๊ณ  a [] ๋Š” 0= ice, 1= wall, 2= sand, 3= hole ๋ฐ 4= water๋ฅผ ๊ฐ–๋Š” ์ •์ˆ˜์˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค.

0์†”๋ฃจ์…˜์ด์—†๋Š” ๊ฒฝ์šฐ ๋ฐ˜ํ™˜ ํ•ฉ๋‹ˆ๋‹ค.

p=>a=>(r=F=([x,y],n,R=a[y],c=R[x])=>R[c&(R[x]=4)|n>=r||[-1,0,1,2].map(d=>(g=_=>(k=a[v=Y,Y+=d%2][h=X,X+=~-d%2])||g())(X=x,Y=y)>3?0:k>2?r=-~n:F(k>1?[X,Y]:[h,v],-~n)),x]=c)(p)|r

์˜จ๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด์‹ญ์‹œ์˜ค!

๋Œ“๊ธ€

p => a => (                       // given the starting position p[] and the matrix a[]
  r =                             // r = best result, initialized to a non-numeric value
  F = (                           // F = recursive function taking:
    [x, y],                       //   (x, y) = current position
    n,                            //   n = number of shots, initially undefined
    R = a[y],                     //   R = current row in the matrix
    c = R[x]                      //   c = value of the current cell
  ) =>                            //
    R[                            // this will update R[x] once the inner code is executed
      c & (R[x] = 4) |            //   set the current cell to 4 (water); abort if it was
      n >= r ||                   //   already set to 4 or n is greater than or equal to r
      [-1, 0, 1, 2].map(d =>      //   otherwise, for each direction d:
        (g = _ => (               //     g = recursive function performing the shot by
          k = a[                  //         saving a backup (h, v) of (X, Y)
            v = Y, Y += d % 2][   //         and updating (X, Y) until we reach a cell
            h = X, X += ~-d % 2]) //         whose value k is not 0 (ice)
          || g()                  //   
        )(X = x, Y = y)           //     initial call to g() with (X, Y) = (x, y)
        > 3 ?                     //     if k = 4 (water -> fail):
          0                       //       abort immediately
        :                         //     else:
          k > 2 ?                 //       if k = 3 (hole -> success):
            r = -~n               //         set r to n + 1
          :                       //       else:
            F(                    //         do a recursive call to F():
              k > 1 ?             //           if k = 2 (sand):
                [X, Y]            //             start the next shots from the last cell
              :                   //           else (wall):
                [h, v],           //             start from the last ice cell
              -~n                 //           increment the number of shots
            )                     //         end of recursive call
      ), x                        //   end of map(); x = actual index used to access R[]
    ] = c                         // restore the value of the current cell to c
)(p) | r                          // initial call to F() at the starting position; return r

๋‹ต๋ณ€

ํŒŒ์ด์ฌ 3 , 273 ๋ฐ”์ดํŠธ

def p(g,c,d,k=0):
	while 1>k:c+=d;k=g.get(c,9)
	return-(k==2)or c-d*(k==3)
def f(g):
	c={q for q in g if g.get(q,9)>4};I=0;s=[c]
	while all(g.get(q,9)-4for q in c):
		c={k for k in{p(g,k,1j**q)for k in c for q in range(4)}if-~k}
		if c in s:return-1
		s+=[c];I+=1
	return I

์˜จ๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด์‹ญ์‹œ์˜ค!

ovs ๋•๋ถ„์— -41 ๋ฐ”์ดํŠธ-Jonathan
Frech ๋•๋ถ„์— -1 ๋ฐ”์ดํŠธ


๋‹ต๋ณ€

C #, 461 418 ๋ฐ”์ดํŠธ

์ด๊ฒƒ์€์ด ๋„์ „์„ (ํฌ๋ง์ ์œผ๋กœ) ๋ถ€ํ™œ์‹œํ‚ค๊ธฐ์œ„ํ•œ ๋น„ ๊ฒฝ์Ÿ์  ์ฐธ์กฐ ๊ตฌํ˜„์ž…๋‹ˆ๋‹ค.

์ผ€๋นˆ ํฌ๋ฃจ์ด ์„ผ ๊ณจํ”„

int P(string[]C){int w=C[0].Length,i=0,l=c.Length;var c=string.Join("",C);var h=new int[l];for(var n=new List<int>();i<l;n.Add(i++))h[i]=c[i]!='@'?int.MaxValue:0;for(i=1;;i++){var t=n;n=new List<int>();foreach(int x in t){foreach(int d in new[]{-1,1,-w,w}){for(int j=x+d;c[j]==' ';j+=d);if(c[j]=='#'&h[j-d]>s){h[j-d]=s;n.Add(j-d);}if(c[j]=='.'&h[j]>s){h[j]=s;n.Add(j);}if(c[j]=='o')return s;}}if(n.Count<1)return -1;}}

์–ธ ๊ณจํ”„

int IceGolf(string[] course)
{
    // Width of the course
    int w = course[0].Length;

    // Course as single string
    var c = string.Join("", course);

    // Array of hits per field
    var hits = new int[c.Length];

    // Fields to continue from
    var nextRound = new List<int>();

    // Initialize hits
    for (int i = 0; i < hits.Length; i++)
    {
        if (c[i] != '@')
            // All fields start with a high value
            hits[i] = Int32.MaxValue;
        else
        {
            // Puck field starts with 0
            hits[i] = 0;
            nextRound.Add(i);
        }
    }

    for (int s = 1; ; s++)
    {
        // clear the fields that will be used in the next iteration
        var thisRound = nextRound;
        nextRound = new List<int>();

        foreach (int i in thisRound)
        {
            // test all 4 directions
            foreach (int d in new[] { -1, 1, -w, w })
            {
                int j = i+d;

                // ICE - slide along
                while (c[j] == ' ')
                    j += d;

                // WALL - stop on previous field
                if (c[j] == '#' && hits[j-d] > s)
                {
                    hits[j-d] = s;
                    nextRound.Add(j-d);
                }

                // SAND - stop
                if (c[j] == '.' && hits[j] > s)
                {
                    hits[j] = s;
                    nextRound.Add(j);
                }

                // HOLE return strikes
                if (c[j] == 'o')
                    return s;
            }
        }

        // No possible path found
        if (nextRound.Count == 0)
            return -1;
    }
}

์˜จ๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•ด๋ณด์‹ญ์‹œ์˜ค


๋‹ต๋ณ€