Arrow Pointing อยู่ที่ไหน


32

Arrow Pointing อยู่ที่ไหน

ในการท้าทายนี้เป้าหมายของคุณคือตามลูกศรและเอาท์พุทอักขระที่ชี้ไป

ตัวอย่าง

การป้อนข้อมูล:

d  S------+    b
          |     
          |     
   c      +--->a

เอาท์พุท: a


การป้อนข้อมูล:

S-----+---a->c
      |       
      V       
      b       

เอาท์พุท: b

ลูกศรไม่ได้ชี้ไปที่cเพราะถูกหารด้วยเครื่องหมายaหมายความว่าเส้นทางนี้ไม่นำไปสู่หัวลูกศร


การป้อนข้อมูล:

a S      s
  |      |
  V      V
  b      c

เอาท์พุท: b


การป้อนข้อมูล:

d s<+S+--V
    |||  Q
    -++   

เอาท์พุท: Q

เส้นทางนี้เริ่มต้นที่Sจะไปลงไปทางขวาไปขึ้นไปทางขวาแล้วชี้ลงที่ Q. หมายเหตุว่าเส้นทางไม่ได้ไปตรงจากไปS+


การป้อนข้อมูล:

d s-+   +-S  +--+
    +-->b |  |  |
     |  | +--+  |
     +--+ A<----+

เอาท์พุท: A


การป้อนข้อมูล:

S-----+   
|     +-^ 
+---+->B  
    +---^ 

เอาท์พุท: B

เพราะบรรทัดที่ถูกต้องจะไม่นำไปสู่การตัวละครช่องว่าง บรรทัดเดียวที่ไม่นำไปสู่อักขระช่องว่างนำไปสู่ ​​aB

ท้าทาย

อินพุตจะเป็นสตริงหลายบรรทัดที่คุณต้องการค้นหาอักขระที่ลูกศรชี้ไป จะมีลูกศรที่ถูกต้องเพียงหนึ่งลูก ลูกศรที่ถูกต้องจะเพียงSชี้ไปที่ตัวอักษรและตัวเลขไม่รวม บรรทัดจะไม่ทับซ้อนกัน เช่น-|-

  • S (เมืองหลวง) หมายถึงที่ที่ลูกศรเริ่ม
  • - แสดงถึงเส้นแนวนอน
  • +แสดงถึงการเปลี่ยนแปลงที่เป็นไปได้ในแกน +ลูกศรที่ถูกต้องจะไม่เริ่มต้นด้วย
  • | แสดงถึงเส้นแนวตั้ง
  • > < V ^สิ่งเหล่านี้แสดงถึงหัวลูกศร +เหล่านี้จะไม่เชื่อมต่อกับ

จะมีเพียงหนึ่งSในสตริง อินพุตจะถูกเสริมให้เป็นสี่เหลี่ยม (ไม่จำเป็นต้องเป็นสี่เหลี่ยมจัตุรัส)


1
"สิ่งนี้จะไม่ปรากฏขึ้นพร้อมกับ a S" น่าจะได้รับการใส่คำว่า "นี่จะไม่เป็นตัวอักษรแรกของลูกศร" (เนื่องจากQตัวอย่างมีการ +ติดกับS.) " +หมายถึงการเปลี่ยนแปลงในแกน" อาจดีกว่า " +แสดงถึงความเป็นไปได้ในการเปลี่ยนแปลงในแกน" (เนื่องจากBตัวอย่างแสดงให้เห็นว่าคุณสามารถเลื่อนผ่าน+โดยไม่ต้องเปลี่ยนทิศทาง) มิฉะนั้นความท้าทายที่ดี :)
Martin Ender

ฉันได้ทำการเปลี่ยนแปลงหนึ่งในกรณีทดสอบของคุณ ฉันคิดว่ามันจะทำให้การเขียนโปรแกรมที่เกิดขึ้นยากขึ้นเพื่อให้ได้คำตอบที่ถูกต้องเช่นเดียวกับฉันในตอนแรก
El'endia Starman

