本文整理汇总了C#中Stack.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Equals方法的具体用法?C# Stack.Equals怎么用?C# Stack.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Equals方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BruteForce
/*-----------------------Brute Force Algorithm-------------------*/
/*
* This method solves a Sudoku puzzle by finding the first empty cell
* and putting the first number it finds can be inserted. It then puts
* the new grid into a stack and moves on. It continues until it finds
* a solution or until the stack is empty.
*
* If true was passed into the method then it will solve with Heuristics.
* The Heuristics will find numbers that will only go in one cell and then
* add them in. After this can't find a number that only goes in one place
* it will move on to the brute force method.
*/
public int[,] BruteForce(bool heuristics)
{
var pushCounter = 1; //Counts how many times the grid gets pushed
var stack = new Stack<int[,]>();
stack.Push(_board);
var solved = false;
var found = false; //no empty cell, or dead end
while (!stack.Equals(new Stack<int[,]>()) && !solved)
//loops until solution is fount or the stack is empty
{
_board = stack.Pop(); //pops off the stack for next loop
if (heuristics) Heuristics2(_board); //does heuristics if boolean is true
for (var x = 0; x < 9 && !found; x++) //increments x value
{
for (var y = 0; y < 9 && !found; y++) //increments y value
{
if (_board[x, y] == 0) //if it's an empty cell then call numberAt method
{
found = true;
for (var num = 1; num < 10; num++) //loops through numbers 1-9 for numberAt method call
if (NumberAt(x, y, num)) //if a number can go in the cell grid, copy grid,
{
//put in potential answer, and push copy into stack
var gridB = CopyBoard();
gridB[x, y] = num;
stack.Push(gridB);
pushCounter += 1;
}
}
}
}
if (found) found = false;
else solved = true;
}
Console.WriteLine("\nSteps Taken : " + pushCounter);
return _board;
}
示例2: LeastDigitChoice
/*---------------------- Heuristic Method -------------------------------*/
/*
* This method solves a Sudoku grid by first trying to find a number that
* has the least amount of possibilities to go into a cell. It then
* puts that number in the cell and moves on. If it gets stuck it falls back
* to a brute force method.
*/
public int[,] LeastDigitChoice(bool heuristic)
{
var pushCounter = 1; //Counts how many times the grid gets pushed
var stack = new Stack<int[,]>();
stack.Push(_board);
var solved = false;
var found = false; //no empty cell, or dead end
while (!stack.Equals(new Stack<int[,]>()) && !solved)
//loops until solution is found or the stack is empty
{
_board = stack.Pop(); //pops off the stack for next loop
if (heuristic) Heuristics2(_board); //does heuristics if boolean is true
var rowBestCell = 0;
var colBestCell = 0;
var minChoices = 9;
for (var i = 0; i < 9; i++) //increments x value
{
for (var j = 0; j < 9; j++) //increments y value
{
if (_board[i, j] == 0) //if it's an empty cell then call numberAt method
{
found = true;
var choices = 0;
for (var number = 1; number < 10; number++)
if (NumberAt(i, j, number))
{
choices++;
}
if (choices < minChoices)
{
minChoices = choices;
rowBestCell = i;
colBestCell = j;
}
}
}
}
for (var num = 1; num < 10; num++) //loops through numbers 1-9 for numberAt method call
if (NumberAt(rowBestCell, colBestCell, num)) //if a number can go in the cell grid, copy grid,
{
//put in potential answer, and push copy into stack
var gridB = CopyBoard();
gridB[rowBestCell, colBestCell] = num;
stack.Push(gridB);
pushCounter += 1;
}
if (found) found = false;
else solved = true;
}
Console.WriteLine("Push Counter : " + pushCounter);
return _board;
}
示例3: Main
static void Main(string[] args)
{
string filename ;
// ensure arguments is correct
if (args.Length != 1)
{
System.Console.WriteLine("RenderStroke2: <filename>");
System.Console.WriteLine();
System.Console.WriteLine("Instructions:");
System.Console.WriteLine();
System.Console.WriteLine("1. To locate Kinematic Templates log files, create and save a drawing.");
System.Console.WriteLine("2. Open the File menu and select Developer Options.");
System.Console.WriteLine("3. Copy the text in \"Log Directory\" and paste into the dialog box here.");
System.Console.WriteLine("4. Select the log file of a previous drawing you created.");
// show dialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.CheckFileExists = true;
dlg.Filter = "Kinematic Template Log Files (*.log)|*.log";
dlg.Title = "Open Log File";
bool? ret = dlg.ShowDialog();
if (ret.HasValue)
{
if (ret.Value == true)
{
filename = dlg.FileName;
}
else
return;
}
else
{
return;
}
}
else
{
filename = args[0];
}
// load file into a queue
int c = 0;
Queue<SupportedString> lines = new Queue<SupportedString>();
try
{
using (TextReader reader = new StreamReader(filename))
{
string line = reader.ReadLine();
while (line != null)
{
c++;
lines.Enqueue(new SupportedString() { Line = line, LineNumber = c });
line = reader.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Cannot read the log file: " + ex.Message);
Environment.Exit(1);
}
// quit if empty
if (lines.Count == 0)
{
System.Console.WriteLine("RenderStroke2: log file is empty");
return;
}
ThreadStart threadParam = new ThreadStart(() =>
{
// app state
AppState s = new AppState()
{ ViewBox = "",
PenType = PenType.Ink,
Templates = new Dictionary<TemplateID,TemplateLog>(),
EraserSize = 16};
// split up lines
string[] split = null;
// place to put all the stuff
Queue<Stroke> strokes = new Queue<Stroke>();
Stack<Stroke> undoStack = new Stack<Stroke>();
Stack<Stroke> redoStack = new Stack<Stroke>();
while (lines.Count > 0)
{
split = lines.Peek().Line.Split('/');
// do some processing here
if ("viewbox".Equals(split[ACTION]))
s.ViewBox = split[OTHER];
else if ("pen".Equals(split[ACTION]) || "use_pen".Equals(split[ACTION])
|| "goto_draw_mode".Equals(split[ACTION]))
s.PenType = PenType.Ink;
//.........这里部分代码省略.........