zoukankan      html  css  js  c++  java
  • 11. 跳马问题

    有一个无限大的棋盘,棋盘上有一匹马,马移动的方式为日字型。即假设马当前的位置为(x,y),那么下一步可以移动到(x+1,y+2),(x+1,y-2),(x-1,y+2),(x-1,y-2),(x+2,y+1),(x+2,y-1),(x-2,y+1)或者(x-2,y-1)这8个位置。

    问马是否能从坐标(x,y)按照上述移动规则移动到坐标(x2,y2)。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication9
    {
        class Program
        {
            static void Main(string[] args)
            {
                Coordinate a = new Coordinate(4, 4);
                Coordinate b = new Coordinate(6, 6);
                Console.Write(IsAbleMoveTo(a, b));
            }
    
            static Coordinate MoveUpOne(Coordinate a)
            {
                a.x += 2;
                a.y += 1;
                a.x -= 1;
                a.y -= 2;
                a.x -= 1;
                a.y += 2;
                return a;
            }
    
            static Coordinate MoveDownOne(Coordinate a)
            {
                a.x += 1;
                a.y -= 2;
                a.x += 1;
                a.y += 2;
                a.x -= 2;
                a.x -= 1;
                return a;
            }
    
            static Coordinate MoveLeftOne(Coordinate a)
            {
                a.x -= 1;
                a.y += 2;
                a.x += 2;
                a.y -= 1;
                a.x -= 2;
                a.y -= 1;
                return a;
            }
    
            static Coordinate MoveRightOne(Coordinate a)
            {
                a.x += 2;
                a.y += 1;
                a.x -= 2;
                a.y += 1;
                a.x += 1;
                a.y -= 2;
                return a;
            }
    
            static bool IsAbleMoveTo(Coordinate a, Coordinate b)
            {
                int x = b.x - a.x;
                int y = b.y - a.y;
                if (x>0)
                {
                    for (int i = 0; i < x; i++)
                    {
                        a=MoveRightOne(a);
                    }
                }
                else
                {
                    for (int i = 0; i < -x; i++)
                    {
                        a=MoveLeftOne(a);
                    }
                }
                if (y>0)
                {
                    for (int i = 0; i < y; i++)
                    {
                        a=MoveUpOne(a);
                    }
                }
                else
                {
                    for (int i = 0; i < -y; i++)
                    {
                        a=MoveDownOne(a);
                    }
                }
                if (a.x == b.x && a.y == b.y)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
        }
    
        struct Coordinate
        {
            public int x;
            public int y;
            public Coordinate(int a, int b)
            {
                x = a;
                y = b;
            }
        }
    }
    View Code
  • 相关阅读:
    Oracle_高级功能(9) 性能优化
    Oracle_高级功能(8) 事务和锁
    Oracle_高级功能(7) 数据字典视图和动态性能视图
    Oracle_高级功能(6) 分区
    Oracle_高级功能(5) 用户、角色、权限
    Oracle_高级功能(4) 数据库存储结构
    Oracle_高级功能(3) synonym和database link
    Oracle_高级功能(2) 索引
    hdu 1561 The more, The Better 树形dp
    C. Glass Carving 正着做或者倒着做都可以
  • 原文地址:https://www.cnblogs.com/Ligeance/p/3535751.html
Copyright © 2011-2022 走看看