หัวลูกศรสามารถเปลี่ยนทิศทางได้---^หรือไม่? กล่าวอีกนัยหนึ่งถ้าเป็นในตัวอย่าง B สามารถอยู่ในแถวแรกได้หรือไม่
edc65

@ edc65 คุณหมายถึงชอบในตัวอย่าง Q?
Downgoat

1
ส่วนหัวลูกศรจะไม่เชื่อมต่อกับ '+' แต่มันสามารถเชื่อมต่อกับ 'S' ได้ไหม? คือS>aถูกต้อง?
edc65

คำตอบ:


9

JavaScript (ES6), 195 245 231 242 246 250

Edit4ตอนนี้เป็นฟังก์ชั่นวนซ้ำแบบเดี่ยว อาจไม่สามารถเล่นกอล์ฟได้มากขึ้น

Edit3ทดสอบเส้นตรงและทดสอบการรวมหัวลูกศรในฟังก์ชัน T ฟังก์ชัน S และ H ถูกนำออก

Edit2ปรับปรุงและอีกต่อไป :( หลังจากนี้ชี้แจงนี้

แก้ไขการปรับปรุงเล็กน้อยตัดอักขระที่นี่และที่นั่นรอให้ CJammers ก้าวเข้ามา

ทดสอบการเรียกใช้ตัวอย่างข้อมูลด้านล่างในเบราว์เซอร์ที่สอดคล้องกับ EcmaScript 6 (ใช้ได้กับ Firefox Chrome ยังขาดตัวดำเนินการสเปรด...)

f=(s,p=s[I='indexOf']`S`,w=m=[o=~s[I]`
`,1,-o,-1],q,v,r)=>m.some((d,i)=>{for(v=w,q=p;r=s[q+=d],r=='|-'[i&1];)v='+';return r=r==v?f(s,q,v):/\w/.test(r=s[q+m['^>V<'[I](r)]])&&r},s=[...s],s[p]=0)&&r


// Less golfed
// U: recursive scan function
U = (s, // input string or array
     p = s.indexOf`S`, // current position, defult to position of S into string (at first call)
     w = // char to compare looking for a '+', at first call default to garbage as you can't follow '+' near 'S'
       m = [// m, global, offset array form movements. 
         o = ~s.indexOf`\n`, // o, global, row line +1 (negated)
         1, -o, -1], // complete the values of m array
     // m and o are assigned again and again at each recursive call, but that is harmless
     // params working as local variables as the function is recursive
     q,v,r) => 
{
   s = [...s]; //convert to array, as I have to modify it while scanning
     //the conversion is done again and again at each recursive call, but that is harmless
   s[p] = 0; // make s[p] invalid to avoid bouncing back and forth
  
   m.some( // for each direction, stop if endpoint found
      (d,i ) => {
        q = p; // start at current position, then will add the offset relative to current direction
        v = w; // for each direction, assign the starting value that can be '+' or garbage at first call
        // compare char at next position with the expected char based on direction (- for horiz, | for vertical)
        // in m array, values at even positon are vertical movements, odds are horizontal
        while (s[q+=d] == '|-'[i&1]) // while straight direction, follow it until it ends
          v = '+'; // from now on '+' is allowed
        r = s[q];
        if (r == v) // check if found a '+'
          r = U(s, q, v) // recursive call to check all directions from current position
        else
        {
          r = s[q + m['^>V<'.indexOf(r)]], // pointed char in p (if arrowhead, else will return undefined)
          r = /\w/.test(r) && r // if alphanumeric, then we found our result - else r = false
        }  
        return r; // returning r to .some; if r is truthy, the .some function will stop
      }
   ) 
   return r; // result if found, else undefined or null
}

// TEST
out=x=>O.innerHTML+=x.replace(/</g,'&#60;')+'\n'

// Using explicit newlines '\n' to ensure the padding to a rectangle
;[
 ['z<+S-+->a','a'] 
,['a<-+S>b','b']
,['S-+\n  |\n  V\n  a','a']
,['a S      s\n  |      |\n  V      V\n  b      c  ','b']
,['S-----+  \n|     +-^  \n+---+->B \n    +---^','B']
,['d s<+S+--V\n    |||  Q\n    -++    ','Q']
,['d s-+   +-S  +--+\n    +-->b |  |  |\n     |  | +--+  |\n     +--+ A<----+  ','A']
,['S-----+   \n|     +-^ \n+---+->B  \n    +---^ ','B']
].forEach(t=>{
  r=f(t[0])
  k=t[1]
  out('Test '+(r==k?'OK':'Fail')+'\n'+t[0]+'\nResult: '+ r +'\nCheck: '+k+'\n')
})
<pre id=O></pre>


5

JavaScript 2016, 264 263 249 240 235 234 ไบต์

เรียกใช้ใน Firefox:

m=l=>{o=(q,e)=>q.indexOf(e)
s=[-1,1,c=~o(l,`
`),-c]
f=i=>!o[i]&(!(r=l[i+s[o(a="<>^V",o[i]=c=l[i])]])|r<"0"|r>"z"|r=="S"||alert(s=r))&&c&&[for(j of (c!='+'?a:"")+"-|+")for(k of s)l[i+k]!=j|~o(l[i]+j,k*k>1?"-":"|")||f(i+k)]
f(o(l,'S'))}

m(`d s-+   +-S  +--+
    +-->b |  |  |
     |  | +--+  |
     +--+ A<----+`)

มีบางส่วนในบันทึกย่อของฉัน:

m=l=>{o=(q,e)=>q.indexOf(e) //shorthand for indexOf
s=[-1,1,c=o(l,`
`)+1,-c] // get all possible moves in 1d
w=[] // to keep track of the already-visited indexes
f=i=>{c=l[i] // current character
if(!w[i]&&c) // only continue if the index wasn't already visited and we aren't out of bounds
{r=l[i+s[o(a="<>^V",w[i]=c)]] // sets w[i] to a truthy value and maps the arrows to their corresponding moves in s. If c is an arrow, r is the character it's pointing to.
if(!r||r<"0"||r>"z"||r=="S"||alert(r)) // if r is an alphanumeric character that isn't r, show it and return. If not, continue.
for(j of (c!='+'?a:"")+"-|+") // iterate over all characters to make sure plusses are checked out last, which is necessary at the start.
for(k of s) // check out all possible moves
l[i+k]!=j|| // check if the move leads to the character we're looking for
~o(l[i]+j,k*k>1?"-":"|")|| // make sure the current nor that character goes against the direction of the move
f(i+k)}} // make the move, end of f
f(o(l,'S'))} // start at S

คุณอาจบันทึกบางส่วนด้วยการทำo = 'indexOf'แล้วทำq[o](e)เมื่อคุณต้องการใช้
ไม่ใช่ชาร์ลส์ที่

นอกจากนี้ในการตีกอล์ฟfor(;;)วงมักจะมีประสิทธิภาพมากที่สุด อาจผิดในกรณีนี้ แต่ลองใช้ดู
ไม่ใช่ชาร์ลส์ที่

1
กรณีทดสอบ a<-+S->bฉันคิดว่าควรให้bเท่านั้นเนื่องจากA arrow ที่ถูกต้องจะไม่เริ่มต้นด้วย +
edc65

แก้ไขที่และเปลี่ยน f เป็นหนึ่งซับ ฉันชอบแนวทางของคุณในการแก้ไขปัญหานี้จริงๆ
bopjesvla

หมายเหตุด้านข้าง: ฉันรักความเข้าใจในอาเรย์จริงๆ แต่มันไม่ได้อยู่ในมาตรฐาน EcmaScript ทุกรุ่นอีกต่อไป ฉันขอแนะนำให้ติดแท็กคุณ anser JavaScript 2016(
stiil

3

VBA Excel 2007, 894 ไบต์

อย่างนี้เริ่มดีขึ้นมากแล้วมันก็จบลง ฉันมีความรู้สึกว่าตรรกะของฉันมีข้อบกพร่องและฉันสามารถบันทึกจำนวนหนึ่งไบต์ถ้าฉันได้จัดลำดับตรรกะของฉันใหม่ แต่เวลามากเกินไปที่จมลงไปในสิ่งนี้แล้ว = P

อินพุตสำหรับสิ่งนี้คือคอลัมน์ A ของแผ่นงานใด ๆ ที่คุณอยู่ วิธีนี้ใช้ความจริงที่ว่า Excel มีกริดที่ดีและแบ่งทุกอย่างออกเพื่อให้คุณสามารถดูว่ามันทำอะไรได้ชัดเจนขึ้น

Sub m()กำลังคัดลอกข้อมูลที่วางจากคอลัมน์ A และทำลายโดย char ถ้าเราอนุญาตให้มีการดัดแปลงอินพุตถ้าคุณทำการฟอร์แมตเขาวงกตเป็น 1 ถ่านต่อเซลล์คุณสามารถบันทึกไม่กี่ไบต์โดยการลบsub m()

วางเขาวงกตลงใน Excel ขนาดไม่เกิน 99 แถวโดย 27 อักขระ หากคุณต้องการเขาวงกตที่ใหญ่กว่านี้จะมีเพียง 2 ไบต์พิเศษเพื่อเพิ่มขอบเขตเป็น 999 แถวและคอลัมน์ ZZ

นอกจากนี้อาจต้องมีการเรียกผู้ตัดสินว่าแผ่นงาน Excel นั้นถูกต้อง "อินพุตมาตรฐาน" สำหรับคำตอบ VBA ถ้าไม่เป็นไปไม่ได้ที่จะให้ VBA Multi-line VIA กับหน้าต่างทันที

หากต้องการเรียกใช้รหัสนี้เพียงวางรหัสนี้ลงในโมดูล Excel วางเขาวงกตลงใน A1 แล้วเรียกใช้ sub j()

Sub m()
For k=1 To 99
u=Cells(k,1).Value
For i=1 To Len(u)
Cells(k,i+1).Value=Mid(u,i,1)
Next
Next
Columns(1).Delete
End Sub
Sub j()
m
With Range("A:Z").Find("S",,,,,,1)
h .row,.Column,0
End With
End Sub
Sub h(r,c,t)
If t<>1 Then l r-1,c,1,0,r
If t<>-1 Then l r+1,c,-1,0,r
If t<>2 Then l r,c-1,2,0,r
If t<>-2 Then l r,c+1,-2,0,r
End Sub
Sub l(r,c,y,p,i)
On Error GoTo w
q=Cells(r,c).Text
If q="V" Or q="^" Or q="<" Or q=">" Then
p=1
End If
f="[^V<>]"
If p=1 And q Like "[0-9A-UW-Za-z]" Then
MsgBox q: End
Else
If q="^" Then p=1: GoTo t
If q="V" Then p=1: GoTo g
If q="<" Then p=1: GoTo b
If q=">" Then p=1: GoTo n
Select Case y
Case 1
t:If q="|" Or q Like f Then l r-1,c,y,p,q
Case -1
g:If q="|" Or q Like f Then l r+1,c,y,p,q
Case 2
b:If q="-" Or q Like f Then l r,c-1,y,p,q
Case -2
n:If q="-" Or q Like f Then l r,c+1,y,p,q
End Select
If q="+" And i<>"S" Then h r,c,y*-1
p=0
End If
w:
End Sub

2

Python 3, 349 ไบต์

ฮึมีจำนวนมาก

S=input().split('\n')
Q=[[S[0].find('S'),0]]
D=[[1,0],[0,1],[-1,0],[0,-1]]
A='-S--++-'
B='|S||++|'
P='>V<^'
i=0
while D:
 a,b=Q[i];i+=1
 j=S[b][a]
 for x,y in D:
  try:
   c,d=a+x,b+y;k=S[d][c]
   if k in P:
    z=P.index(k);print(S[d+D[z][1]][c+D[z][0]]);D=[]
   if (j+k in A and x)+(j+k in B and y)*~([c,d]in Q):Q+=[[c,d]]
  except IndexError: pass

เป็นหลักการค้นหาความกว้างแรก โบนัส: นี่เป็นทางออกจริง ๆ แทนที่จะใช้exit()อย่างมีความสุข


สิ่งนี้ไม่ได้ใช้ข้อ จำกัด ในการค้นหาประมาณครึ่งหนึ่งนั่นคือมนุษย์ ideone.com/OzoWRX
bopjesvla

@bopjesvla: Drat คุณพูดถูก จุดที่ดี!
El'endia Starman

คุณสามารถใช้สตริงหลายบรรทัดได้input()อย่างไร มันเป็นปัญหาสำหรับฉัน
The_Basset_Hound

@The_Basset_Hound: คุณไม่สามารถป้อนบรรทัดใหม่ด้วยตนเอง คุณต้องคัดลอก - วางทุกอย่างพร้อมกัน
El'endia Starman

@ El'endiaStarman ฉันกำลังคัดลอก / วางและยังคงอ่านเฉพาะบรรทัดแรกที่ดูเหมือน
The_Basset_Hound

2

Perl 5

การแก้ปัญหาเปิดออกนานกว่าโซลูชันอื่น ๆ
แม้หลังจากเล่นกอล์ฟแล้ว ดังนั้นนี่คือเวอร์ชันที่ไม่ดี
มันพิมพ์แผนที่เพื่อให้คุณสามารถติดตามเคอร์เซอร์

มันทำงานอย่างไร? ในแต่ละขั้นตอนจะทำการย้ายที่เป็นไปได้ในสแต็ค และมันยังคงทำงานต่อไปจนกระทั่งไม่มีอะไรเหลืออยู่บนสแต็กหรือพบวิธีแก้ปัญหา
สามารถแก้ไขได้อย่างง่ายดายเพื่อค้นหาวิธีแก้ไขทั้งหมดและเลือก -> ขณะที่ (@_) {...

while(<>){
  chomp;
  @R=split//;
  $j++;$i=0;
  for(@R){$nr++;$i++;$A[$i][$j]=$_;if('S'eq$_){$x=$i;$y=$j}}
  $xm=$i,if$xm<$i;$ym=$j;
}
push(@_,[($x,$y,'S',0)]);
$cr='';
while(@_&&''eq$cr){
 @C=pop@_;
 ($x,$y,$d,$n)=($C[0]->[0],$C[0]->[1],$C[0]->[2],$C[0]->[3]);
 $c=$A[$x][$y];
 $A[$x][$y]='.';
 if($c=~m/[S+]/){
    if('L'ne$d&&$A[$x+1][$y]=~m/[+-]/){push(@_,[($x+1,$y,'R',$n+1)])}
    if('D'ne$d&&$A[$x][$y-1]=~m/[+|]/){push(@_,[($x,$y-1,'U',$n+1)])}
    if('R'ne$d&&$A[$x-1][$y]=~m/[+-]/){push(@_,[($x-1,$y,'L',$n+1)])}
    if('U'ne$d&&$A[$x][$y+1]=~m/[+|]/){push(@_,[($x,$y+1,'D',$n+1)])}
 }
 if($c eq'|'){
    if($d ne'U'&&$A[$x][$y+1]=~m/[+|<>^V]/){push(@_,[($x,$y+1,'D',$n+1)])}
    if($d ne'D'&&$A[$x][$y-1]=~m/[+|<>^V]/){push(@_,[($x,$y-1,'U',$n+1)])}
 }
 if($c eq'-'){
    if($d ne'L'&&$A[$x+1][$y]=~m/[+-<>^V]/){push(@_,[($x+1,$y,'R',$n+1)])}
    if($d ne'R'&&$A[$x-1][$y]=~m/[+-<>^V]/){push(@_,[($x-1,$y,'L',$n+1)])}
 }
 if($c=~m/[<>^V]/&&$n<$nr){
    if($c eq'>'&&$A[$x+1][$y]=~m/\w/){$cr=$A[$x+1][$y];$nr=$n}
    if($c eq'<'&&$A[$x-1][$y]=~m/\w/){$cr=$A[$x-1][$y];$nr=$n}
    if($c eq'V'&&$A[$x][$y+1]=~m/\w/){$cr=$A[$x][$y+1];$nr=$n}
    if($c eq'^'&&$A[$x][$y-1]=~m/\w/){$cr=$A[$x][$y-1];$nr=$n}
 }
 print_map()
}
print "$cr\n";
sub print_map {
    print "\033[2J"; #clearscreen
    print "\033[0;0H"; #cursor at 0,0
    for$j(1..$ym){for$i(1..$xm){print (($x==$i&&$y==$j)?'X':$A[$i][$j])}print"\n"}
    sleep 1;
}

ทดสอบ

$ cat test_arrows6.txt
S-----+
|     +-^
+---+->B
    +---^

$ perl arrows.pl < test_arrows6.txt
.-----+
.     +-^
......XB
    .....
B

2
ฉันเห็นชื่อและความคิด "Perl" และ "ทำใน 5 ไบต์" หัวใจวาย
Conor O'Brien

2

รุ่น PHP (ความคิดเห็นเป็นภาษาฝรั่งเศส, ขอโทษ)

<?php
/*
* By Gnieark https://blog-du-grouik.tinad.fr oct 2015
* Anwser to "code golf" http://codegolf.stackexchange.com/questions/57952/where-is-the-arrow-pointing in PHP
*/
//ouvrir le fichier contenant la "carte et l'envoyer dans un array 2 dimmension
$mapLines=explode("\n",file_get_contents('./input.txt'));
$i=0;
foreach($mapLines as $ligne){
    $map[$i]=str_split($ligne,1);
    if((!isset($y)) && in_array('S',$map[$i])){
        //tant qu'à parcourir la carte, on cherche le "S" s'il n'a pas déjà été trouvé.
        $y=$i;
        $x=array_search('S',$map[$i]);
    }
    $i++;
}
if(!isset($y)){
    echo "Il n'y a pas de départ S dans ce parcours";
    die;
}
echo "\n".file_get_contents('./input.txt')."\nLe départ est aux positions ".$map[$y][$x]." [".$x.",".$y."]. Démarrage du script...\n";
$previousX=-1; // Contiendra l'ordonnée de la position précédente. (pour le moment, une valeur incohérente)
$previousY=-1; // Contiendra l'absycede la position précédente. (pour le moment, une valeur incohérente)
$xMax=count($map[0]) -1;
$yMax=count($map) -1;
$previousCrosses=array(); //On ne gardera en mémoire que les croisements, pas l'ensemble du chemin.
while(1==1){ // C'est un défi de codagee, pas un script qui sera en prod. j'assume.
    switch($map[$y][$x]){
        case "S":
            //même fonction que "+"
        case "+":
            //on peut aller dans les 4 directions
            $target=whereToGoAfterCross($x,$y,$previousX,$previousY);
            if($target){
          go($target[0],$target[1]);
            }else{
          goToPreviousCross();
            }
            break;
        case "s":
            goToPreviousCross();
            break;
        case "-":
        //déplacement horizontal
        if($previousX < $x){
          //vers la droite
          $targetX=$x+1;
          if(in_array($map[$y][$targetX],array('-','+','S','>','^','V'))){
        go($targetX,$y);
          }else{
        //On est dans un cul de sac
        goToPreviousCross();
          }
        }else{
          //vers la gauche
          $targetX=$x-1;
          if(in_array($map[$y][$targetX],array('-','+','S','<','^','V'))){
        go($targetX,$y);
          }else{
        //On est dans un cul de sac
        goToPreviousCross();
          }
        }
            break;
        case "|":
        //déplacement vertical
        if($previousY < $y){
          //on descend (/!\ y augmente vers le bas de la carte)
          $targetY=$y+1;
          if(in_array($map[$targetY][$x],array('|','+','S','>','<','V'))){
        go ($x,$targetY);
          }else{
        goToPreviousCross();
          }
        }else{
        //on Monte (/!\ y augmente vers le bas de la carte)
          $targetY=$y - 1;
          if(in_array($map[$targetY][$x],array('|','+','S','>','<','V'))){
        go ($x,$targetY);
          }else{
        goToPreviousCross();
          } 
        }
            break;
    case "^":
    case "V":
    case ">":
    case "<":
      wheAreOnAnArrow($map[$y][$x]);
      break;
    }
}
function wheAreOnAnArrow($arrow){
  global $x,$y,$xMax,$yMax,$map;
  switch($arrow){
    case "^":
      $targetX=$x;
      $targetY=$y -1;
      $charsOfTheLoose=array(" ","V","-","s");
      break;
    case "V":
      $targetX=$x;
      $targetY=$y + 1;
      $charsOfTheLoose=array(" ","^","-","s");
      break;
    case ">":
      $targetX=$x + 1;
      $targetY=$y;
      $charsOfTheLoose=array(" ","<","|","s");   
      break;
    case "<":
      $targetX=$x - 1;
      $targetY=$y;
      $charsOfTheLoose=array(" ",">","|","s");   
      break;
    default:     
      break;
  }
  if(($targetX <0) OR ($targetY<0) OR ($targetX>$xMax) OR ($targetY>$yMax) OR (in_array($map[$targetY][$targetX],$charsOfTheLoose))){
      //on sort du cadre ou on tombe sur un caractere inadapté
      goToPreviousCross();
  }else{
    if(preg_match("/^[a-z]$/",strtolower($map[$targetY][$targetX]))){
      //WIN
      echo "WIN: ".$map[$targetY][$targetX]."\n";
      die;
     }else{
      //on va sur la cible
      go($targetX,$targetY);
     }
  }
}
function whereToGoAfterCross($xCross,$yCross,$previousX,$previousY){

            //haut
            if(canGoAfterCross($xCross,$yCross +1 ,$xCross,$yCross,$previousX,$previousY)){
                return array($xCross,$yCross +1);
            }elseif(canGoAfterCross($xCross,$yCross -1 ,$xCross,$yCross,$previousX,$previousY)){
                //bas
                return array($xCross,$yCross -1);
            }elseif(canGoAfterCross($xCross-1,$yCross,$xCross,$yCross,$previousX,$previousY)){
                //gauche
                return array($xCross-1,$yCross);
            }elseif(canGoAfterCross($xCross+1,$yCross,$xCross,$yCross,$previousX,$previousY)){
                //droite
                return array($xCross+1,$yCross);
            }else{
          //pas de direction possible
          return false;
            }  
}
function canGoAfterCross($xTo,$yTo,$xNow,$yNow,$xPrevious,$yPrevious){
    global $previousCrosses,$xMax,$yMax,$map;
    if(($xTo < 0) OR ($yTo < 0) OR ($xTo >= $xMax) OR ($yTo >= $yMax)){return false;}// ça sort des limites de la carte
    if(
    ($map[$yTo][$xTo]==" ") // on ne va pas sur un caractere vide
    OR (($xTo==$xPrevious)&&($yTo==$yPrevious)) //on ne peut pas revenir sur nos pas (enfin, ça ne servirait à rien dans cet algo)
    OR (($xTo==$xNow)&&($map[$yTo][$xTo]=="-")) //Déplacement vertical, le caractere suivant ne peut etre "-"
    OR (($yTo==$yNow)&&($map[$yTo][$xTo]=="|")) // Déplacement horizontal, le caractère suivant ne peut être "|"
    OR ((isset($previousCrosses[$xNow."-".$yNow])) && (in_array($xTo."-".$yTo,$previousCrosses[$xNow."-".$yNow]))) //croisement, ne pas prendre une direction déjà prise
   ){    
      return false;
    }
    return true;    
}
function go($targetX,$targetY){
    global $previousX,$previousY,$x,$y,$previousCrosses,$map;
    if(($map[$y][$x]=='S')OR ($map[$y][$x]=='+')){
        //on enregistre le croisement dans lequel on renseigne la direction prise et la direction d'origine
        $previousCrosses[$x."-".$y][]=$previousX."-".$previousY;
        $previousCrosses[$x."-".$y][]=$targetX."-".$targetY; 
    }
    $previousX=$x;
    $previousY=$y;
    $x=$targetX;
    $y=$targetY;
    //debug
    echo "deplacement en ".$x.";".$y."\n";   
}
function goToPreviousCross(){
  global $x,$y,$previousX,$previousY,$xMax,$yMax,$previousCrosses;

  /*
  * On va remonter aux précédents croisements jusqu'à ce 
  * qu'un nouveau chemin soit exploitable
  */
  foreach($previousCrosses as $key => $croisement){
    list($crossX,$crossY)=explode("-",$key);
    $cross=whereToGoAfterCross($crossX,$crossY,-1,-1);
    if($cross){
      go($crossX,$crossY);
      return true;
    } 
  }
  //si on arrive là c'est qu'on est bloqués
  echo "Aucun chemin n'est possible\n";
  die;
}

1

Haskell, 268 ไบต์

ขอแสดงความยินดีกับ Javascripters! เติมเงินรางวัล แต่นี่คือสิ่งที่ฉันได้รับ พฤษภาคม / อาจไม่ทำงานในทุกกรณี แต่จริง ๆ แล้วจัดการลูกศรที่เริ่มต้นและหัวลูกศรที่เชื่อมโยงกับ+es เท่าที่ฉันรู้ ไม่ได้รวมถึงการค้นหาSเพียง(0,0)เพื่อตอนนี้

import Data.List
x%m=length m<=x||x<0
a s(x,y)z|y%s||x%(head s)=[]|0<1=b s(x,y)z$s!!y!!x
b s(x,y)z c|g c>[]=filter(>' ')$concat[a s(x+i k,y+i(3-k))k|k<-g c,k-z`mod`4/=2]|0<1=[c]
i=([0,-1,0,1]!!)
g c=findIndices(c`elem`)$words$"V|+S <-+S ^|+S >-+S"
f s=a(lines s)(0,0)0

0

ฉันต้องการดูเวอร์ชัน APL ด้วยจิตวิญญาณของ https://www.youtube.com/watch?v=a9xAKttWgP4

เมื่อเริ่มต้นแล้วจูเลียสแบบเวกเตอร์ซึ่งฉันคิดว่าสามารถแปลได้ 1: 0.3 เป็น APL หรือ J ใช้สตริง R แทนลูกศร L x K ก่อนอื่นแปลเมทริกซ์ของสัญลักษณ์เป็นเมทริกซ์ของเมทริกซ์ 3x3 ขนาดเล็กที่มีรูปแบบคือการขยายไบนารีของตัวอักษรของสตริง "\ 0 \ x18 \ fH \ t]] \ x1cI" ตัวอย่างเช่น '+' ถูกเข้ารหัสเป็น reshape ([0, ตัวเลข (int (']'), 2,8)], 3,3)

  0 2 0
  2 2 2
  0 2 0

ในการเป็นตัวแทนนี้เส้นทางประกอบด้วย 2 และถูกน้ำท่วมโดย 3 จากจุดเริ่มต้น

A=zeros(Int,L*3+3,K*3+3)
s(i,j=i,n=2)=A[i:end+i-n,j:end+j-n]
Q=Int['A':'Z';'a':'z']
for k=0:K-1,l=0:L-1
r=R[K*l+k+1]
q=search(b"V^><+S|-",r)
d=reverse(digits(b"\0\x18\fH\t]]\x1cI"[q+1],2,8))
A[1+3l:3l+3,1+3k:3k+3]=2*[d;0]
A[3l+2,3k+2]+=r*(r in Q&&r!='V'&&r!='^')+(1-r)*(r=='S')+3(5>q>0)
end
m=0
while sum(A)>m
m=sum(A)
for i=0:1,j=1:2,(f,t)=[(3,6),(11,15)]
A[j:end+j-2,j:end+j-2]=max(s(j),f*(s(j).*s(2-i,1+i).==t))
end
end
for i=[1,4],k=Q
any(s(1,1,5).*s(5-i,i,5).==11*k)&&println(Char(k))
end

ทดสอบ,

   R=b"""
       d  S------+    b
                 |     
                 |     
          c      +--->a
       """;

   K=search(R,'\n') # dimensions
   L=div(endof(R),K)


   include("arrow.jl")

a

โดยวิธีการที่ฉันคิดว่าประโยค "อีก + อาจจะอยู่ติดกัน แต่ลูกศรควรจัดลำดับความสำคัญไปที่ - หรือ | ก่อน" วางแนวทางเวกเตอร์ที่เสียเปรียบ อย่างไรก็ตามฉันแค่เพิกเฉยมัน

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.