| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 456 人关注过本帖
标题:时间空间复杂度计算1
只看楼主 加入收藏
heye969
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-5-28
收藏
 问题点数:0 回复次数:0 
时间空间复杂度计算1
   第一个:    /// <summary>
        /// 蛇向前移动一个象素
        /// </summary>
        public void Move()
        {
            //蛇头向前移动一个象素
            switch (direction)
            {
                case MoveDirection.Left:
                    head.X--;
                    break;
                case MoveDirection.Right:
                    head.X++;
                    break;
                case MoveDirection.Up:
                    head.Y--;
                    break;
                case MoveDirection.Down:
                    head.Y++;
                    break;
                default:
                    break;
            }

            //蛇尾根据前一个点来确定移动方向的
            Point prePoint = head;
            if (bodyPoints.Count > 0)
            {
                //如果蛇身处有点则取蛇身最后一个点
                prePoint = bodyPoints[bodyPoints.Count - 1];
            }

            //确定要移动的方向
            //如果这两个点在水平方向
            if (tail.Y == prePoint.Y)
            {
                //如果蛇尾在右边
                if (tail.X > prePoint.X)
                {
                    //蛇尾左移
                    tail.X--;
                }
                else//蛇尾在左边
                {
                    //蛇尾右移
                    tail.X++;
                }
            }
            else//垂直方向
            {
                //蛇尾在前一个点的下面
                if (tail.Y > prePoint.Y)
                {
                    //蛇尾上移
                    tail.Y--;
                }
                else//蛇尾在前一个点上面
                {
                    //蛇尾下移
                    tail.Y++;
                }
            }

            //如果蛇尾追上了前一个点,这个点肯定是蛇身上的点
            if (bodyPoints.Count > 0 && tail.X == prePoint.X &&
                tail.Y == prePoint.Y)
            {
                bodyPoints.RemoveAt(bodyPoints.Count - 1);
            }
        }
   第二个:     /// <summary>
        /// 改变方向
        /// </summary>
        /// <param name="dir">改变后的方向</param>
        public void ChangeDirection(MoveDirection dir)
        {
            //如果改变后的方向和当前方向相反,则什么也不做
            if ((dir == MoveDirection.Left && direction == MoveDirection.Right) ||
                (dir == MoveDirection.Right && direction == MoveDirection.Left) ||
                (dir == MoveDirection.Up && direction == MoveDirection.Down) ||
                (dir == MoveDirection.Down && direction == MoveDirection.Up))
            {
                return;
            }

            //改变方向
            if (dir != direction)
            {
                //插入一个点
                bodyPoints.Insert(0, head);
                direction = dir;
            }
            //向前移动一个点
            Move();
        }

   第三个:     /// <summary>
        /// 判断蛇头是否已经接触到蛇体,每移动一个点要调用此判断
        /// 判断方法即蛇头的点不能位于其他两点之间
        /// </summary>
        /// <returns></returns>
        public bool IsHeadTouchedBody()
        {
            Point p1 = tail;
            //判断蛇尾和蛇体各点组成的线是否穿过蛇头
            //蛇体第一个点和第二个点组成的线不可能穿过蛇头,所以只循环到i=1
            for (int i = bodyPoints.Count - 1; i > 0; i--)
            {
                Point p2 = bodyPoints[i];
                //蛇头在两点之间
                if (IsBetweenTwoPoint(p1, p2, head))
                {
                    return true;
                }
                p1 = p2;
            }
            return false;
        }
 第四个:       /// <summary>
        /// 判断p是否在p1和p2之间

        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        private bool IsBetweenTwoPoint(Point p1, Point p2, Point p)
        {
            //p1和p2的X坐标或Y座标有且只有一个是相等的
            Debug.Assert(p1.X == p1.X && p1.Y != p2.Y || p1.Y == p2.Y && p1.X != p2.X);
            //在垂直直线上
            if (p1.X == p2.X)
            {
                if (p.X == p1.X &&
                    ((p.Y >= p1.Y && p.Y <= p2.Y) || //p2在p1的下面
                    (p.Y <= p1.Y && p.Y >= p2.Y)))//p2在p1的上面
                {
                    return true;
                }
            }
            //在水平直线上
            else
            {
                if (p.Y == p1.Y &&
                    ((p.X >= p1.X && p.X <= p2.X) || //p2在p1的右边
                    (p.X <= p1.X && p.X >= p2.X))) //p2在p1的左边
                {
                    return true;
                }
            }
            return false;
        }
  第五个;      /// <summary>
        /// 吃一个食物,当且仅当这个食物和蛇头相碰时吃调这个食物
        /// </summary>
        /// <param name="food"></param>
        /// <returns></returns>
        public bool EatFood(Food food)
        {
            int radius = (food.Size + 1) / 2;
            //蛇头碰到食物
            if ((head.X == food.Location.X && Math.Abs(head.Y - food.Location.Y) <= radius) ||
                (head.Y == food.Location.Y && Math.Abs(head.X - food.Location.X) <= radius))
            {
                //蛇尾根据前一个点来确定方向
                //默认前一个点为蛇头
                Point prePoint = head;
                if (bodyPoints.Count > 0)
                {
                    //如果蛇身处有点则取蛇身最后一个点
                    prePoint = bodyPoints[bodyPoints.Count - 1];
                }
                //确定要移动的方向
                //这两个点在水平位置
                if (tail.Y == prePoint.Y)
                {
                    //蛇尾在右边
                    if (tail.X > prePoint.X)
                    {
                        //蛇尾右移
                        tail.X += food.Size;
                    }
                    //蛇尾在左边
                    else
                    {
                        //左移
                        tail.X -= food.Size;
                    }
                }
                else//这两个点在垂直位置tail.X == prePoint.X
                {
                    //蛇尾在下面
                    if (tail.Y > prePoint.Y)
                    {
                        //下移
                        tail.Y += food.Size;
                    }
                    //蛇尾在上面
                    else
                    {
                        //上移
                        tail.Y -= food.Size;
                    }
                }
                return true;
            }
            return false;
        }

        #region DrawObject Members

分别计算一下,最好有详细过程,谢谢
搜索更多相关主题的帖子: 空间 时间 
2010-05-28 19:48
快速回复:时间空间复杂度计算1
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022115 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved