主题:[原创]QBasic 迷宫
CONST true = -1, False = 0
CLS
a$ = STRING$(3, CHR$(219)): b$ = STRING$(3, CHR$(176))
INPUT "X, y = ", x, y: ptx = x: pty = y
DIM a(12, 12)
FOR i = 1 TO x
FOR j = 1 TO y
Daterr:
CLS
pri (ptx), (pty), (i), (j), a$, b$
PRINT "X, y = "; LTRIM$(RTRIM$(STR$(x))); ","; LTRIM$(RTRIM$(STR$(y)))
PRINT "I, j = "; LTRIM$(STR$(i)); ","; LTRIM$(STR$(j))
IF bflag = true THEN
PRINT "A("; LTRIM$(RTRIM$(STR$(bflagi))); ","; LTRIM$(STR$(bflagj));
PRINT ")="; LTRIM$(STR$(a(bflagi, bflagj)))
END IF
PRINT "A("; LTRIM$(RTRIM$(STR$(i))); ","; LTRIM$(RTRIM$(STR$(j))); ")=";
DO: in$ = INKEY$: LOOP UNTIL in$ <> ""
IF in$ <> "1" AND in$ <> "0" THEN
PRINT "Connot Press " + CHR$(34) + in$ + CHR$(34)
PRINT "Reinput(Y/N)?"
DO
IF INKEY$ = "y" THEN GOTO Daterr
IF INKEY$ = "n" THEN PRINT "End of input": END
LOOP
END IF
a(i, j) = VAL(in$)
PRINT LTRIM$(RTRIM$(STR$(a(i, j))))
bflag = true: bflagi = i: bflagj = j
NEXT j, i
CLS
DIM s(100), x(100), y(100)
FOR i = 1 TO x
FOR j = 1 TO y
IF a(i, j) = 1 THEN LOCATE i, j * 3: PRINT a$
NEXT j, i
FOR i = 1 TO 4: FOR j = 1 TO 2
READ v(i, j)
NEXT j, i: DATA 0,1,1,0,0,-1,-1,0
LOCATE 13: PRINT "Press any key to run(Press [e] to end of program)"
DO: be$ = INKEY$: LOOP UNTIL be$ <> ""
IF be$ = "e" THEN END
INPUT "Start I, J = ", x, y
startx = x: starty = y
INPUT "End I, J = ", xe, ye
p = 0: x(p) = x: y(p) = y: i = 0
IF xe <= 0 OR ye <= 0 OR xe > ptx OR ye > pty THEN PRINT "Data Error!": END
IF a(xe, ye) = 1 THEN PRINT "Data Error!": END
IF x <= 0 OR y <= 0 OR x > ptx OR y > pty THEN PRINT "Data Error!": END
IF a(x, y) = 1 THEN PRINT "Data Error!": END
GOSUB 10
DO
Nex: i = i + 1
IF i <= 4 THEN
IF x(p) + v(i, 1) > ptx THEN GOTO Nex
IF y(p) + v(i, 2) > pty THEN GOTO Nex
x = x(p) + v(i, 1): y = y(p) + v(i, 2)
IF a(x, y) = 0 THEN
p = p + 1: s(p) = i: x(p) = x: y(p) = y
a(x, y) = 2
i = 0
GOSUB 10
END IF
ELSE
GOSUB 10
i = s(p): p = p - 1
END IF
LOOP UNTIL p = 0 OR x = xe AND y = ye
IF p = 0 THEN
LOCATE 16
PRINT "Not Find"
ELSE
LOCATE 16: PRINT "Start "; LTRIM$(RTRIM$(STR$(startx)));
PRINT ","; LTRIM$(RTRIM$(STR$(starty))); "---->";
FOR i = 1 TO p
PRINT LTRIM$(STR$(x(i))); ","; LTRIM$(STR$(y(i))); "---->";
NEXT i
PRINT "Finish"
END IF
END
10 LOCATE x(p), y(p) * 3: FOR k = 1 TO 30000: NEXT
IF i <= 4 THEN PRINT USING "##"; p + 1 ELSE PRINT b$
RETURN
SUB pri (ptx, pty, i, j, a$, b$)
FOR pi = 1 TO ptx
FOR pj = 1 TO pty
IF pi = i AND pj = j THEN PRINT b$; ELSE PRINT a$;
NEXT pj: PRINT
NEXT pi
END SUB