当前位置: 首页 > news >正文

N×N矩阵螺旋打印输出的OO算法

原题目见 [算法]方正面试题:N×N矩阵螺旋打印输出

本人毕业刚一年,属于OO方面的新手,以下代码纯属练习。先说下我的思路:按照下右上左的顺序对列表进行循环,一直走到不能走。比如对于1所在的位置(0,0),它可以向下走,可以向右走,按照“下右上左”的方式就是向下走到4所在的位置。

一、建一个实体类,用于存放数字左边,是否可以上下左右移动等信息。

ContractedBlock.gif ExpandedBlockStart.gif NumberEntity
 public class NumberEntity
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private bool hasDown = false;

        
public bool HasDown
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get {
                
if (x + 1 < y_sum)
                    
return true;
                
return false;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { hasDown = value; }
        }

        
private bool hasUp = false;

        
public bool HasUp
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (x - 1 >= 0)
                    
return true;
                
return false;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { hasUp = value; }
        }

        
private bool hasLeft = false;

        
public bool HasLeft
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (y - 1 >= 0)
                    
return true;
                
return false;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { hasLeft = value; }
        }

        
private bool hasRight = false;

        
public bool HasRight
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (y + 1 < x_sum)
                    
return true;
                
return false;
            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { hasRight = value; }
        }


        
private int x = 0;

        
public int X
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return x; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { x = value; }
        }


        
private int y = 0;

        
public int Y
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return y; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { y = value; }
        }


        
private int value = 0;

        
public int Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return this.value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set this.value = value; }
        }


        
private bool isPrint = false;

        
public bool IsPrint
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return isPrint; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { isPrint = value; }
        }


        
private int x_sum = 0;

        
private int y_sum = 0;

        
public NumberEntity(int x, int y,int sum ,int value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.x = x;
            
this.y = y;
            
this.x_sum = this.y_sum = sum;
            
this.value = value;
        }


    }

二、建一个实体维护类

ContractedBlock.gif ExpandedBlockStart.gif EntityManager
public class EntityManager
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private Dictionary<string, NumberEntity> entities = new Dictionary<string, NumberEntity>();

        
public void Add(NumberEntity entity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            entities.Add(entity.X.ToString() 
+ entity.Y.ToString(), entity);
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 根据坐标获得实体
        
/// </summary>
        
/// <param name="x"></param>
        
/// <param name="y"></param>
        
/// <returns></returns>

        public NumberEntity GetEntity(int x, int y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (!entities.ContainsKey(x.ToString() + y.ToString()))
                
return null;
            
return entities[x.ToString() + y.ToString()];
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 更新一个数字实体
        
/// </summary>
        
/// <param name="entity"></param>

        public void Update(NumberEntity entity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            entities[entity.X.ToString() 
+ entity.Y.ToString()] = entity;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 获得下一个可用的数字实体
        
/// </summary>
        
/// <param name="entity"></param>
        
/// <returns></returns>

        public NumberEntity GetNext(NumberEntity entity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (entity.HasDown)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                NumberEntity nextEntity 
= GetEntity(entity.X+1, entity.Y);
                
if (!nextEntity.IsPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return nextEntity;
                }

            }

            
if (entity.HasRight)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                NumberEntity nextEntity 
= GetEntity(entity.X, entity.Y+1);
                
if (!nextEntity.IsPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return nextEntity;
                }

            }

            
if (entity.HasUp)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                NumberEntity nextEntity 
= GetEntity(entity.X-1, entity.Y);
                
if (!nextEntity.IsPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return nextEntity;
                }

            }

            
if (entity.HasLeft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                NumberEntity nextEntity 
= GetEntity(entity.X, entity.Y-1);
                
if (!nextEntity.IsPrint)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return nextEntity;
                }

            }

            
return null;
        }

    }

 

三、建一个执行类
ContractedBlock.gif ExpandedBlockStart.gif Process
public class Process
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private List<int> result = new List<int>();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 按照下右上左顺序依次循环,走到最后一个不能走的数字为止
        
/// </summary>
        
/// <param name="numbers"></param>
        
/// <returns></returns>

        public List<int> GetResult(NumberEntity entity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.entityManger = entitymanager;
        }

    }

四、测试程序
ContractedBlock.gif ExpandedBlockStart.gif Test
static void Main(string[] args)
ExpandedBlockStart.gifContractedBlock.gif        
{
            EntityManager manager 
= new EntityManager();
            
int k = 1;
            
for (int x = 0; x < 3; x++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
for (int y = 0; y < 3; y++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    manager.Add(
new NumberEntity(x, y,3, k++));
                }

            }


            Process process 
= new Process(manager);
            List
<int> result = new List<int>();
            result 
= process.GetResult(new NumberEntity(00,31));

            
foreach (int r in result)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Console.Write(r.ToString());
            }

            Console.Read();
        }

转载于:https://www.cnblogs.com/honan/archive/2009/07/24/1530480.html

相关文章:

  • lucene.net应用大全
  • 他们在创新,我们在做什么?
  • [翻译]XNA 3.0 Game Programming Recipes之twenty-two
  • 轻松实现二级无刷新联动菜单
  • 全新研发的Web OS操作 Palm智能机Pre不足3000
  • 一步一步自制网线
  • PowerDesigner 学习 收集整理
  • 2009年8月小记(DES加密模式, vim, DOS隐藏与排序,tinyget压力测试,线程等待)
  • .net MySql
  • 关于 VB.NET 中 Obsolete 特性的问题
  • pjsip pjsua test
  • l转 Js 获取屏幕坐标值
  • HTML中id属性使用的几种情况
  • [转载]sns是seo发展之路?
  • (zt)基于Facebook和Flash平台的应用架构解析
  • ES6指北【2】—— 箭头函数
  • __proto__ 和 prototype的关系
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Android框架之Volley
  • Create React App 使用
  • 闭包--闭包作用之保存(一)
  • 讲清楚之javascript作用域
  • 入门级的git使用指北
  • 数据仓库的几种建模方法
  • 为物联网而生:高性能时间序列数据库HiTSDB商业化首发!
  • 再谈express与koa的对比
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • 阿里云服务器购买完整流程
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • # Maven错误Error executing Maven
  • (10)ATF MMU转换表
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (安卓)跳转应用市场APP详情页的方式
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (未解决)macOS matplotlib 中文是方框
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .NET Core引入性能分析引导优化
  • .net 生成二级域名
  • .Net 中Partitioner static与dynamic的性能对比
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • .NET框架设计—常被忽视的C#设计技巧
  • .NET与 java通用的3DES加密解密方法
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • [04]Web前端进阶—JS伪数组
  • [23] GaussianAvatars: Photorealistic Head Avatars with Rigged 3D Gaussians
  • [51nod1610]路径计数
  • [AMQP Connection 127.0.0.1:5672] An unexpected connection driver error occured
  • [Android Studio 权威教程]断点调试和高级调试
  • [COGS 622] [NOIP2011] 玛雅游戏 模拟
  • [go 反射] 进阶
  • [Kubernetes]8. K8s使用Helm部署mysql集群(主从数据库集群)
  • [orleans2.1]这是你没玩过的船新版本