PASCAL递归与回溯算法.ppt

PASCAL递归与回溯算法.ppt

递归与回溯算法 山东省实验中学 王乃广 判断(x,y)位置能否放数值k的函数 function ok(x,y,k:integer):boolean; begin ok:=true; if x1 then if not (p[b[x-1,y]+k]) then          ok:=false; if y1 then if not (p[b[x,y-1]+k]) then ok:=false; end; procedure try(x,y,dep:integer);//递归搜索(x,y)处放第 dep 个数 var i:integer; begin if dep=n*n+1 then print else //如果已放了n*n个数,得出一种方法 for i:=1 to n*n do if not(used[i]) and ok(x,y,i) then begin b[x,y]:=i; used[i]:=true; if y=n then try(x+1,1,dep+1) //如果当前是最右边一列,则转到下一行首列 else try(x,y+1,dep+1); //继续放当前行的下一列 used[i]:

文档评论(0)

1亿VIP精品文档

相关文档