public void DiagonalNumberPair(string name1, string name2, Dictionary <string, RDElement> dict, object factPool, Rule rule, Action <int> callBack, int callBackID) { FactPool pool = (FactPool)factPool; for (int x = 1; x <= pool.X; ++x) { for (int y = 1; y <= pool.Y; ++y) { if (IsUnfinishedNumberCell(x, y, factPool) && IsUnfinishedNumberCell(x + 1, y + 1, factPool)) { dict[name1] = new RDCell(x, y); dict[name2] = new RDCell(x + 1, y + 1); callBack.Invoke(callBackID); } if (IsUnfinishedNumberCell(x, y + 1, factPool) && IsUnfinishedNumberCell(x + 1, y, factPool)) { dict[name1] = new RDCell(x, y + 1); dict[name2] = new RDCell(x + 1, y); callBack.Invoke(callBackID); } } } dict.Remove(name1); dict.Remove(name2); }
public RDNumber ColorOf(RDEdge edge, object factPool) { FactPool pool = (FactPool)factPool; int[,] arr = edge.Direction ? pool.VLine : pool.HLine; return(new RDNumber(arr[edge.X, edge.Y])); }
public void OppositeCornerPair(string name1, string name2, Dictionary <string, RDElement> dict, object factPool, Rule rule, Action <int> callBack, int callBackID) { FactPool pool = (FactPool)factPool; for (int x = 0; x <= pool.X; ++x) { for (int y = 0; y <= pool.Y; ++y) { if (IsUnfinishedCorner(x + 1, y + 1, 0, 0, factPool) || IsUnfinishedCorner(x, y, 1, 1, factPool)) { dict[name1] = new RDCorner(x + 1, y + 1, 0, 0); dict[name2] = new RDCorner(x, y, 1, 1); callBack.Invoke(callBackID); dict[name2] = new RDCorner(x + 1, y + 1, 0, 0); dict[name1] = new RDCorner(x, y, 1, 1); callBack.Invoke(callBackID); } if (IsUnfinishedCorner(x + 1, y, 0, 1, factPool) || IsUnfinishedCorner(x, y + 1, 1, 0, factPool)) { dict[name1] = new RDCorner(x + 1, y, 0, 1); dict[name2] = new RDCorner(x, y + 1, 1, 0); callBack.Invoke(callBackID); dict[name2] = new RDCorner(x + 1, y, 0, 1); dict[name1] = new RDCorner(x, y + 1, 1, 0); callBack.Invoke(callBackID); } } } dict.Remove(name1); dict.Remove(name2); }
public void NeighborNumberPair(string name1, string name2, Dictionary <string, RDElement> dict, object factPool, Rule rule, Action <int> callBack, int callBackID) { FactPool pool = (FactPool)factPool; for (int x = 1; x <= pool.X; ++x) { for (int y = 1; y <= pool.Y; ++y) { if (IsUnfinishedNumberCell(x, y, factPool) && IsUnfinishedNumberCell(x, y + 1, factPool)) { dict[name1] = new RDCell(x, y); dict[name2] = new RDCell(x, y + 1); //Console.WriteLine(x + "," + y + ",y," + pool.Cell[x, y] + pool.Cell[x, y + 1]); callBack.Invoke(callBackID); dict[name1] = new RDCell(x, y + 1); dict[name2] = new RDCell(x, y); //Console.WriteLine(x + "," + y + ",y," + pool.Cell[x, y] + pool.Cell[x, y + 1]); callBack.Invoke(callBackID); } if (IsUnfinishedNumberCell(x, y, factPool) && IsUnfinishedNumberCell(x + 1, y, factPool)) { dict[name1] = new RDCell(x, y); dict[name2] = new RDCell(x + 1, y); //Console.WriteLine(x + "," + y + ",x," + pool.Cell[x, y] + pool.Cell[x + 1, y]); callBack.Invoke(callBackID); dict[name1] = new RDCell(x + 1, y); dict[name2] = new RDCell(x, y); //Console.WriteLine(x + "," + y + ",x," + pool.Cell[x, y] + pool.Cell[x + 1, y]); callBack.Invoke(callBackID); } } } dict.Remove(name1); dict.Remove(name2); }
public void NumberCorner(string name, Dictionary <string, RDElement> dict, object factPool, Rule rule, Action <int> callBack, int callBackID) { FactPool pool = (FactPool)factPool; for (int x = 1; x <= pool.X; ++x) { for (int y = 1; y <= pool.Y; ++y) { if (IsUnfinishedNumberCell(x, y, factPool)) { for (int dx = 0; dx <= 1; ++dx) { for (int dy = 0; dy <= 1; ++dy) { if (IsUnfinishedCorner(x, y, dx, dy, factPool)) { dict[name] = new RDCorner(x, y, dx, dy); callBack.Invoke(callBackID); } } } } } } dict.Remove(name); }
private bool IsUnfinishedNumberCell(int x, int y, object factPool) { FactPool pool = (FactPool)factPool; if (y > pool.Y || x > pool.X) { return(false); } return(pool.Cell[x, y] != -1 && (pool.HLine[x, y] == 0 || pool.VLine[x, y] == 0 || pool.HLine[x + 1, y] == 0 || pool.VLine[x, y + 1] == 0)); }
private int GetEdgeState(RDEdge edge, object factPool) { FactPool pool = (FactPool)factPool; if (edge.Direction == false) { return(pool.HLine[edge.X, edge.Y]); } else { return(pool.VLine[edge.X, edge.Y]); } }
public bool ColorOf(RDEdge edge, RDNumber result, object factPool) { //Console.WriteLine(edge.X + "::" + edge.Y); FactPool pool = (FactPool)factPool; int[,] arr = edge.Direction ? pool.VLine : pool.HLine; if (arr[edge.X, edge.Y] != 0) { if (arr[edge.X, edge.Y] != result.Data) { throw new Exception("结果冲突!"); } return(false); } arr[edge.X, edge.Y] = result.Data; return(true); }
public void GraphCut(string name, Dictionary <string, RDElement> dict, object factPool, Rule rule, Action <int> callBack, int callBackID) { FactPool pool = (FactPool)factPool; Queue <KeyValuePair <int, int> > myq = new Queue <KeyValuePair <int, int> >(); bool[,] inq = new bool[pool.X + 2, pool.Y + 2]; KeyValuePair <int, int>[,] parents = new KeyValuePair <int, int> [pool.X + 2, pool.Y + 2]; inq[0, 0] = true; parents[0, 0] = new KeyValuePair <int, int>(-1, -1); myq.Enqueue(new KeyValuePair <int, int>(0, 0)); while (myq.Count > 0) { KeyValuePair <int, int> kv = myq.Dequeue(); int x = kv.Key, y = kv.Value; for (int d = 0; d < 4; ++d) { int tx = x + dirX[d], ty = y + dirY[d]; if (tx >= 0 && ty >= 0 && tx <= pool.X + 1 && ty <= pool.Y + 1 && !inq[tx, ty]) { if (GetEdgeState(GetEdgeBetweenCell(x, y, tx, ty), factPool) > 0) { inq[tx, ty] = true; parents[tx, ty] = kv; myq.Enqueue(new KeyValuePair <int, int>(tx, ty)); } } } } for (int x = 0; x <= pool.X; ++x) { for (int y = 0; y <= pool.Y; ++y) { if (!inq[x, y]) { continue; } for (int d = 0; d < 2; ++d) { int tx = x + dirX[d], ty = y + dirY[d]; if (!inq[tx, ty]) { continue; } if (GetEdgeState(GetEdgeBetweenCell(x, y, tx, ty), factPool) == 0) { RDList list = new RDList(); list.Data.Add(GetEdgeBetweenCell(x, y, tx, ty)); while (parents[x, y].Key != -1) { list.Data.Add(GetEdgeBetweenCell(x, y, parents[x, y].Key, parents[x, y].Value)); KeyValuePair <int, int> kv = parents[x, y]; x = kv.Key; y = kv.Value; } while (parents[tx, ty].Key != -1) { list.Data.Add(GetEdgeBetweenCell(tx, ty, parents[tx, ty].Key, parents[tx, ty].Value)); KeyValuePair <int, int> kv = parents[tx, ty]; tx = kv.Key; ty = kv.Value; } dict[name] = list; //Console.WriteLine("Let's See:"); //Console.WriteLine(list); callBack.Invoke(callBackID); // Data has been changed, return directly dict.Remove(name); return; } } } } }
private bool IsUnfinishedCorner(int x, int y, int dx, int dy, object factPool) { FactPool pool = (FactPool)factPool; return(pool.HLine[x + dx, y] == 0 || pool.VLine[x, y + dy] == 0); }
public void Rope(string name, Dictionary <string, RDElement> dict, object factPool, Rule rule, Action <int> callBack, int callBackID) { FactPool pool = (FactPool)factPool; bool[,] visit = new bool[pool.X + 2, pool.Y + 2]; int total1Count = 0; for (int x = 1; x <= pool.X + 1; ++x) { for (int y = 1; y <= pool.Y + 1; ++y) { if (!visit[x, y]) { int totalEdges = 0; for (int d = 0; d < 4; ++d) { int tx = x + dirX[d], ty = y + dirY[d]; if (GetEdgeState(GetEdgeBetweenVertex(x, y, tx, ty), factPool) == 1) { ++totalEdges; } } if (totalEdges == 1) { total1Count++; } } } } if (total1Count <= 2) { return; // Maybe a single roop } for (int x = 1; x <= pool.X + 1; ++x) { for (int y = 1; y <= pool.Y + 1; ++y) { if (!visit[x, y]) { int totalEdges = 0; for (int d = 0; d < 4; ++d) { int tx = x + dirX[d], ty = y + dirY[d]; if (GetEdgeState(GetEdgeBetweenVertex(x, y, tx, ty), factPool) == 1) { ++totalEdges; } } if (totalEdges == 1) { visit[x, y] = true; RDList list = new RDList(); int lastX = -2, lastY = -2, tx = -2, ty = -2, cx = x, cy = y; bool end; while (true) { end = true; for (int d = 0; d < 4; ++d) { tx = cx + dirX[d]; ty = cy + dirY[d]; if (GetEdgeState(GetEdgeBetweenVertex(cx, cy, tx, ty), factPool) == 1 && (tx != lastX || ty != lastY)) { list.Data.Add(GetEdgeBetweenVertex(cx, cy, tx, ty)); end = false; break; } } if (end) { break; } lastX = cx; lastY = cy; cx = tx; cy = ty; if (visit[cx, cy])// Error! { return; } visit[cx, cy] = true; } //Console.WriteLine(list.Data.First() + "-" + list.Data.Last()); dict[name] = list; callBack.Invoke(callBackID); } } } } dict.Remove(name); //Console.WriteLine("End"); }