25,985
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace Wall
{
public partial class _Default : System.Web.UI.Page
{
protected StringBuilder HTML = new StringBuilder();
protected StringBuilder SCRIPT = new StringBuilder();
private List<List<int>> totalWays = new List<List<int>>();
private Dictionary<string, bool> allWalls = new Dictionary<string, bool>();
private Dictionary<string, bool> deleteWalls = new Dictionary<string, bool>();
protected int maxX;
protected int maxY;
protected void Page_Load(object sender, EventArgs e)
{
HTML.Append("");
maxX = Convert.ToInt32(this.TextBox1.Text);
maxY = Convert.ToInt32(this.TextBox2.Text);
for (int y = 0; y < maxY; y++)
{
HTML.Append("<tr>" );
for (int x = 1; x <= maxX; x++)
{
int id = x + y * maxX;
List<int> room = new List<int>();
room.Add(id);
totalWays.Add(room);
string theWall = makeWall(id, id + 1);
if (x < maxX) { allWalls.Add(theWall,true); }
theWall = makeWall(id, id + maxX);
if (y < maxY) { allWalls.Add(theWall, true); }
HTML.Append("<td id=td" + id + ">" + " </td>");
}
HTML.Append("</tr>");
}
while (totalWays.Count > 1)
{
makePuzzle();
}
foreach (string Key in deleteWalls.Keys)
{
SCRIPT.Append("d(" + Key + ");");
}
}
private void makePuzzle()
{
//随机在全部中选择一个回廊
int firstChoice = randomInt(totalWays.Count);
//列出该回廊全部相关回廊;
List<int> firstChoiceWay = totalWays[firstChoice];
List<List<int>> tempWay = new List<List<int>>();
for (int i = 0; i < totalWays.Count; i++)
{
if (i != firstChoice)
{
if (isNeighbor(firstChoiceWay, totalWays[i]))
{
tempWay.Add(totalWays[i]);
//break;
}
}
}
//随机在totalWays中选择一个回廊
int secondChoice = randomInt(tempWay.Count);
List<int> secondCoiceWay = tempWay[secondChoice];
//得到2者间全部可以拆的墙
List<string> tempWalls = new List<string>();
for (int i = 0; i < firstChoiceWay.Count; i++)
{
for (int j = 0; j < secondCoiceWay.Count; j++)
{
if (IsExsists(firstChoiceWay[i], secondCoiceWay[j]))
{
tempWalls.Add(makeWall(firstChoiceWay[i], secondCoiceWay[j]));
}
}
}
int thirdChoice = randomInt(tempWalls.Count);
string theWall = tempWalls[thirdChoice];
//纪录拆墙办法
deleteWalls.Add(theWall,true);
//增加一个新的
List<int> newWay = new List<int>();
newWay.AddRange(firstChoiceWay);
newWay.AddRange(secondCoiceWay);
totalWays.Add(newWay);
//移掉2个
totalWays.Remove(firstChoiceWay);
totalWays.Remove(secondCoiceWay);
allWalls.Remove(theWall);
}
private string makeWall(int x, int y)
{
if (x > y)
{
return y.ToString() + "," + x;
}
else
{
return x.ToString() + "," + y;
}
}
private int randomInt(int input)
{
Random rand = new Random(input);
//随机在全部中选择一个
//得到回廊的回廊
if (input == 0)
{
return 0;
}
else
{
return rand.Next(0, input);
}
}
/// <summary>
/// 判断两个集合是否是邻居
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
private bool isNeighbor(List<int> a, List<int> b)
{
bool re = false;
for (int i = 0; i < a.Count; i++)
{
for (int j = 0; j < b.Count; j++)
{
if (IsExsists(a[i], b[j]))
{
re = true;
break;
}
}
if (re) { break; }
}
return re;
}
private bool IsExsists(int x, int y)
{
bool re = false;
if (Math.Abs(x-y) == 1 || Math.Abs(x-y) == maxX)
{
string theWall = makeWall(x,y);
if (allWalls.ContainsKey(theWall))
{
re = true;
}
}
return re;
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}