### 题面

“迷宫”题：从图左边入口处的2011进去，在迷宫里转悠，最后变成2012从右边出来。可以在迷宫里转圈，可以重复之前走过的路，但不能回退。

### 基本思路

Current Depth: 27
Expression Path: 2011＋7>2018÷2>1009＋7>1016÷2>508＋7>515－5>510×3>1530÷2>765＋7>772÷2>386＋7>393×3>1179－5>1174÷2>587＋7>594÷2>297＋7>304－5>299×3>897－5>892×3>2676÷2>1338＋7>1345－5>1340×3>4020÷2>2010＋7>2017－5>2012

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConApp
{
class Program
{
static void Main(string[] args)
{
CommonComputeNode exitNode = PuzzleFrame.TryCompute(PuzzleFrame.GetOrigin());
if (exitNode != null)
{
Console.WriteLine("Current Depth: {0}", exitNode.Depth);

List<CommonComputeNode> path = exitNode.GetPath();
List<string> expressionList = path.Select<CommonComputeNode, string>(node => node.Expression).ToList();
string expressionPathString = string.Join(">", expressionList.ToArray());

Console.WriteLine("Expression Path: {0}>{1}", expressionPathString, PuzzleFrame.DesiredValue);
}
else
{
}

}
}

#region PuzzleGame.

internal static class PuzzleFrame
{
internal static int OriginalValue = 2011;
internal static int DesiredValue = 2012;
internal static int MaxDepth = 30;

internal static CommonComputeNode GetOrigin()
{
return ComputeNodeFactory.GetPlus7(OriginalValue, ClockRotationDirection.Clockwise);
}

internal static CommonComputeNode TryCompute(CommonComputeNode node)
{
if (!node.CanCompute
|| HasExceededMaxDepath(node.Depth))
return null;

node.Compute();

if (node.CanGameOver())
return node;

CommonComputeNode returnNode = null;
node.AppendChildNodes();
foreach (CommonComputeNode childNode in node.ChildNodes)
{
returnNode = TryCompute(childNode);
if (returnNode != null)
break;
}
return returnNode;
}

private static bool HasExceededMaxDepath(int nodeDepth)
{
return nodeDepth > MaxDepth;
}

private static bool CanGameOver(this CommonComputeNode node)
{
return DesiredValue.Equals(node.Result) && IsExitNode(node);
}
private static bool IsExitNode(CommonComputeNode node)
{
return IsClockwiseMultipliedBy3(node) || IsCounterclockwiseMinus5(node);
}
private static bool IsCounterclockwiseMinus5(CommonComputeNode node)
{
return (node.OperatorWithRightOperandSign.Equals("－5") && node.RotationDirection.Equals(ClockRotationDirection.Counterclockwise));
}
private static bool IsClockwiseMultipliedBy3(CommonComputeNode node)
{
return (node.OperatorWithRightOperandSign.Equals("×3") && node.RotationDirection.Equals(ClockRotationDirection.Clockwise));
}
}
internal static class ComputeNodeFactory
{
internal static CommonComputeNode GetPlus7(int inputValue, ClockRotationDirection direction)
{
return new Plus7(inputValue, direction);
}

internal static void AppendChildNodes(this CommonComputeNode node)
{
switch (node.OperatorWithRightOperandSign)
{
case "＋7":
AppendPlus7ChildNodes(node);
break;
case "÷2":
AppendDividedBy2ChildNodes(node);
break;
case "×3":
AppendMultipliedBy3ChildNodes(node);
break;
case "－5":
AppendMinus5ChildNodes(node);
break;
}
}
private static void AppendMinus5ChildNodes(CommonComputeNode node)
{
switch (node.RotationDirection)
{
case ClockRotationDirection.Clockwise:
break;
case ClockRotationDirection.Counterclockwise:
break;
}
}
private static void AppendMultipliedBy3ChildNodes(CommonComputeNode node)
{
switch (node.RotationDirection)
{
case ClockRotationDirection.Clockwise:
break;
case ClockRotationDirection.Counterclockwise:
break;
}
}
private static void AppendDividedBy2ChildNodes(CommonComputeNode node)
{
switch (node.RotationDirection)
{
case ClockRotationDirection.Clockwise:
break;
case ClockRotationDirection.Counterclockwise:
break;
}
}
private static void AppendPlus7ChildNodes(CommonComputeNode node)
{
switch (node.RotationDirection)
{
case ClockRotationDirection.Clockwise:
break;
case ClockRotationDirection.Counterclockwise:
break;
}
}
}
public abstract class CommonComputeNode
{
public abstract string OperatorWithRightOperandSign { get; }
public abstract int Compute();

public int LeftOperand { get; private set; }
public int Depth
{
get { return (ParentNode == null) ? 0 : (ParentNode.Depth + 1); }
}
public virtual bool CanCompute
{
get { return true; }
}
public string Expression
{
get { return string.Format("{0}{1}", LeftOperand, OperatorWithRightOperandSign); }
}
public ClockRotationDirection RotationDirection { get; private set; }
public int? Result { get; protected set; }
public CommonComputeNode ParentNode { get; private set; }
public List<CommonComputeNode> ChildNodes { get; private set; }

protected CommonComputeNode(int leftOperandValue, ClockRotationDirection clockDirection)
{
LeftOperand = leftOperandValue;
RotationDirection = clockDirection;
ChildNodes = new List<CommonComputeNode>();
}

{
childNode.ParentNode = this;

return this;
}
public List<CommonComputeNode> GetPath()
{
List<CommonComputeNode> path = new List<CommonComputeNode>();

CommonComputeNode node = this;
do
{
node = node.ParentNode;
} while (node != null);
path.Reverse();

return path;
}
}
public class Plus7 : CommonComputeNode
{
public override string OperatorWithRightOperandSign
{
get { return "＋7"; }
}

public Plus7(int inputValue, ClockRotationDirection direction)
: base(inputValue, direction) { }

public override int Compute()
{
Result = LeftOperand + 7;
return Result.Value;
}
}
public class DividedBy2 : CommonComputeNode
{
public override string OperatorWithRightOperandSign
{
get { return "÷2"; }
}

public DividedBy2(int inputValue, ClockRotationDirection direction)
: base(inputValue, direction) { }

public override int Compute()
{
Result = LeftOperand / 2;
return Result.Value;
}

public override bool CanCompute
{
get { return IsDivisibleBy2(); }
}
private bool IsDivisibleBy2()
{
return (LeftOperand % 2).Equals(0);
}
}
public class MultipliedBy3 : CommonComputeNode
{
public override string OperatorWithRightOperandSign
{
get { return "×3"; }
}

public MultipliedBy3(int inputValue, ClockRotationDirection direction)
: base(inputValue, direction) { }

public override int Compute()
{
Result = LeftOperand * 3;
return Result.Value;
}
}
public class Minus5 : CommonComputeNode
{
public override string OperatorWithRightOperandSign
{
get { return "－5"; }
}

public Minus5(int inputValue, ClockRotationDirection direction)
: base(inputValue, direction) { }

public override int Compute()
{
Result = LeftOperand - 5;
return Result.Value;
}
}

public enum ClockRotationDirection
{
Clockwise = 0,
Counterclockwise = 1
}

#endregion
}
``````

### 参赛结果

（因为俺会进一步改善代码质量，提升执行速度 :D）