public bool backtrack0(List <UnpaintPoint> G, UnpaintPoint P, List <List <char> > All, ref List <List <char> > Paint, ref List <List <char> > FinalOutput, int num) { List <List <char> > re = new List <List <char> >(All); if (ProbeG(P, '0', re)) { for (int i = 0; i < List0[P.Row, P.Col].Count; i++) { if (rowAll[List0[P.Row, P.Col][i].Row][List0[P.Row, P.Col][i].Col] == 'u') { re[List0[P.Row, P.Col][i].Row][List0[P.Row, P.Col][i].Col] = List0[P.Row, P.Col][i].Point1; } } re[P.Row][P.Col] = '0'; re = PropagateOwn(re); bool reX = backtrack(G, re, ref Paint, ref FinalOutput, num + 1); if (reX) { Paint = re; return(true); } else { return(false); } } else { return(false); } }
//傳點進入看能否填入 private bool Probe(UnpaintPoint point) { bool Probe0 = ProbeG(point, '0', rowAll); bool Probe1 = ProbeG(point, '1', rowAll); //沒辦法找到解 if (!Probe0 && !Probe1) { return(false); } else if (Probe0 && Probe1) { rowAll = PropagateMatrix(point); /* Console.Write("成功填入的點 座標為:"); * Console.WriteLine(point.Row + 1 + " " + (point.Col + 1) + " 填入的點為" + rowAll[point.Row][point.Col] + " 兩個都能填的情況");*/ return(false); } else if (Probe1) { List <int> re = new List <int>(); for (int i = 0; i < List1[point.Row, point.Col].Count; i++) { if (rowAll[List1[point.Row, point.Col][i].Row][List1[point.Row, point.Col][i].Col] == 'u') { rowAll[List1[point.Row, point.Col][i].Row][List1[point.Row, point.Col][i].Col] = List1[point.Row, point.Col][i].Point1; re.Add(i); } } for (int i = 0; i < re.Count; i++) { List1[point.Row, point.Col].RemoveAt(i); } rowAll[point.Row][point.Col] = '1'; return(true); } else if (Probe0) { List <int> re = new List <int>(); for (int i = 0; i < List0[point.Row, point.Col].Count; i++) { if (rowAll[List0[point.Row, point.Col][i].Row][List0[point.Row, point.Col][i].Col] == 'u') { rowAll[List0[point.Row, point.Col][i].Row][List0[point.Row, point.Col][i].Col] = List0[point.Row, point.Col][i].Point1; } } for (int i = 0; i < re.Count; i++) { List0[point.Row, point.Col].RemoveAt(i); } rowAll[point.Row][point.Col] = '0'; return(true); } else { return(false); } }
private double m(UnpaintPoint point, char num, List <List <char> > reAll) { List <List <char> > re = new List <List <char> >(Copy(reAll)); re[point.Row][point.Col] = num; re = PropagateOwn(re); List <Point> reP = ComparePoint(re, reAll); return(Convert.ToDouble(reP.Count)); }
//判斷能否填入點 private bool ProbeG(UnpaintPoint point, char num, List <List <char> > all) { //假設棋盤 List <List <char> > reAll = new List <List <char> >(Copy(all)); //填入num的部分 reAll[point.Row][point.Col] = num; List <char> re = new List <char>(reAll[point.Row]); bool paintRow = Fix(25, Program.row[point.Row].Count, reAll[point.Row], Program.row[point.Row], ref re); if (!paintRow) { return(false); } //轉置 reAll = OwnChangeMatrix(reAll); re = new List <char>(reAll[point.Col]); bool paintCol = Fix(25, Program.col[point.Col].Count, reAll[point.Col], Program.col[point.Col], ref re); //再轉回 reAll = OwnChangeMatrix(reAll); if (!paintCol) { return(false); } else { reAll = PropagateOwn(reAll); List <Point> reList = ComparePoint(all, reAll); for (int i = 0; i < reList.Count; i++) { if (reList[i].Row != point.Row && reList[i].Col != point.Col) { if (reList[i].Point1 == '0') { List1[reList[i].Row, reList[i].Col].Add(new Point(point.Row, point.Col, Inverse(num))); } else if (reList[i].Point1 == '1') { List0[reList[i].Row, reList[i].Col].Add(new Point(point.Row, point.Col, Inverse(num))); } } } return(true); } }
private double Vmin(UnpaintPoint p, List <List <char> > reAll) { double r0 = m(p, '0', reAll); double r1 = m(p, '1', reAll); if (r0 > r1) { return(r1); } else { return(r0); } }
//自訂盤面不塗顏色的地方 public List <UnpaintPoint> AllUnpaintPoint(List <List <char> > Own) { List <UnpaintPoint> reG = new List <UnpaintPoint>(); for (int i = 0; i < 25; i++) { for (int j = 0; j < 25; j++) { if (Own[i][j] == 'u') { UnpaintPoint re = new UnpaintPoint(i, j); reG.Add(re); } } } return(reG); }
//比對兩邊Matrix private List <List <char> > PropagateMatrix(UnpaintPoint point) { List <List <char> > put0 = new List <List <char> >(Copy(rowAll)); List <List <char> > put1 = new List <List <char> >(Copy(rowAll)); List <List <char> > output = new List <List <char> >(); put0[point.Row][point.Col] = '0'; put1[point.Row][point.Col] = '1'; put0 = PropagateOwn(put0); put1 = PropagateOwn(put1); for (int i = 0; i < 25; i++) { List <char> re = match(put0[i], put1[i], 25); output.Add(re); } return(output); }
private double Vlog(UnpaintPoint point, char num, List <List <char> > reAll) { return(Math.Log(1 + m(point, num, reAll)) / Math.Log(10) + 1); }