読者です 読者をやめる 読者になる 読者になる

Sudoku(数独) を Mathematicaで解く(12): code5: HiddenSingles

目次へ

第9 回で HiddenSingleRow、第10回で HiddenSingleColumn、第9 回で HiddenSingleBox を説明した。今後これを使うことを考慮して、関数のまとめ作る。これを code5:HiddenSingle と呼ぶことにする。

(* code5: HiddenSigle  *)
(* == findHiddenSingleRow[_map] == *)
findHiddenSingleRow2[map_, nrow_] := 
 Module[{maptmpx, nums, pos, pos99},
  maptmpx = map[[nrow]];
  nums = Cases[Tally[Flatten[maptmpx]], {_, 1}][[All, 1]];
  pos = Flatten[(Position[maptmpx, #] & /@ nums), 1][[All, 1]];
  pos99 = {nrow, #} & /@ pos;
  {nums, pos99}
]

findHiddenSingleRow[map_] := Module[{tmp},
  tmp = DeleteCases[
    Table[findHiddenSingleRow2[map, nrow], {nrow, 1, 9}],
    {{}, {}}];
  If[tmp == {}, {{{}, {}}}, 
   Flatten[Function[x, MapThread[{#1, #2} &, x]] /@ tmp, 1]]
]

makeMatrixByHiddenSingleRow[ini_] := Module[{mapini, singles},
  mapini = makeMap[ini];
  singles = findHiddenSingleRow[mapini];
  rules = Map[Apply[Rule, Reverse[#]] &, singles];
  Fold[ReplacePart[#1, #2] &, ini, rules]
]

findHiddenSingleRowColor[map_] := Module[{tmp},
  tmp = findHiddenSingleRow[map];
  If[tmp == {{{}, {}}}, tmp, {{#[[1]]}, #[[2]]} & /@ tmp]
]

plotMapGridsColoredByHiddenSingleRow[ini_] := 
 Module[{mapini, delList, grids},
  mapini = makeMap[ini];
  delList = findHiddenSingleRowColor[mapini];
      grids = makeGridsColored[makeGrids[mapini], delList, "Yellow"];
  plotGridsColored[ini, grids]
]

(* == findHiddenSingleColumn[map_] == *)
findHiddenSingleColumn[map_, ncol_] := 
 Module[{maptmp, maptmpx, nums, pos, pos99},
  maptmp = Table[{}, {9}, {9}];
  maptmp = map;
  (maptmpx = maptmp[[All, ncol]];
   nums = Cases[Tally[Flatten[maptmpx]], {_, 1}][[All, 1]];
   pos = Flatten[(Position[maptmpx, #] & /@ nums), 1][[All, 1]];
   pos99 = {#, ncol} & /@ pos; 
   {nums, pos99})
]

makeMatrixByHiddenSingleColumn[ini_] := Module[{mapini, singles},
  mapini = makeMap[ini];
  singles = findHiddenSingleColumn[mapini];
  rules = Map[Apply[Rule, Reverse[#]] &, singles];
  Fold[ReplacePart[#1, #2] &, ini, rules]
]
  
findHiddenSingleColumn[map_] := Module[{tmp},
  tmp = DeleteCases[
    Table[findHiddenSingleColumn[map, ncol], {ncol, 1, 
      9}], {{}, {}}];
  If[tmp == {}, {{{}, {}}}, 
   Flatten[Function[x, MapThread[{#1, #2} &, x]] /@ tmp, 1]]
]

findHiddenSingleColumnColor[map_]:=Module[{tmp},
tmp=DeleteCases[Table[findHiddenSingleColumn[map,ncol],{ncol,1,9}],{{},{}}];
If[tmp=={},{{{},{}}},Flatten[Function[x,MapThread[{{#1},#2}&,x]]/@tmp,1]]
]

plotMapGridsColoredByHiddenSingleColumn[ini_] := 
 Module[{mapini, delList, grids},
  mapini = makeMap[ini];
  delList = findHiddenSingleColumnColor[mapini];
      grids = makeGridsColored[makeGrids[mapini], delList, "Yellow"];
  plotGridsColored[ini, grids]
]

(* == findHiddenSingleBox[map_] == *)
findHiddenSingleBox2[map_, nbox_] := 
 Module[{f, g, h, maptmp, maptmpx, nums, pos, pos99},
  (* begin{補助関数}  *)  
  f[i_]:= Which[1<=i<=3,1;;3,4<=i<=6,4;;6,7<=i<=9,7;;9];
  g[j_]:= Which[j==1||j==4||j==7,1;;3, 
                j==2||j==5||j==8,4;;6,
j==3||j==6||j==9,7;;9]; h[i_]:= Which[i==1,{0,0},i==2,{0,3},i==3,{0,6}, i==4,{3,0},i==5,{3,3},i==6,{3,6},
i==7,{6,0},i==8,{6,3},i==9,{6,6}]; (* end{補助関数} *) maptmp = Table[{}, {9}, {9}]; maptmp = map; (maptmpx = maptmp[[f[nbox], g[nbox]]]; nums = Cases[Tally[Flatten[maptmpx]],{_,1}][[All,1]]; pos = Flatten[(Position[maptmpx, #]&/@nums),1][[All,{1,2}]]; pos99 = (# + h[nbox])&/@pos; {nums, pos99}) ] findHiddenSingleBox[map_] := Module[{tmp}, tmp = DeleteCases[ Table[findHiddenSingleBox2[map, nbox], {nbox, 1, 9}], {{}, {}}]; If[tmp == {}, {{{}, {}}}, Flatten[Function[x, MapThread[{#1, #2} &, x]] /@ tmp, 1]] ] makeMatrixByHiddenSingleBox[ini_] := Module[{mapini, singles}, mapini = makeMap[ini]; singles = findHiddenSingleBox[mapini]; rules = Map[Apply[Rule, Reverse[#]] &, singles]; Fold[ReplacePart[#1, #2] &, ini, rules] ] findHiddenSingleBoxColor[map_] := Module[{tmp}, tmp = DeleteCases[ Table[findHiddenSingleBox2[map, nbox], {nbox, 1, 9}], {{}, {}}]; If[tmp == {}, {{{}, {}}}, Flatten[Function[x, MapThread[{{#1}, #2} &, x]] /@ tmp, 1]] ] plotMapGridsColoredByHiddenSingleBox[ini_]:=Module[{mapini,delList,grids}, mapini=makeMap[ini]; delList=findHiddenSingleBoxColor[mapini]; grids=makeGridsColored[makeGrids[mapini],delList,"Yellow"]; plotGridsColored[ini,grids] ]

次回は code5:HiddenSigle を組み込んで数独ゲームを解くことを行います。

目次へ