?? fenyou.cs
字號:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace FenYou
{
class FenYou
{
static void Main(string[] args)
{
Queue N = new Queue(); // 要訪問的結(jié)點(diǎn)
ArrayList NPass = new ArrayList(); //已經(jīng)訪問過的結(jié)點(diǎn)
CurrentState IinitialN = new CurrentState();//初始狀態(tài)
N.Enqueue(IinitialN);
Console.WriteLine("初始狀態(tài):(" + IinitialN.CA/10 + "," + (double)IinitialN.CB7/10 + "," + (double)IinitialN.CB3/10 + ")\n");
string output1, output2, answer; //output1輸出當(dāng)前已訪問節(jié)點(diǎn),output2輸出即將訪問節(jié)點(diǎn),answer為分油成功的步驟。
CurrentState firstnote = IinitialN;
while (firstnote.CA != 5&& firstnote.CB7 != 5)
{
//檢查節(jié)點(diǎn)是否已經(jīng)訪問過?
foreach (CurrentState i in NPass)
{
if (firstnote.CA == i.CA && firstnote.CB7 == i.CB7 && firstnote.CB3 == i.CB3)
{
firstnote = (CurrentState)N.Dequeue();
if (N.Count == 0)
return;
}
}
for (int k = 1; k < 7; k++) //節(jié)點(diǎn)展開
{
CurrentState m1 = null;
switch (k)
{
case 1:
if (firstnote.CA > 0) //用一斤瓶灌滿7兩瓶
{
CurrentState m = new CurrentState(firstnote.CA + firstnote.CB7 - 7, 7, firstnote.CB3, firstnote);
m1 = m;
}
break;
case 2:
if (firstnote.CA > 0) //用一斤瓶灌滿3兩瓶
{
CurrentState m = new CurrentState(firstnote.CA + firstnote.CB3 - 3, firstnote.CB7, 3, firstnote);
m1 = m;
}
break;
case 3:
if (firstnote.CB3 > 0) //用3兩瓶灌入一斤瓶
{
CurrentState m = new CurrentState(firstnote.CA + firstnote.CB3, firstnote.CB7, 0, firstnote);
m1 = m;
}
break;
case 4:
if (firstnote.CB7 > 0) //用7兩瓶灌入一斤瓶
{
CurrentState m = new CurrentState(firstnote.CA + firstnote.CB7, 0, firstnote.CB3, firstnote);
m1 = m;
}
break;
case 5:
if (firstnote.CB7 > 0 && firstnote.CB3 < 3) //用7兩瓶灌入3兩瓶
{
if ((firstnote.CB3 + firstnote.CB7) > 3)
{
CurrentState m = new CurrentState(firstnote.CA, firstnote.CB7 + firstnote.CB3 - 3, 3, firstnote);
m1 = m;
}
else
{
CurrentState m = new CurrentState(firstnote.CA, 0, firstnote.CB7+firstnote.CB3, firstnote);
m1 = m;
}
}
break;
case 6:
if (firstnote.CB7 < 7 && firstnote.CB3 > 0) //用3兩瓶灌入7兩瓶
{
if ((firstnote.CB3 + firstnote.CB7) > 7)
{
CurrentState m = new CurrentState(firstnote.CA, 7, firstnote.CB3 + firstnote.CB7 - 7, firstnote);
m1 = m;
}
else
{
CurrentState m = new CurrentState(firstnote.CA, firstnote.CB3 + firstnote.CB7, 0, firstnote);
m1 = m;
}
}
break;
}
if (m1 != null) //判斷是否出現(xiàn)答案,如果有,輸出結(jié)果。
{
N.Enqueue(m1);
if (m1.CA == 5 && m1.CB7 == 5)
{
answer = "Success:\n";
while (m1 != null)
{
answer +="("+(double)m1.CA/10+","+(double)m1.CB7/10+","+(double)m1.CB3/10+")\n";
//Console.WriteLine( answer);
m1 = m1.Father;
}
Console.WriteLine("分油答案:\n"+answer);
//distime.Stop();
//distime.ToString();
return;
}
}
}
CurrentState newnote = null;
newnote = firstnote;
NPass.Add(newnote);
firstnote = (CurrentState)N.Dequeue();
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -