2012-01-27 4 views
0

나는 boe-bot라고 불리는 로봇으로 프로젝트를 진행 중입니다.Pbasic에서 boe-bot와의 미로의 최단 거리 계산하기

내 목표는 미로를 두 번 통과하는 것입니다. 첫 번째 실행에서, 나의 boe-bot은 메모리에 저장된 경로를 저장하는 미로를 가로 지릅니다. 두 번째 실행에서는 메모리에 저장된 모든 경로가 있으며 막 다른 방향으로 이어지는 잘못된 경로가 삭제되므로 Boe-Bot은 미로 끝에 최단 경로를 지정할 수 있습니다.

이렇게하려면 막 다른 길로 이어지는 잘못된 경로를 파싱하기위한 교체 규칙을 만들어야합니다.

코드를 pbasic으로 만들었지 만 코드가 잘못되었습니다. 이걸로 나를 도울 수있는 사람이 밖에 있니?

' {$STAMP BS2} 
' {$PBASIC 2.5} 



' -----[ Variables ]---------------------------------------------------------- 
turn VAR Word 
turns VAR Word 
pointer VAR Byte 
ptr VAR pointer 'create an alias for pointer 

' -----[ Main Routine ]------------------------------------------------------- 
DO ' Begin main routine 

ptr = 0 'Points TO the NEXT available position in the array. 

turns(ptr) = turn 'This puts an L in the first position of the array or left turn in array 

ptr = ptr + 1 'Add one TO the pointer so the NEXT letter goes in the NEXT position in the array. 

IF (turns < 3)THEN 'Array needs at least three characters for this algorithm to work 
RETURN 
IF (turns(3)(ptr) - 1 <> "U")THEN 'EXIT IF the NEXT-TO-last turn is NOT a U-Turn 
RETURN 

IF (turns(3) = "LUL") 'Look at the right three characters in the array Left U-Turn Left 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "S" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "LUR") 'Look at the right three characters in the array Left U-Turn Right 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "U" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "LUS") 'Look at the right three characters in the array Left U-turn Straight 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "R" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 
' Increment/decrement routine only changes pulse durations by 2 at a time. 

IF (turns(3) == "RUL") 'Look at the right three characters in the array Right U-turn Left 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "U" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "RUR") 'Look at the right three characters in the array Right U-turn Right 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "L" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "RUS") 'Look at the right three characters in the array Right U-turn Straight 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "L" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer to point to the NEXT character in the array. 

IF (turns(3) == "SUL") 'Look at the right three characters in the array Straight U-turn Left 
ptr = ptr - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(ptr) = "R" 'The turn we should have taken (AND will take NEXT time. 
ptr = ptr + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "SUR") 'Look at the right three characters in the array Straight U-turn Right 
pointer = pointer - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(pointer) = "L" 'The turn we should have taken (AND will take NEXT time. 
pointer = pointer + 1 'set up the pointer TO point TO the NEXT character in the array. 

IF (turns(3) == "SUS") 'Look at the right three characters in the array Straight U-turn Straight 
pointer = pointer - 3 'shorten the array by 3 characters. We'll "chop off" the LUL and replace with S 
turns(pointer) = "U" 'The turn we should have taken (AND will take NEXT time. 
pointer = pointer + 1 'set up the pointer TO point TO the NEXT character in the array. 
+0

문법 또는 알고리즘에 문제가 있습니까? –

+0

더 나은 답변을 얻으려면 더 많은 태그 (pbasic 등), 출력 된 오류 및 (경우에 따라) 의사 코드를 추가 할 수 있습니다. http://sscce.org/ – Glycan

+0

정말로 오류를 게시해야합니다. –

답변

1

이 미로 해결 방법은 A * 계획 알고리즘의 간단한 버전입니다. 이 미로 해결 방법을 설명하는 내 페이지를 체크 아웃 할 수 있습니다. http://www.benaxelrod.com/robots/maze/index.html

코드를 수정하는 데 도움이되는 의사 코드가 있습니다.

관련 문제