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

Knight's Trip

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 482    Accepted Submission(s): 106

Problem Description
In chess, each move of a knight consists of moving by two squares horizontally and one square vertically, or by one square horizontally and two squares vertically. A knight making one move from location (0,0) of an infinite chess board would end up at one of the following eight locations: (1,2), (-1,2), (1,-2), (-1,-2), (2,1), (-2,1), (2,-1), (-2,-1).

Starting from location (0,0), what is the minimum number of moves required for a knight to get to some other arbitrary location (x,y)?

 
Input
Each line of input contains two integers x and y, each with absolute value at most one billion. The integers designate a location (x,y) on the infinite chess board. The final line contains the word END.
 
Output
For each location in the input, output a line containing one integer, the minimum number of moves required for a knight to move from (0,0) to (x, y).
 
Sample Input
1 2、
2 4
END
 
Sample Output
1
2
  1. /**
  2. 首先,xy的大小排序和转化为都是正数步数不变应该懂吧。
  3. y=2*x这种情况直接就是(x+y)/3步。
  4. 如果y<2*x但是(x+y)%3==0的话,那么我们可以通过控制(1,2),(2,1)
  5. 两种跳法的次数达到...总数必然是(x+y)/3,然后xy的和对3取余是1的话,
  6. 我们是不是必然可以在(x+y-1)/3步的时候跳到(x,y-1)这个点,但是不能一步
  7. 跳到(x,y),回撤两步到(x-4,y-5)这个点,我们可以用三步跳到(x,y),那么
  8. 就是原先的步数+1。余数为2,就是先跳到(x-1,y-1)这个地方,我们知道(0,0)
  9. 到(1,1)只需要两步,那么(x-1,y-1)到(x,y)也就是原先步数+2.然后考虑y>2*x,
  10. 先把(0,1)的情况特殊处理一下。接着我们可以用x步跳到(x,y),那么原问题就
  11. 转化为(0,0)到(0,y-2*x)。当y-2*x是4的倍数的话我们可以直接(1,2)(-1,2)这个
  12. 跳可以在(y-2*x)/2步到达。余数为1,就是(0,0)到(0,1)的问题,但是这个需要
  13. 三步不是最优的,我们后撤两步变为(0,0)到(0,5),我们可以三步达到,那么就
  14. 是原先的步数加上1就是解。余数为2,我们可以分别跳一次(2,1)(-2,1)到达。
  15. 余数为3,转化为(0,0)到(0,3)的问题我们可以(-1,2)(1,1)(0,3)三步到达。
  16. 以上就是全部情况,o(╯□╰)o,在纸上画画,应该所有在这这几类范围内。
  17. **/
  18.  
  19. #include<stdio.h>
  20. #include<string.h>
  21. int main()
  22. {
  23. char c[];
  24.  
  25. while(scanf("%s", c), strcmp(c, "END"))
  26. {
  27. int x, y;
  28. int k;
  29.  
  30. sscanf(c, "%d", &x);
  31. scanf("%d", &y);
  32.  
  33. if(x<) x = -x;
  34. if(y<) y = -y;
  35.  
  36. if(y<x) {k=x, x=y, y=k;}
  37. if(y<=*x)
  38. {
  39. if(x== && y==)
  40. printf("2\n");
  41. else if(x== && y==)
  42. printf("4\n");
  43. else
  44. printf("%d\n", (x+y)/+(x+y)%);
  45. }
  46. else
  47. {
  48. int ans = x;
  49. int c=(y-*x)%;
  50.  
  51. ans += c;
  52. ans += (y-*x-c)/;
  53.  
  54. if(y== && x==)
  55. ans = ;
  56.  
  57. printf("%d\n", ans);
  58. }
  59. }
  60. return ;
  61. }

(数学)Knight's Trip -- hdu -- 3766的更多相关文章

  1. [ACM_数学] 大菲波数 (hdu oj 1715 ,java 大数)

    大菲波数 Problem Description Fibonacci数列,定义如下:f(1)=f(2)=1f(n)=f(n-1)+f(n-2) n>=3.计算第n项Fibonacci数值.   ...

  2. 数学(GCD,计数原理)HDU 5656 CA Loves GCD

    CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 2621 ...

  3. Ant Trip HDU - 3018(欧拉路的个数 + 并查集)

    题意: Ant Tony和他的朋友们想游览蚂蚁国各地. 给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 保证这M条边都不同且不会存在同一点的自环边. 也就 ...

  4. Day4 - K - Ant Trip HDU - 3018

    Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together with his fr ...

  5. 2018年暑假ACM个人训练题7 题解报告

    A:HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用) B:(还需要研究一下.....) C:HDU 1071 The area(求三个点确定的抛物线的面积, ...

  6. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  7. 【数学】HDU 5761 Rower Bo

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 题目大意: 船在(0,a),船速v1,水速v2沿x轴正向,船头始终指向(0,0),问到达(0, ...

  8. 【数学】HDU 5753 Permutation Bo

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 题目大意: 两个序列h和c,h为1~n的乱序.h[0]=h[n+1]=0,[A]表示A为真则为 ...

  9. 【规律】【贪心】【数学】HDU 5573 Binary Tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 题目大意: 从1走到第k层,下一层的数是上一层的数*2或者*2+1,可以选择加上或者减去走的数 ...

随机推荐

  1. Git 安装和使用教程(更加详细)

    转载至:https://www.cnblogs.com/smuxiaolei/p/7484678.html#undefined Git 安装和使用教程 git 提交 全部文件 git add .  g ...

  2. Vue v-if 和 v-show

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 1L - ASCII码排序

    输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符. Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格. Output 对于每组输入数据,输出一行,字符中间用一个空格 ...

  4. 789A Anastasia and pebbles

    A. Anastasia and pebbles time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. De novo RNA-Seq Assembly Using De Bruijn Graphs

    De novo RNA-Seq Assembly Using De Bruijn Graphs  2017-06-12 09:42:47     59     0     0 在说基因组的拼接之前,可 ...

  6. Node.js v7.4.0 Documentation Addons

    https://nodejs.org/docs/latest/api/addons.html Node.js Addons are dynamically-linked shared objects, ...

  7. Vue修饰符

    为了方便大家写代码,vue.js给大家提供了很多方便的修饰符,比如我们经常用到的取消冒泡,阻止默认事件等等~ 目录 表单修饰符 事件修饰符 鼠标按键修饰符 键值修饰符 v-bind修饰符(实在不知道叫 ...

  8. log4j日志整合输出(slf4j+commonslog+log4j+jdklogger)

    log4j日志整合输出(slf4j+commonslog+log4j+jdklogger) 博客分类: 日志   J2EE项目中,经常会用到很多第三方的开源组件和软件,这些组件都使用各自的日志组件,比 ...

  9. spring学习七 spring和dynamic project进行整合

    spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...

  10. 使用EventLog Analyzer监控、管理及分析日志