主题:MM,帅哥!帮帮我!很急!!
随便你好
[专家分:530] 发布于 2007-03-23 19:20:00
【112】半张棋盘如图①所示,马从棋盘的左下角起跳,目标是棋盘的右上角。并规定马只许往右跳,不许往左跳。图中所示为其中一种跳行路线,请你设计一个程序,由计算机寻找并打印出马的所有可能的跳行的路线。图②所示为马向右的四种跳法。
这是一个典型的深度优先搜索问题,下面给出程序清单,请在①②中填上内容,使程序完整。试试看程序能否正确运行(正确结果有37条路线)
CLS : FOR i = 1 TO 4: READ v(i, 1), v(i, 2): NEXT
DATA 2,1,1,2,2,-1,1,-2,2
p = 1: x = 1: y = 1: x(p) = x: y(p) = y: i = 0: xe = 9: ye = 5
WHILE p > 0
i = i + 1
IF i <= 4 THEN
x = x(p) + v(i, 1): y = y(p) + v(i, 2)
IF ① THEN
p = p + 1: s(p) = I: x(p) = x: y(p) = y: i = 0
IF ② THEN GOSUB 10
END IF
ELSE
i = s(p): p = p - 1
END IF
WEND: END
10 n = n + 1: PRINT n; " "; : FOR j = 1 TO p - 1: PRINT USING "#"; x(j);
PRINT ","; : PRINT USING "#"; y(j); : PRINT " --> "; : NEXT
PRINT USING "#"; x(j); : PRINT ","; : PRINT USING "#"; y(j): RETURN
回复列表 (共5个回复)
沙发
Lovely哆啦 [专家分:1360] 发布于 2007-03-30 21:09:00
很难,解不出来
板凳
爱丽斯LING [专家分:560] 发布于 2007-04-01 16:27:00
CLS : FOR i = 1 TO 4: READ v(i, 1), v(i, 2): NEXT
DATA 2,1,1,2,2,-1,1,-2,2
p = 1: x = 1: y = 1: x(p) = x: y(p) = y: i = 0: xe = 9: ye = 5
WHILE p > 0
i = i + 1
IF i <= 4 THEN
x = x(p) + v(i, 1): y = y(p) + v(i, 2)
IF P<=8 THEN
p = p + 1: s(p) = I: x(p) = x: y(p) = y: i = 0
IF P>=7 OR P>=8 THEN GOSUB 10
END IF
ELSE
i = s(p): p = p - 1
END IF
WEND: END
10 n = n + 1: PRINT n; " "; : FOR j = 1 TO p - 1: PRINT USING "#"; x(j);
PRINT ","; : PRINT USING "#"; y(j); : PRINT " --> "; : NEXT
PRINT USING "#"; x(j); : PRINT ","; : PRINT USING "#"; y(j): RETURN
3 楼
QB小猪 [专家分:1200] 发布于 2007-08-13 10:40:00
CLS : FOR i = 1 TO 4: READ v(i, 1), v(i, 2): NEXT
DATA 2,1,1,2,2,-1,1,-2
p = 1: x = 1: y = 1: x(p) = x: y(p) = y: i = 0: XE = 9: YE = 5
WHILE p > 0
i = i + 1
IF i <= 4 THEN
x = x(p) + v(i, 1): y = y(p) + v(i, 2)
IF x >= 1 AND x <= XE AND y >= 1 AND y <= YE THEN
p = p + 1: s(p) = i: x(p) = x: y(p) = y: i = 0
IF x = XE AND y = YE THEN GOSUB 10
END IF
ELSE
i = s(p): p = p - 1
END IF
WEND: END
10 n = n + 1: PRINT n; " "; : FOR j = 1 TO p - 1: PRINT USING "#"; x(j);
PRINT ","; : PRINT USING "#"; y(j); : PRINT " --> "; : NEXT
PRINT USING "#"; x(j); : PRINT ","; : PRINT USING "#"; y(j): RETURN
这才是正确答案!!!
4 楼
天尝地酒 [专家分:870] 发布于 2007-08-14 10:00:00
CLS : FOR i = 1 TO 4: READ v(i, 1), v(i, 2): NEXT
DATA 2,1,1,2,2,-1,1,-2,2
p = 1: x = 1: y = 1: x(p) = x: y(p) = y: i = 0: xe = 9: ye = 5
WHILE p > 0
i = i + 1
IF i <= 4 THEN
x = x(p) + v(i, 1): y = y(p) + v(i, 2)
IF x >= 1 AND x <= XE AND y >= 1 AND y <= YE① THEN
p = p + 1: s(p) = I: x(p) = x: y(p) = y: i = 0
IF x=xe and y=ye② THEN GOSUB 10
END IF
ELSE
i = s(p): p = p - 1
END IF
WEND: END
10 n = n + 1: PRINT n; " "; : FOR j = 1 TO p - 1: PRINT USING "#"; x(j);
PRINT ","; : PRINT USING "#"; y(j); : PRINT " --> "; : NEXT
PRINT USING "#"; x(j); : PRINT ","; : PRINT USING "#"; y(j): RETURN
5 楼
wzc1996 [专家分:1680] 发布于 2007-08-16 21:02:00
CLS : FOR i = 1 TO 4: READ v(i, 1), v(i, 2): NEXT
DATA 2,1,1,2,2,-1,1,-2,2
p = 1: x = 1: y = 1: x(p) = x: y(p) = y: i = 0: xe = 9: ye = 5
WHILE p > 0
i = i + 1
IF i <= 4 THEN
x = x(p) + v(i, 1): y = y(p) + v(i, 2)
IF x >= 1 AND x <= XE AND y >= 1 AND y <= YE THEN
p = p + 1: s(p) = I: x(p) = x: y(p) = y: i = 0
IF x=xe and y=ye THEN GOSUB 10
END IF
ELSE
i = s(p): p = p - 1
END IF
WEND: END
10 n = n + 1: PRINT n; " "; : FOR j = 1 TO p - 1: PRINT USING "#"; x(j);
PRINT ","; : PRINT USING "#"; y(j); : PRINT " --> "; : NEXT
PRINT USING "#"; x(j); : PRINT ","; : PRINT USING "#"; y(j): RETURN
我来回复