public static int getNextBlockIndex() { var pIndex = -1; if (allSolutionStack.Count == 0) { pIndex = 0; } else { PuzzleResultInfo pri = (PuzzleResultInfo)allSolutionStack.Peek(); pIndex = getPIndex(pri.blockIndex); if (pIndex == -1) { return(-1); } pIndex++; } if (pIndex >= piorityIndex.Length) { return(-1); } PuzzleResult pr = (PuzzleResult)results[piorityIndex[pIndex]]; if (pr.useFlag == true) { pIndex++; } if (pIndex >= piorityIndex.Length) { return(-1); } return(piorityIndex[pIndex]); }
public static void findAllSolutions() { var blockIndex = getNextBlockIndex(); //var findFlag = false; if(blockIndex == -1) return; PuzzleResult pr = (PuzzleResult)results[blockIndex]; for(var posIndex=0; posIndex<pr.result.Length; posIndex++) { if(checkPos(blockIndex, posIndex) == true) { //findFlag = true; var posArray = results[blockIndex].result[posIndex].pos; //Add to stack and mark on map PuzzleResultInfo pri = new PuzzleResultInfo(); pri.blockIndex = blockIndex; pri.posIndex = posIndex; allSolutionStack.Push(pri); addPosArrayToMap(posArray); //Add to check map(for overlap) addPosArrayToGrid(posArray); //Add to check grid(for isolate grid) if(allSolutionStack.Count == 11) { //Add to solution list //allSolutionList.Add(allSolutionStack); //Error: not deep copy //将对象转化成Json字符串 PuzzleResultInfo[] pris = (PuzzleResultInfo[])allSolutionStack.ToArray<PuzzleResultInfo>(); using (MemoryStream ms = new MemoryStream()) { GridTest.ds.WriteObject(ms, pris); string resultStr = Encoding.UTF8.GetString(ms.ToArray()); //Console.WriteLine(output); allSolutionList.Add(resultStr); } //drawOneSolution(allSolutionStack); //console.log("Find one"); //Rmove and clear map allSolutionStack.Pop(); removePosArrayFromMap(posArray);//Remove from check map(for overlap) removePosArrayFromGrid(posArray); //Remove from check grid(for isolate grid) break; } else { findAllSolutions(); } //Remove and clear map allSolutionStack.Pop(); removePosArrayFromMap(posArray);//Remove from check map(for overlap) removePosArrayFromGrid(posArray); //Remove from check grid(for isolate grid) } } return; }
public static void findAllSolutions() { var blockIndex = getNextBlockIndex(); //var findFlag = false; if (blockIndex == -1) { return; } PuzzleResult pr = (PuzzleResult)results[blockIndex]; for (var posIndex = 0; posIndex < pr.result.Length; posIndex++) { if (checkPos(blockIndex, posIndex) == true) { //findFlag = true; var posArray = results[blockIndex].result[posIndex].pos; //Add to stack and mark on map PuzzleResultInfo pri = new PuzzleResultInfo(); pri.blockIndex = blockIndex; pri.posIndex = posIndex; allSolutionStack.Push(pri); addPosArrayToMap(posArray); //Add to check map(for overlap) addPosArrayToGrid(posArray); //Add to check grid(for isolate grid) if (allSolutionStack.Count == 11) { //Add to solution list //allSolutionList.Add(allSolutionStack); //Error: not deep copy //将对象转化成Json字符串 PuzzleResultInfo[] pris = (PuzzleResultInfo[])allSolutionStack.ToArray <PuzzleResultInfo>(); using (MemoryStream ms = new MemoryStream()) { GridTest.ds.WriteObject(ms, pris); string resultStr = Encoding.UTF8.GetString(ms.ToArray()); //Console.WriteLine(output); allSolutionList.Add(resultStr); } //drawOneSolution(allSolutionStack); //console.log("Find one"); //Rmove and clear map allSolutionStack.Pop(); removePosArrayFromMap(posArray); //Remove from check map(for overlap) removePosArrayFromGrid(posArray); //Remove from check grid(for isolate grid) break; } else { findAllSolutions(); } //Remove and clear map allSolutionStack.Pop(); removePosArrayFromMap(posArray); //Remove from check map(for overlap) removePosArrayFromGrid(posArray); //Remove from check grid(for isolate grid) } } return; }