主题:求一个八皇后问题,有人能编出来吗?
强强
[专家分:4740] 发布于 2009-03-10 19:23:00
谁能编出来?
回复列表 (共6个回复)
沙发
gadenflm [专家分:150] 发布于 2009-03-11 19:26:00
10 I = 1
20 A(I) = 1
30 G = 1
40 FOR K = I - 1 TO 1 STEP -1
50 IF A(I) = A(K) THEN 70
60 IF ABS(A(I) - A(K)) <> I - K THEN 90
70 G = 0
80 GOTO 100
90 NEXT K
100 IF I <> 8 THEN 180
110 IF G = 0 THEN 180
120 FOR L = 1 TO 8
130 PRINT USING "##"; A(L);
140 NEXT L
150 PRINT "*";
160 M = M + 1
170 IF M MOD 3 = 0 THEN PRINT
180 IF G = 0 THEN 230
190 IF I = 8 THEN 230
200 I = I + 1
210 A(I) = 1
220 GOTO 30
230 IF A(I) < 8 THEN 270
240 I = I - 1
250 IF I = 0 THEN 290
260 GOTO 230
270 A(I) = A(I) + 1
280 GOTO 30
290 PRINT
300 PRINT "SUM="; USING "##"; M;
310 PRINT
320 END
板凳
强强 [专家分:4740] 发布于 2009-03-11 23:27:00
你的程序我都没试,不过看样子好象不能对,这么几行语句就能解决吗??你自己试了吗,如果程序正确的话你应该是个天才
3 楼
天天和和 [专家分:1420] 发布于 2009-03-17 22:22:00
唔!是的饿,我也不会~~
4 楼
hqm1 [专家分:570] 发布于 2009-03-21 12:13:00
DECLARE SUB TRY (I)
CLS
DIM SHARED A(8), B(8), C(16), D(-7 TO 7)
DIM SHARED T
TRY (1)
END
SUB TRY (I)
FOR J = 1 TO 8
IF B(J) = 0 AND C(I + J) = 0 AND D(I - J) = 0 THEN
A(I) = J: B(J) = 1: C(I + J) = 1: D(I - J) = 1
IF I < 8 THEN
TRY (I + 1)
ELSE
T = T + 1: PRINT T;
FOR K = 1 TO 8: PRINT A(K); : NEXT K: PRINT
END IF
B(J) = 0: C(I + J) = 0: D(I - J) = 0
END IF
NEXT J
END SUB
5 楼
szc123 [专家分:80] 发布于 2009-04-11 08:47:00
N皇后的程序
CLS
INPUT n
DIM a(n)
i = 1
WHILE i > 0
a(i) = a(i) + 1
IF a(i) > n THEN
a(i) = 0
i = i - 1
ELSE
p = 1
FOR j = 1 TO i - 1
IF a(i) = a(j) OR i + a(i) = j + a(j) OR i - a(i) = j - a(j) THEN p = 0: EXIT FOR
NEXT j
IF p THEN
IF i = n THEN s = s + 1: GOSUB 10 ELSE i = i + 1
END IF
END IF
WEND
PRINT s
END
10
PRINT "no."; USING "####"; s;
FOR j = 1 TO n
PRINT USING "###"; j;
PRINT ",";
PRINT USING "###"; a(j);
NEXT j
PRINT
RETURN
6 楼
593170024 [专家分:500] 发布于 2009-11-09 19:16:00
CLS
INPUT n
DIM a(n)
t# = TIMER
i = 1
WHILE i > 0
a(i) = a(i) + 1
IF a(i) > n THEN
a(i) = 0
i = i - 1
ELSE
f = -1
FOR j = 1 TO i - 1
IF a(i) = a(j) OR i + a(i) = j + a(j) OR i - a(i) = j - a(j) THEN f = 0: EXIT FOR
NEXT j
IF f = -1 THEN
IF i = n THEN GOSUB 10: S = S + 1 ELSE i = i + 1
END IF
END IF
WEND
PRINT S, TIMER - t#
END
10
FOR k = 1 TO i
PRINT USING "#"; a(k);
NEXT k
PRINT
RETURN
我来回复