zoukankan      html  css  js  c++  java
  • hdu 1030 Delta-wave

    http://acm.hdu.edu.cn/showproblem.php?pid=1030

     Delta-wave

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6821    Accepted Submission(s): 2628

    Problem Description
    A triangle field is numbered with successive integers in the way shown on the picture below. 
    The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell
    to cell through vertices. The number of edges the traveller passes makes the length of the traveller's route. 
    Write the program to determine the length of the shortest route connecting cells with numbers N and M. 
     
    Input
    Input contains two integer numbers M and N in the range from 1 to 1000000000 separated with space(s).
     
    Output
    Output should contain the length of the shortest route.
     
    Sample Input
    6 12
     
    Sample Output
    3
     

    一道找规律的题,每个三角形的位置用三维的坐标(x, y, z)表示, x表示水平层数从上到下,y表示从左到右,z表示从右到左

    求从第n个三角到第m个三角的最近距离 = 各个坐标差之和

     即:d = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);

    第n个三角说明从1到n共有n个三角,判断它在第几行即坐标值x

    用for循环来找

     for(i = 1 ; ; i += 2)//i表示某一行三角的个数,每行三角的个数相差2,所以i每次加2
            {
                if(n - i <= 0)//如果此时三角的个数小于或等于某一行的三角个数,则已经可以确定了坐标,此时n表示第n个三角在x行的编号
                {
                    y = (i - n) / 2 + 1;
                    z = (n + 1) / 2;
                    break;
                }//求出坐标y和z
                x++;//如果n大于前i行的三角个数,则n所在的行数再下一行,即x++
                n -= i;
            }

    结合图来理解

    http://images.cppblog.com/cppblog_com/guodongshan/1030.jpg

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    
    using namespace std;
    
    int main()
    {
        int n, i, m, x1, y1, z1, x2, y2, z2, d;
        while(~scanf("%d%d", &n, &m))
        {
            x1 = x2 = 1;
            for(i = 1 ; ; i += 2)
            {
                if(n - i <= 0)
                {
                    y1 = (i - n) / 2 + 1;
                    z1 = (n + 1) / 2;
                    break;
                }
                x1++;
                n -= i;
            }
            for(i = 1 ; ; i += 2)
            {
                if(m - i <= 0)
                {
                    y2 = (i - m) / 2 + 1;
                    z2 = (m + 1) / 2;
                    break;
                }
                x2++;
                m -= i;
            }
            d = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2);
            printf("%d
    ", d);
        }
        return 0;
    }
  • 相关阅读:
    ubuntu12.04 死机 卡屏 画面冻结解决方案
    Install Firefox 20 in Ubuntu 13.04, Ubuntu 12.10, Ubuntu 12.04, Linux Mint 14 and Linux Mint 13 by PPA
    ListView1.SelectedItems.Clear()
    android studio 下载地址
    jquery.slider.js jquery幻灯片测试
    jquery.hovermenu.js
    jquery.tab.js选项卡效果
    适配 placeholder,jquery版
    jquery.autoscroll.js jquery自动滚动效果
    将 Google Earth 地图集成到自己的窗体上的 简单控件
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4792288.html
Copyright © 2011-2022 走看看