求高手用MFP编写求解“八皇后问题”程序
mfp吧
全部回复
仅看楼主
level 6
yuanqc8888 楼主
八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例。该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8X8的国际象棋棋盘上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法。
我1978年40岁时初学程序设计,学过AIGOL-60、FORTURN、BASIC等语言。当时听说八皇后问题后,经过思考画出了框图,先后用AIGOL-60和BASIC写出代码,成功地得出92种摆法。但这些语言是非结构化语言,使用了 GOTO 转移语句。
现在已退休多年。近几天学习 MFP,想编写求解八皇后问题程序,但MFP没有GOTO语句,对MFP又还不怎么熟悉,一时还没有头绪。在此,求高手用MFP编写出求解“八皇后问题”程序,供我等MFP新手学习学习。
2017年04月22日 15点04分 1
level 2
同是新入门的,不过您比我年长多了,我只是高中生,看到您也在奋战我也很有斗志了[哈哈]
2017年04月23日 05点04分 2
level 8
把BASIC代码发给我吧
2017年04月29日 14点04分 3
level 8
大爷你好强啊,都80多的人了还在研究这个。[大拇指]
2017年05月01日 08点05分 4
level 8
这是一个穷举的算法:
function eightqueens()
variable x1=1, y1=1, x2=1, y2=2, x3=1, y3=3, x4=1
variable y4=4, x5=1, y5=5, x6=1, y6=6, x7=1, y7=7
variable x8=1, y8=8, t=0, s
for variable x1 = 1 to 8
for variable x2 = 1 to 8
if abs(x1-x2)>1
for variable x3 = 1 to 8
if x3 != x1
if abs(x3-x1) != 2
if abs(x3-x2)>1
for variable x4 = 1 to 8
if abs(x4-x1) != 3
if x4 != x1
if abs(x4-x2) != 2
if x4 != x2
if abs(x4-x3)>1
for variable x5 = 1 to 8
if abs(x5-x1) != 4
if x5 != x1
if abs(x5-x2) != 3
if x5 != x2
if abs(x5-x3) != 2
if x5 != x3
if abs(x5-x4)>1
for variable x6 = 1 to 8
if abs(x6-x1) != 5
if x6 != x1
if abs(x6-x2) != 4
if x6 != x2
if abs(x6-x3) != 3
if x6 != x3
if abs(x6-x4) != 2
if x6 != x4
if abs(x6-x5)>1
for variable x7 = 1 to 8
if abs(x7-x1) != 6
if x7 != x1
if abs(x7-x2) != 5
if x7 != x2
if abs(x7-x3) != 4
if x7 != x3
if abs(x7-x4) != 3
if x7 != x4
if abs(x7-x5) != 2
if x7 != x5
if abs(x7-x6)>1
for variable x8 = 1 to 8
if abs(x8-x1) != 7
if x8 != x1
if abs(x8-x2) != 6
if x8 != x2
if abs(x8-x3) != 5
if x8 != x3
if abs(x8-x4) != 4
if x8 != x4
if abs(x8-x5) != 3
if x8 != x5
if abs(x8-x6) != 2
if x8 != x6
if abs(x8-x7)>1
s=t+1
print("\n"+"第"+s+"种摆法")
print("\n")
print("x1="+x1)
print("\n")
print("y1="+y1)
print("\n")
print("x2="+x2)
print("\n")
print("y2="+y2)
print("\n")
print("x3="+x3)
print("\n")
print("y3="+y3)
print("\n")
print("x4="+x4)
print("\n")
print("y4="+y4)
print("\n")
print("x5="+x5)
print("\n")
print("y5="+y5)
print("\n")
print("x6="+x6)
print("\n")
print("y6="+y6)
print("\n")
print("x7="+x7)
print("\n")
print("y7="+y7)
print("\n")
print("x8="+x8)
print("\n")
print("y8="+y8)
t=t+1
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
next
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
next
endif
endif
endif
endif
endif
endif
endif
endif
endif
next
endif
endif
endif
endif
endif
endif
endif
next
endif
endif
endif
endif
endif
next
endif
endif
endif
next
endif
next
next
print("\n")
print("总摆法数为"+t)
endf
2017年05月06日 14点05分 5
level 1
zan
2017年05月08日 23点05分 6
level 6
yuanqc8888 楼主
穷举法当然能搜索出全部结果,但效率偏低,应该在条件不满足时及时回朔,代码也短得多。
下面是我在C4driod上的结果:
2017年05月13日 03点05分 7
level 6
yuanqc8888 楼主
长图不清晰
2017年05月13日 03点05分 9
level 6
yuanqc8888 楼主
2017年05月13日 03点05分 10
level 6
向前辈致敬!90年21岁学习笫一门编程语言fortran77,下面是我的回溯法mfp代码:
function eightqueens()
variable queen=alloc_array(8), _//可放棋子位置
a=alloc_array(8), _ //竖列占用
b=alloc_array(15), _ //右上斜线占用
c=alloc_array(15) //右下斜线占用
variable row=0,col=0,ok=0
variable t=0 //方案计数
queen[row]=-1 //首行的列初值,进入for循环用
do //回溯循环
for col=queen[row]+1 to 7 //正向试放循环
ok=iff(and(a[col]==0,b[col+row]==0, _
c[col-row+7]==0),1,0)
if ok==1
queen[row]=col //记录第row行col列可放位置,用于回溯定位及结果输出
a[col]=1
b[col+row]=1
c[col-row+7]=1
row=row+1 //继续试放下一行
if row>7 //找到方案
t=t+1
print(t)
print(queen)
print("\n")
break
endif
col=-1 //下一行的列初值,维持试放循环
endif
next
//不能继续试放或找到一个方案,for循环结束,开始回溯
row=row-1 //回溯
if row>=0
col=queen[row]
a[col]=0
b[col+row]=0
c[col-row+7]=0
endif
until row<0 //回溯结束
endf
部分结果如下
$>eightqueens()
1[0, 4, 7, 5, 2, 6, 1, 3]
2[0, 5, 7, 2, 6, 3, 1, 4]
3[0, 6, 3, 5, 7, 1, 4, 2]
4[0, 6, 4, 7, 1, 3, 5, 2]
5[1, 3, 5, 7, 2, 0, 6, 4]
6[1, 4, 6, 0, 2, 7, 5, 3]
7[1, 4, 6, 3, 0, 7, 5, 2]
8[1, 5, 0, 6, 3, 7, 2, 4]
9[1, 5, 7, 2, 0, 3, 6, 4]
10[1, 6, 2, 5, 7, 4, 0, 3]
11[1, 6, 4, 7, 0, 3, 5, 2]
12[1, 7, 5, 0, 2, 4, 6, 3]
13[2, 0, 6, 4, 7, 1, 3, 5]
14[2, 4, 1, 7, 0, 6, 3, 5]
15[2, 4, 1, 7, 5, 3, 6, 0]
16[2, 4, 6, 0, 3, 1, 7, 5]
2017年05月21日 08点05分 11
level 6
yuanqc8888 楼主
[大拇指][大拇指][大拇指]
2017年05月21日 09点05分 12
level 1
真的厉害
2018年03月21日 11点03分 13
1