码迷,mamicode.com
首页 > 移动开发 > 详细

移动距离

时间:2015-04-14 19:52:26      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

Problem H: 移动距离

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 3  Solved: 1
[Submit][Status][Web Board] [Edit] [TestData]

Description

X星球居民小区的楼房全是一样的,并且按矩阵样式排列。其楼房的编号为1,2,3... 当排满一行时,从下一行相邻的楼往反方向排号。 比如:当小区排号宽度为6时,开始情形如下: 1 2 3 4 5 6 12 11 10 9 8 7 13 14 15 ..... 我们的问题是:已知了两个楼号m和n,需要求出它们之间的最短移动距离(不能斜线方向移动) 输入为3个整数w m n,空格分开,都在1到10000范围内 w为排号宽度,m,n为待计算的楼号。 要求输出一个整数,表示m n 两楼间最短移动距离。

Input

6 8 2

Output

4

Sample Input

4 7 20

Sample Output

5
思路:记下m和n的位置。。然后再算最短移动距离
#include<iostream>
using namespace std;
int num[100][100];
int main()
{
    int w,m,n,count,xa,xb,ya,yb,flag,i,j;
    while(scanf("%d%d%d",&w,&m,&n)!=EOF)
    {
        if(m>n)
        {
            int t;
            t=m;
            m=n;
            n=t;
        }
        flag=0;
       count=1;
       for(i=0;i<1000;i++)
       {
           if(i%2==0)
           {
               for(j=0;j<w;j++)
               {
                   num[i][j]=count;
                   if(m==num[i][j])
                   {
                       xa=i;
                       xb=j;
                   }
                   else if(n==num[i][j])
                   {
                       ya=i;
                       yb=j;
                       flag=1;
                       break;
                   }
                   count++;
               }
           }
           else
           { 
               for(j=w-1;j>=0;j--)
               {
                   num[i][j]=count;
                    if(m==num[i][j])
                   {
                       xa=i;
                       xb=j;
                   }
                   else if(n==num[i][j])
                   {
                       ya=i;
                       yb=j;
                       flag=1;
                       break;
                   }
                   count++;
               }
           }
           if(flag)
               break;
       }
       if(xb<yb)
           cout<<(yb-xb)+(ya-xa)<<endl;
       else
          cout<<(xb-yb)+(ya-xa)<<endl;
    }
    return 0;
}

移动距离

标签:

原文地址:http://blog.csdn.net/zuguodexiaoguoabc/article/details/45045383

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!