📄 treearithmetic.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using DS.Tree;
namespace JiuGong
{
public class TreeArithmetic
{
public BinaryTree Tree = new BinaryTree();
public string Init = "";
public string Aim = "";
public ArrayList m_pScanbuf = new ArrayList();
public ArrayList BestPath = new ArrayList();
private int row = -1;
private int col = -1;
#region 声明属性
public int Row
{
get
{
return this.row;
}
set
{
this.row = value;
}
}
public int Col
{
get
{
return this.col;
}
set
{
this.col = value;
}
}
#endregion
public struct Scanbuf
{
public string Place;
public int ScanID;
}
public TreeArithmetic(string Init, string Aim, int Row, int Col)
{
this.Init = Init;
this.Aim = Aim;
this.Row = Row;
this.Col = Col;
}
//添加树中节点
//public bool AddTree(string place,TreeNode Node )
//{
// if (Node == null)
// {
// Node=new TreeNode(place);
// //刚开始没有加此,发生一直都返回false的情况
// return true;
// }
// if (Node.Item.ToString() == place)
// {
// return false;
// }
// if (Int32.Parse(Node.Item.ToString())> Int32.Parse(place))
// {
// return AddTree(place, Node.RightChild);
// }
// return AddTree(place, Node.LeftChild);
//}
public bool AddTree(string place, TreeNode node, TreeNode root)
{
if (root == null)
{
root = new TreeNode(place);
return true;
}
if (node.Item.ToString() == place)
{
return false;
}
if (Int32.Parse(node.Item.ToString()) > Int32.Parse(place))
{
if (node.LeftChild == null)
{
node.LeftChild = new TreeNode(place);
return true;
}
else
{
return AddTree(place, node.LeftChild, root);
}
}
else
{
if (node.RightChild == null)
{
node.RightChild = new TreeNode(place);
return true;
}
else
{
return AddTree(place, node.RightChild, root);
}
}
}
public void FreeTree(TreeNode root)
{
if (root.LeftChild != null)
{
FreeTree(root.LeftChild);
}
else if (root.RightChild != null)
{
FreeTree(root.RightChild);
}
else
{
root = null;
}
}
public bool ComputeFeel()
{
const int MAXSIZE = 362880;
string target = this.Aim, fountain = this.Init, parent = "";
int parentID = 0, child = 1;
string[] NextStates;
Scanbuf temp;
if (fountain == target)
{
return false;
}
if (m_pScanbuf.Count > 0)
{
m_pScanbuf.Clear();
}
this.Tree.Root = new TreeNode(fountain);
AddTree(fountain, Tree.Root, Tree.Root);
Scanbuf buf = new Scanbuf();
buf.Place = fountain;
buf.ScanID = -1;
m_pScanbuf.Add(buf);
while (parentID < MAXSIZE && child < MAXSIZE)
{
if (parentID < child)
{
temp = (Scanbuf)m_pScanbuf[parentID];
parent = temp.Place;
NextStates = GetNextStates(parent.ToString());
for (int i = 0; i < 4; i++)
{
if (NextStates[i] != "")
{
fountain = NextStates[i];
if (AddTree(fountain, Tree.Root, Tree.Root))
{
buf.Place = fountain;
buf.ScanID = parentID;
m_pScanbuf.Add(buf);
if (fountain == target)
{
GetPath(child);
m_pScanbuf.Clear();
FreeTree(this.Tree.Root);
return true;
}
child++;
}
}
}
}
parentID++;
}
m_pScanbuf.Clear();
FreeTree(this.Tree.Root);
this.BestPath.Clear();
return false;
}
#region 获取下一个可能状态
public string[] GetNextStates(string sInitState)
{
string[] NextStates ={ "", "", "", "" };
int i = 0;
//数字0在sInitState中的下标
int indexZero = sInitState.IndexOf('0');
char[] Array = sInitState.ToCharArray();
//此下标对应与假想的二维数组中的行、列坐标
int X = -1, Y = -1;
X = (int)indexZero / 3;
Y = indexZero % 3;
//将要移动的一个数字在sInitState中的下标
int indexMove = -1;
char c;
if (X != -1 && Y != -1)
{
//是否可以向左移动
if (Y < col - 1)
{
indexMove = X * 3 + (Y + 1);
c = Array[indexMove];
Array[indexMove] = '0';
Array[indexZero] = c;
NextStates[i] = new string(Array);
i++;
Array = sInitState.ToCharArray();
}
//是否可以向右移动
if (Y >= 1)
{
indexMove = X * 3 + (Y - 1);
c = Array[indexMove];
Array[indexMove] = '0';
Array[indexZero] = c;
NextStates[i] = new string(Array);
i++;
Array = sInitState.ToCharArray();
}
//是否可以向上移动
if (X < Row - 1)
{
indexMove = (X + 1) * 3 + Y;
c = Array[indexMove];
Array[indexMove] = '0';
Array[indexZero] = c;
NextStates[i] = new string(Array);
i++;
Array = sInitState.ToCharArray();
}
//是否可以向下移动
if (X > 0)
{
indexMove = (X - 1) * 3 + Y;
c = Array[indexMove];
Array[indexMove] = '0';
Array[indexZero] = c;
NextStates[i] = new string(Array);
i++;
Array = sInitState.ToCharArray();
}
}
return NextStates;
}
#endregion
public void GetPath(int child)
{
ArrayList Path = new ArrayList();
int ParentID = child;
Scanbuf temp;
while (ParentID != -1)
{
temp = (Scanbuf)m_pScanbuf[ParentID];
Path.Add(temp.Place.ToString());
ParentID = temp.ScanID;
}
Path.Reverse();
this.BestPath.Clear();
this.BestPath.AddRange(Path);
}
}
//
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -