static Seat data = new Seat();
public static void Run()
{
for (int a = 0; a < 4; a++)
{
if (data.IsSelected(0, a)) //第1局编号0。如果已经被人坐了。
continue;
data.Selected(0, a, "A"); //第1局编号0。A坐a椅。
for (int b = 0; b < 4; b++)
{
if (data.IsSelected(0, b))
continue;
data.Selected(0, b, "B");
for (int c = 0; c < 4; c++)
{
if (data.IsSelected(0, c))
continue;
data.Selected(0, c, "C");
for (int d = 0; d < 4; d++)
{
if (data.IsSelected(0, d))
continue;
data.Selected(0, d, "D");
for (int a2 = 0; a2 < 5; a2++)
{
if (a2 == 1)
continue;
if (data.IsSelected(1, a2)) //第2局编号1
continue;
if (data.IsSelected(0, a2, "A")) //如果第1局A坐了a2椅
continue;
data.Selected(1, a2, "A");
for (int b2 = 0; b2 < 5; b2++)
{
if (b2 == 1)
continue;
if (data.IsSelected(1, b2))
continue;
if (data.IsSelected(0, b2, "B"))
continue;
data.Selected(1, b2, "B");
for (int c2 = 0; c2 < 5; c2++)
{
if (c2 == 1)
continue;
if (data.IsSelected(1, c2))
continue;
if (data.IsSelected(0, c2, "C"))
continue;
data.Selected(1, c2, "C");
for (int d2 = 0; d2 < 5; d2++)
{
if (d2 == 1)
continue;
if (data.IsSelected(1, d2))
continue;
if (data.IsSelected(0, d2, "D"))
continue;
data.Selected(1, d2, "D");
data.Count++; //可能的情况数加1
Console.WriteLine("{0,5} {1}", data.Count, data.Current);
data.UnSelected(1, d2);
}
data.UnSelected(1, c2);
}
data.UnSelected(1, b2);
}
data.UnSelected(1, a2);
}
data.UnSelected(0, d);
}
data.UnSelected(0, c);
}
data.UnSelected(0, b);
}
data.UnSelected(0, a); //A起身(释放坐椅)
}
}
obj.Try("A").Try("B").Try("C").Try("D").Try2("A").Try2("B").Try2("C").Try2("D").Write();
public static void Run2()
{
Try("A",
() => Try("B",
() => Try("C",
() => Try("D",
() => Try2("A",
() => Try2("B",
() => Try2("C",
() => Try2("D",
null
)
)
)
)
)
)
)
);
}
public static void Try(string name, Action action) //第1局
{
for (int i = 0; i < 4; i++)
{
if (data.IsSelected(0, i))
continue;
data.Selected(0, i, name);
if (action == null)
{
Console.WriteLine(data.Current);
}
else
{
action();
}
data.UnSelected(0, i);
}
}
public static void Try2(string name, Action action) //第2局
{
for (int i = 0; i < 5; i++)
{
if (i == 1)
continue;
if (data.IsSelected(1, i))
continue;
if (data.IsSelected(0, i, name))
continue;
data.Selected(1, i, name);
if (action == null)
{
data.Count++;
Console.WriteLine("{0,5} {1}", data.Count, data.Current);
}
else
{
action();
}
data.UnSelected(1, i);
}
}
static Seat data = new Seat(); //借用Seat保存数据
public Seat2(string name, Seat2 parent, Action<Seat2> method)
{
this.Name = name;
this.Parent = parent;
if (parent != null)
parent.Child = this;
this.Method = method;
}
public static void Run()
{
new Seat2("A", null, me => me.Try())
.T("B", me => me.Try())
.T("C", me => me.Try())
.T("D", me => me.Try())
.T("A", me => me.Try2())
.T("B", me => me.Try2())
.T("C", me => me.Try2())
.T("D", me => me.Try2())
.P().Start();
}
public Seat2 T(string name, Action<Seat2> method)
{
return new Seat2(name, this, method);
}
public void Try()
{
for (int i = 0; i < 4; i++)
{
if (data.IsSelected(0, i))
continue;
data.Selected(0, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(0, i);
}
}
public void Try2()
{
for (int i = 0; i < 5; i++)
{
if (i == 1)
continue;
if (data.IsSelected(1, i))
continue;
if (data.IsSelected(0, i, this.Name))
continue;
data.Selected(1, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(1, i);
}
}
//运算框架
class ComputeLink<T> where T : ISeat
{
ComputeLink<T> Parent { get; set; } //父节点,即上一级节点
ComputeLink<T> Child { get; set; } //子节点,即下一级节点
T Obj { get; set; } //当前节点对应的算法对象,可以看作业务对象
public ComputeLink(T obj, ComputeLink<T> parent, Action<T> method)
{
if (obj == null)
throw new ArgumentNullException("obj");
this.Obj = obj;
this.Obj.Method = x => method((T)x);
if (parent != null)
{
this.Parent = parent;
parent.Child = this;
parent.Obj.Child = this.Obj;
}
}
public static ComputeLink<T> New(T obj, Action<T> method)
{
return new ComputeLink<T>(obj, null, method);
}
public ComputeLink<T> Do(T obj, Action<T> method)
{
return new ComputeLink<T>(obj, this, method);
}
public ComputeLink<T> Head //链表的头
{
get
{
if (null != this.Parent)
return this.Parent.Head;
return this;
}
}
public void Action() //启动(延迟的)整个计算
{
var head = this.Head;
head.Obj.Method(head.Obj);
}
}
interface ISeat
{
ISeat Child { get; set; }
Action<ISeat> Method { get; set; }
}
class Seat3 : ISeat3
{
static Seat data = new Seat();
string Name { get; set; }
public Seat3(string name)
{
this.Name = name;
}
/// <summary>
/// 解耦的版本
/// </summary>
public static void Run()
{
var sql = ComputeLink<Seat3>
.New(new Seat3("A"), m => m.Try())
.Do(new Seat3("B"), m => m.Try())
.Do(new Seat3("C"), m => m.Try())
.Do(new Seat3("D"), m => m.Try())
.Do(new Seat3("A"), m => m.Try2())
.Do(new Seat3("B"), m => m.Try2())
.Do(new Seat3("C"), m => m.Try2())
.Do(new Seat3("D"), m => m.Try2())
.Do(new Seat3(""), m => m.Print());
sql.Action();
}
public Action<ISeat3> Method { get; set; }
public ISeat3 Child { get; set; }
public void Try()
{
for (int i = 0; i < 4; i++)
{
if (data.IsSelected(0, i))
continue;
data.Selected(0, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(0, i);
}
}
public void Try2()
{
for (int i = 0; i < 5; i++)
{
if (i == 1)
continue;
if (data.IsSelected(1, i))
continue;
if (data.IsSelected(0, i, this.Name))
continue;
data.Selected(1, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(1, i);
}
}
public void Print()
{
data.Count++;
Console.WriteLine("{0,5} {1}", data.Count, data.Current);
}
}
using System;
using System.Linq;
using System.Diagnostics;
namespace ConsoleApplication
{
class Seat
{
static Seat data = new Seat();
public static void Run()
{
//Seat.Run();
//return;
for (int a = ; a < ; a++)
{
if (data.IsSelected(, a)) //第局编号。如果已经被人坐了。
continue;
data.Selected(, a, "A"); //第局编号。A坐a椅。
for (int b = ; b < ; b++)
{
if (data.IsSelected(, b))
continue;
data.Selected(, b, "B");
for (int c = ; c < ; c++)
{
if (data.IsSelected(, c))
continue;
data.Selected(, c, "C");
for (int d = ; d < ; d++)
{
if (data.IsSelected(, d))
continue;
data.Selected(, d, "D");
for (int a = ; a < ; a++)
{
if (a == )
continue;
if (data.IsSelected(, a)) //第局编号
continue;
if (data.IsSelected(, a, "A")) //如果第局A坐了a椅
continue;
data.Selected(, a, "A");
for (int b = ; b < ; b++)
{
if (b == )
continue;
if (data.IsSelected(, b))
continue;
if (data.IsSelected(, b, "B"))
continue;
data.Selected(, b, "B");
for (int c = ; c < ; c++)
{
if (c == )
continue;
if (data.IsSelected(, c))
continue;
if (data.IsSelected(, c, "C"))
continue;
data.Selected(, c, "C");
for (int d = ; d < ; d++)
{
if (d == )
continue;
if (data.IsSelected(, d))
continue;
if (data.IsSelected(, d, "D"))
continue;
data.Selected(, d, "D");
data.Count++; //可能的情况数加
Console.WriteLine("{,} {}", data.Count, data.Current);
data.UnSelected(, d);
}
data.UnSelected(, c);
}
data.UnSelected(, b);
}
data.UnSelected(, a);
}
data.UnSelected(, d);
}
data.UnSelected(, c);
}
data.UnSelected(, b);
}
data.UnSelected(, a); //A起身(释放坐椅)
}
}
public static void Run()
{
Try("A",
() => Try("B",
() => Try("C",
() => Try("D",
() => Try("A",
() => Try("B",
() => Try("C",
() => Try("D",
null
)
)
)
)
)
)
)
);
}
public static void Try(string name, Action action)
{
for (int i = ; i < ; i++)
{
if (data.IsSelected(, i))
continue;
data.Selected(, i, name);
if (action == null)
{
Console.WriteLine(data.Current);
}
else
{
action();
}
data.UnSelected(, i);
}
}
public static void Try(string name, Action action)
{
for (int i = ; i < ; i++)
{
if (i == )
continue;
if (data.IsSelected(, i))
continue;
if (data.IsSelected(, i, name))
continue;
data.Selected(, i, name);
if (action == null)
{
data.Count++;
Console.WriteLine("{,} {}", data.Count, data.Current);
}
else
{
action();
}
data.UnSelected(, i);
}
}
public Seat()
{
seats[, ] = ".";
seats[, ] = ".";
}
private string[,] seats = new string[, ];
public void UnSelected(int game, int i)
{
Debug.Assert(game == && i != || game == && i != );
Debug.Assert(seats[game, i] != null);
seats[game, i] = null;
}
public void Selected(int game, int i, string name)
{
Debug.Assert(game == && i != || game == && i != );
Debug.Assert(seats[game, i] == null);
seats[game, i] = name;
}
public bool IsSelected(int game, int a)
{
return seats[game, a] != null && seats[game, a] != ".";
}
public bool IsSelected(int game, int a, string name)
{
return seats[game, a] == name;
}
public string Current
{
get
{
return string.Format("{} {} {} {} {}-{} {} {} {} {}",
seats[, ], seats[, ], seats[, ], seats[, ], seats[, ],
seats[, ], seats[, ], seats[, ], seats[, ], seats[, ]);
}
}
public int Count { get; set; }
}
class Seat
{
static Seat data = new Seat(); //借用Seat保存法的数据
Seat Parent { get; set; }
Seat Child { get; set; }
string Name { get; set; }
Action<Seat> Method { get; set; }
public Seat(string name, Seat parent, Action<Seat> method)
{
this.Name = name;
this.Parent = parent;
if (parent != null)
parent.Child = this;
this.Method = method;
}
/// <summary>
/// 耦合的版本
/// </summary>
public static void Run()
{
new Seat("A", null, me => me.Try())
.T("B", me => me.Try())
.T("C", me => me.Try())
.T("D", me => me.Try())
.T("A", me => me.Try())
.T("B", me => me.Try())
.T("C", me => me.Try())
.T("D", me => me.Try())
.P().Start();
}
public Seat T(string name, Action<Seat> method)
{
return new Seat(name, this, method);
}
public Seat P()
{
return new Seat("Print", this, me => me.Print());
}
public void Start()
{
var head = this.Head;
head.Method(head);
}
public Seat Head
{
get
{
if (null != this.Parent)
return this.Parent.Head;
return this;
}
}
public void Try()
{
for (int i = ; i < ; i++)
{
if (data.IsSelected(, i))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Try()
{
for (int i = ; i < ; i++)
{
if (i == )
continue;
if (data.IsSelected(, i))
continue;
if (data.IsSelected(, i, this.Name))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Print()
{
data.Count++;
Console.WriteLine("{,} {}", data.Count, data.Current);
}
public override string ToString()
{
return this.Name.ToString();
}
}
class ComputeLink<T> where T : ISeat
{
ComputeLink<T> Parent { get; set; } //父节点,即上一级节点
ComputeLink<T> Child { get; set; } //子节点,即下一级节点
T Obj { get; set; } //当前节点对应的算法对象,可以看作业务对象
public ComputeLink(T obj, ComputeLink<T> parent, Action<T> method)
{
if (obj == null)
throw new ArgumentNullException("obj");
this.Obj = obj;
this.Obj.Method = x => method((T)x);
if (parent != null)
{
this.Parent = parent;
parent.Child = this;
parent.Obj.Child = this.Obj;
}
}
public static ComputeLink<T> New(T obj, Action<T> method)
{
return new ComputeLink<T>(obj, null, method);
}
public ComputeLink<T> Do(T obj, Action<T> method)
{
return new ComputeLink<T>(obj, this, method);
}
public ComputeLink<T> Head //链表的头
{
get
{
if (null != this.Parent)
return this.Parent.Head;
return this;
}
}
public void Action() //启动(延迟的)整个计算
{
var head = this.Head;
head.Obj.Method(head.Obj);
}
}
interface ISeat
{
ISeat Child { get; set; }
Action<ISeat> Method { get; set; }
}
class Seat : ISeat
{
static Seat data = new Seat();
string Name { get; set; }
public Seat(string name)
{
this.Name = name;
}
/// <summary>
/// 解耦的版本
/// </summary>
public static void Run()
{
var sql = ComputeLink<Seat>
.New(new Seat("A"), m => m.Try())
.Do(new Seat("B"), m => m.Try())
.Do(new Seat("C"), m => m.Try())
.Do(new Seat("D"), m => m.Try())
.Do(new Seat("A"), m => m.Try())
.Do(new Seat("B"), m => m.Try())
.Do(new Seat("C"), m => m.Try())
.Do(new Seat("D"), m => m.Try())
.Do(new Seat(""), m => m.Print());
sql.Action();
}
public Action<ISeat> Method { get; set; }
public ISeat Child { get; set; }
public void Try()
{
for (int i = ; i < ; i++)
{
if (data.IsSelected(, i))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Try()
{
for (int i = ; i < ; i++)
{
if (i == )
continue;
if (data.IsSelected(, i))
continue;
if (data.IsSelected(, i, this.Name))
continue;
data.Selected(, i, this.Name);
if (this.Child != null)
{
this.Child.Method(this.Child);
}
data.UnSelected(, i);
}
}
public void Print()
{
data.Count++;
Console.WriteLine("{,} {}", data.Count, data.Current);
}
public override string ToString()
{
return this.Name.ToString();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有