原题目见 [算法]方正面试题:N×N矩阵螺旋打印输出
本人毕业刚一年,属于OO方面的新手,以下代码纯属练习。先说下我的思路:按照下右上左的顺序对列表进行循环,一直走到不能走。比如对于1所在的位置(0,0),它可以向下走,可以向右走,按照“下右上左”的方式就是向下走到4所在的位置。
一、建一个实体类,用于存放数字左边,是否可以上下左右移动等信息。
NumberEntity
public class NumberEntity
{
private bool hasDown = false;
public bool HasDown
{
get {
if (x + 1 < y_sum)
return true;
return false;
}
set { hasDown = value; }
}
private bool hasUp = false;
public bool HasUp
{
get
{
if (x - 1 >= 0)
return true;
return false;
}
set { hasUp = value; }
}
private bool hasLeft = false;
public bool HasLeft
{
get
{
if (y - 1 >= 0)
return true;
return false;
}
set { hasLeft = value; }
}
private bool hasRight = false;
public bool HasRight
{
get
{
if (y + 1 < x_sum)
return true;
return false;
}
set { hasRight = value; }
}
private int x = 0;
public int X
{
get { return x; }
set { x = value; }
}
private int y = 0;
public int Y
{
get { return y; }
set { y = value; }
}
private int value = 0;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
private bool isPrint = false;
public bool IsPrint
{
get { return isPrint; }
set { isPrint = value; }
}
private int x_sum = 0;
private int y_sum = 0;
public NumberEntity(int x, int y,int sum ,int value)
{
this.x = x;
this.y = y;
this.x_sum = this.y_sum = sum;
this.value = value;
}
}
public class NumberEntity
{
private bool hasDown = false;
public bool HasDown
{
get {
if (x + 1 < y_sum)
return true;
return false;
}
set { hasDown = value; }
}
private bool hasUp = false;
public bool HasUp
{
get
{
if (x - 1 >= 0)
return true;
return false;
}
set { hasUp = value; }
}
private bool hasLeft = false;
public bool HasLeft
{
get
{
if (y - 1 >= 0)
return true;
return false;
}
set { hasLeft = value; }
}
private bool hasRight = false;
public bool HasRight
{
get
{
if (y + 1 < x_sum)
return true;
return false;
}
set { hasRight = value; }
}
private int x = 0;
public int X
{
get { return x; }
set { x = value; }
}
private int y = 0;
public int Y
{
get { return y; }
set { y = value; }
}
private int value = 0;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
private bool isPrint = false;
public bool IsPrint
{
get { return isPrint; }
set { isPrint = value; }
}
private int x_sum = 0;
private int y_sum = 0;
public NumberEntity(int x, int y,int sum ,int value)
{
this.x = x;
this.y = y;
this.x_sum = this.y_sum = sum;
this.value = value;
}
}
二、建一个实体维护类
EntityManager
public class EntityManager
{
private Dictionary<string, NumberEntity> entities = new Dictionary<string, NumberEntity>();
public void Add(NumberEntity entity)
{
entities.Add(entity.X.ToString() + entity.Y.ToString(), entity);
}
/**//// <summary>
/// 根据坐标获得实体
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public NumberEntity GetEntity(int x, int y)
{
if (!entities.ContainsKey(x.ToString() + y.ToString()))
return null;
return entities[x.ToString() + y.ToString()];
}
/**//// <summary>
/// 更新一个数字实体
/// </summary>
/// <param name="entity"></param>
public void Update(NumberEntity entity)
{
entities[entity.X.ToString() + entity.Y.ToString()] = entity;
}
/**//// <summary>
/// 获得下一个可用的数字实体
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public NumberEntity GetNext(NumberEntity entity)
{
if (entity.HasDown)
{
NumberEntity nextEntity = GetEntity(entity.X+1, entity.Y);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
if (entity.HasRight)
{
NumberEntity nextEntity = GetEntity(entity.X, entity.Y+1);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
if (entity.HasUp)
{
NumberEntity nextEntity = GetEntity(entity.X-1, entity.Y);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
if (entity.HasLeft)
{
NumberEntity nextEntity = GetEntity(entity.X, entity.Y-1);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
return null;
}
}
public class EntityManager
{
private Dictionary<string, NumberEntity> entities = new Dictionary<string, NumberEntity>();
public void Add(NumberEntity entity)
{
entities.Add(entity.X.ToString() + entity.Y.ToString(), entity);
}
/**//// <summary>
/// 根据坐标获得实体
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public NumberEntity GetEntity(int x, int y)
{
if (!entities.ContainsKey(x.ToString() + y.ToString()))
return null;
return entities[x.ToString() + y.ToString()];
}
/**//// <summary>
/// 更新一个数字实体
/// </summary>
/// <param name="entity"></param>
public void Update(NumberEntity entity)
{
entities[entity.X.ToString() + entity.Y.ToString()] = entity;
}
/**//// <summary>
/// 获得下一个可用的数字实体
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public NumberEntity GetNext(NumberEntity entity)
{
if (entity.HasDown)
{
NumberEntity nextEntity = GetEntity(entity.X+1, entity.Y);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
if (entity.HasRight)
{
NumberEntity nextEntity = GetEntity(entity.X, entity.Y+1);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
if (entity.HasUp)
{
NumberEntity nextEntity = GetEntity(entity.X-1, entity.Y);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
if (entity.HasLeft)
{
NumberEntity nextEntity = GetEntity(entity.X, entity.Y-1);
if (!nextEntity.IsPrint)
{
return nextEntity;
}
}
return null;
}
}
三、建一个执行类
Process
public class Process
{
private List<int> result = new List<int>();
/**//// <summary>
/// 按照下右上左顺序依次循环,走到最后一个不能走的数字为止
/// </summary>
/// <param name="numbers"></param>
/// <returns></returns>
public List<int> GetResult(NumberEntity entity)
{
result.Add(entity.Value);
entity.IsPrint = true;
entityManger.Update(entity);
NumberEntity next = entityManger.GetNext(entity);
if (next == null)
return result;
else
return GetResult(next);
}
private EntityManager entityManger = new EntityManager();
public Process(EntityManager entitymanager)
{
this.entityManger = entitymanager;
}
}
public class Process
{
private List<int> result = new List<int>();
/**//// <summary>
/// 按照下右上左顺序依次循环,走到最后一个不能走的数字为止
/// </summary>
/// <param name="numbers"></param>
/// <returns></returns>
public List<int> GetResult(NumberEntity entity)
{
result.Add(entity.Value);
entity.IsPrint = true;
entityManger.Update(entity);
NumberEntity next = entityManger.GetNext(entity);
if (next == null)
return result;
else
return GetResult(next);
}
private EntityManager entityManger = new EntityManager();
public Process(EntityManager entitymanager)
{
this.entityManger = entitymanager;
}
}
四、测试程序
Test
static void Main(string[] args)
{
EntityManager manager = new EntityManager();
int k = 1;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
manager.Add(new NumberEntity(x, y,3, k++));
}
}
Process process = new Process(manager);
List<int> result = new List<int>();
result = process.GetResult(new NumberEntity(0, 0,3, 1));
foreach (int r in result)
{
Console.Write(r.ToString());
}
Console.Read();
}
static void Main(string[] args)
{
EntityManager manager = new EntityManager();
int k = 1;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
manager.Add(new NumberEntity(x, y,3, k++));
}
}
Process process = new Process(manager);
List<int> result = new List<int>();
result = process.GetResult(new NumberEntity(0, 0,3, 1));
foreach (int r in result)
{
Console.Write(r.ToString());
}
Console.Read();
}