1. A
0
0
0
B
2. A000000
0
0
B
3. A0000
0
0
000B
4. A000000
0
0
0
B000000
由A点到B点连线的几种情况,得出A点所在的X=Xa线上和B点所在的X=Xb线上有任何一点满足是直线连通的则A点和B点是可连通的
//
// Map.cs
// Implementation of the Class Map
// Created on: 15-02-2009 09:05:15
// Original author:cancanChen
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Kyodai

{
public class Map
{
public const int RowCount = 15;
public const int ColCount = 10;
public const int Level = 10;
private int[,] map = new int[RowCount, ColCount];
public void Retset()
{ 
for (int i = 0; i < RowCount; i++)
for (int j = 0; j < ColCount; j++)
{
map[i, j] = 0;
}
int[] list = new int[(RowCount - 2) * (ColCount - 2)];
for (int i = 0; i < Level; i++)
{
list[i * 4] = i + 1;
list[i * 4 + 1] = i + 1;
list[i * 4 + 2] = i + 1;
list[i * 4 + 3] = i + 1;
}
Random(list);
SetList(list);
}
private void Random(int[] list)
{
Random random = new Random((int)DateTime.Now.Millisecond);
for (int i = list.Length; i > 0; i--)
{
int j = random.Next(i);
int temp = list[j];
list[j] = list[i - 1];
list[i - 1] = temp;
}
}
public void Refresh()
{
int[] list = GetList();
Random(list);

SetList(list);
}
private void SetList(int[] list)
{
for (int i = 1; i < RowCount - 1; i++)
for (int j = 1; j < ColCount - 1; j++)
{
map[i, j] = list[(j - 1) * (RowCount - 2) + (i - 1)];
}
}
private int[] GetList()
{
int[] list = new int[(RowCount - 2) * (ColCount - 2)];
for (int i = 1; i < RowCount - 1; i++)
for (int j = 1; j < ColCount - 1; j++)
{
list[(j - 1) * (RowCount - 2) + (i - 1)] = map[i, j];
}
return list;
}
public bool IsWin()
{
for (int i = 1; i < RowCount - 1; i++)
for (int j = 1; j < ColCount - 1; j++)
{
if (map[i, j] > 0)
return false;
}
return true;
}
public int GetNum(int x, int y)
{
return map[x, y];
}
public void SetNum(int x, int y)
{
map[x, y] = 0;
}
private List<Point> _linelist = new List<Point>();
public List<Point> LineList
{
get
{
return _linelist;
}
}
/**//// <summary>
///
/// </summary>
/// <returns></returns>
public bool CheckNoLine()
{
for (int i = 1; i < RowCount - 1; i++)
for (int j = 1; j < ColCount - 1; j++)
{
if (map[i, j] > 0)
{
for (int i1 = 1; i1 < RowCount - 1; i1++)
for (int j1 = 1; j1 < ColCount - 1; j1++)
{
if (CheckLine(i, j, i1, j1))
return false;
}
}
}
return true;
}
public bool CheckLine(int x1, int y1, int x2, int y2)
{
_linelist.Clear();
if (map[x1, y1] != map[x2, y2])
return false;
if (x1 == x2 && y1 == y2)
return false;
//if (CheckHorizontalLine(x1, y1, x2, y2))
//{
// _list = GetHorizontalLine(x1, y1, x2, y2);
// return true;
//}
//if (CheckVerticalLine(x1, y1, x2, y2))
//{
// _list = GetVerticalLine(x1, y1, x2, y2);
// return true;
//}


//x1 上下可到达点
List<Point> list1 = GetHorizontalPoint(x1, y1);
List<Point> list2 = GetHorizontalPoint(x2, y2);
foreach (Point point1 in list1)
{
foreach (Point point2 in list2)
{
if (CheckVerticalLine(point1.X, point1.Y, point2.X, point2.Y))
{
_linelist.AddRange(this.GetHorizontalLine(x1, y1, point1.X, point1.Y));
_linelist.AddRange(this.GetVerticalLine(point1.X, point1.Y, point2.X, point2.Y));
_linelist.AddRange(this.GetHorizontalLine(x2, y2, point2.X, point2.Y));
return true;
}
//else if (CheckVerticalLine(point1.X, point1.Y, point2.X, point2.Y))
//{
// _list.AddRange(this.GetVerticalLine(x1, y1, point1.X, point1.Y));
// _list.AddRange(this.GetVerticalLine(point1.X, point1.Y, point2.X, point2.Y));
// _list.AddRange(this.GetVerticalLine(x2, y2, point2.X, point2.Y));
// return true;
//}
}
}

List<Point> list3 = GetVerticalPoint(x1, y1);
List<Point> list4 = GetVerticalPoint(x2, y2);
foreach (Point point1 in list3)
{
foreach (Point point2 in list4)
{
if (CheckHorizontalLine(point1.X, point1.Y, point2.X, point2.Y))
{
_linelist.AddRange(this.GetVerticalLine(x1, y1, point1.X, point1.Y));
_linelist.AddRange(this.GetHorizontalLine(point1.X, point1.Y, point2.X, point2.Y));
_linelist.AddRange(this.GetVerticalLine(x2, y2, point2.X, point2.Y));
return true;
}
//else if (CheckHorizontalLine(point1.X, point1.Y, point2.X, point2.Y))
// {
// _list.AddRange(this.GetHorizontalLine(x1, y1, point1.X, point1.Y));
// _list.AddRange(this.GetHorizontalLine(point1.X, point1.Y, point2.X, point2.Y));
// _list.AddRange(this.GetHorizontalLine(x2, y2, point2.X, point2.Y));
// return true;
// }
}
}
//foreach (Point point1 in list1)
//{
// foreach (Point point2 in list4)
// {
// if (CheckVerticalLine(point1.X, point1.Y, point2.X, point2.Y))
// {
// _list.AddRange(this.GetHorizontalLine(x1, y1, point1.X, point1.Y));
// _list.AddRange(this.GetVerticalLine(point1.X, point1.Y, point2.X, point2.Y));
// _list.AddRange(this.GetVerticalLine(x2, y2, point2.X, point2.Y));
// return true;
// }
// }
//}
//foreach (Point point1 in list3)
//{
// foreach (Point point2 in list2)
// {
// if (CheckVerticalLine(point1.X, point1.Y, point2.X, point2.Y))
// {
// _list.AddRange(this.GetVerticalLine(x1, y1, point1.X, point1.Y));
// _list.AddRange(this.GetVerticalLine(point1.X, point1.Y, point2.X, point2.Y));
// _list.AddRange(this.GetHorizontalLine(x2, y2, point2.X, point2.Y));
// return true;
// }
// }
//}
return false;
}
/**//// <summary>
/// y1==y2
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private List<Point> GetHorizontalPoint(int x, int y)
{
List<Point> list = new List<Point>();
list.Add(new Point(x, y));
for (int i = x + 1; i < RowCount; i++)
{
if (map[i, y] > 0)
break;
list.Add(new Point(i, y));
}

for (int i = x - 1; i >= 0; i--)
{
if (map[i, y] > 0)
break;
list.Add(new Point(i, y));
}
return list;
}

/**//// <summary>
/// x1==x2
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private List<Point> GetVerticalPoint(int x, int y)
{
List<Point> list = new List<Point>();
list.Add(new Point(x, y));
for (int i = y + 1; i < ColCount; i++)
{
if (map[x, i] > 0)
break;
list.Add(new Point(x, i));
}

for (int i = y - 1; i >= 0; i--)
{
if (map[x, i] > 0)
break;
list.Add(new Point(x, i));
}
return list;
}
/**//// <summary>
/// x1==x2
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
private bool CheckVerticalLine(int x1, int y1, int x2, int y2)
{
if (x1 != x2)
return false;
//x相同 y1可以直接到达y2
int start = Math.Min(y1, y2);
int end = Math.Max(y1, y2);

for (int i = start + 1; i < end; i++)
if (map[x1, i] > 0)
return false;

return true;
}
/**//// <summary>
/// x1==x2
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
private List<Point> GetVerticalLine(int x1, int y1, int x2, int y2)
{
int start = Math.Min(y1, y2);
int end = Math.Max(y1, y2);
List<Point> list = new List<Point>();
for (int i = start; i <= end; i++)
list.Add(new Point(x1, i));
return list;
}
/**//// <summary>
/// y1==y2
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
private bool CheckHorizontalLine(int x1, int y1, int x2, int y2)
{
if (y1 != y2)
return false;
int start = Math.Min(x1, x2);
int end = Math.Max(x1, x2);
for (int i = start + 1; i < end; i++)
if (map[i, y1] > 0)
return false;
return true;
}
/**//// <summary>
/// y1==y2
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <returns></returns>
private List<Point> GetHorizontalLine(int x1, int y1, int x2, int y2)
{
int start = Math.Min(x1, x2);
int end = Math.Max(x1, x2);
List<Point> list = new List<Point>();
for (int i = start; i <= end; i++)
list.Add(new Point(i, y1));
return list;
}




}
}
浙公网安备 33010602011771号