用pascal写了段..贴出来献丑啦...
穷举搜索..很简单吧? 懒的做优化了..反正密码风暴里的搜索量又不大..
这个是给出所有可行解的.只要一个的话自己改一改..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | const sort:array[1..9]of longint=(1,1,1,2,2,2,3,3,3); var map:array[1..9,1..9]of longint; visited:array[1..9,1..9]of boolean; used1,used2:array[1..9,1..9]of boolean; usedp:array[1..3,1..3,1..9]of boolean; procedure init; var i,j:longint; begin fillchar(map,sizeof(map),0); fillchar(visited,sizeof(visited),0); fillchar(used1,sizeof(used1),0); fillchar(used2,sizeof(used2),0); fillchar(usedp,sizeof(usedp),0); for i:=1 to 9 do begin for j:=1 to 9 do begin read(map[i,j]); if map[i,j]>0 then begin visited[i,j]:=true; used1[i,map[i,j]]:=true; used2[j,map[i,j]]:=true; usedp[sort[i],sort[j],map[i,j]]:=true; end; end; readln; end; end; procedure print; var i,j:longint; begin for i:=1 to 9 do begin for j:=1 to 9 do write(map[i,j],' '); writeln; end; writeln; end; procedure dfs(x,y:longint); var i:longint; begin if visited[x,y] then begin if (x=9) and (y=9) then print; if y=9 then dfs(x+1,1) else dfs(x,y+1); exit; end; for i:=1 to 9 do begin if used1[x,i] or used2[y,i] or usedp[sort[x],sort[y],i] then continue; map[x,y]:=i; used1[x,i]:=true; used2[y,i]:=true; usedp[sort[x],sort[y],i]:=true; if (x=9)and(y=9) then print; if y=9 then dfs(x+1,1) else dfs(x,y+1); map[x,y]:=0; used1[x,i]:=false; used2[y,i]:=false; usedp[sort[x],sort[y],i]:=false; end; end; begin assign(input,'data.txt'); reset(input); assign(output,'result.txt'); rewrite(output); init; dfs(1,1); close(input); close(output); end. |

老先生我搞不懂啊 自惭形秽啊
中国的未来还是输入你们年轻人啊
哎……