hdoj 3766 Knight's Trip 【】
2015-11-07 22:41
323 查看
——计算机学院大学生程序设计竞赛(新生为主) |
Knight's TripTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 469 Accepted Submission(s): 99 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分析:这题刚开始一直想用bfs做,但是没有规定N,m,所以用bfs写肯定不对,不用bfs,找规律不太擅长,所以没有做出来。代码:#include<cstdio> #include<cstring> int main() { char str[20]; while(scanf("%s",str),str[0]!='E') { int x=0,y,flag=1; int k; for(int i=0;str[i];i++) { if(str[i]=='-'&&i==0) flag=-1; else x=x*10+str[i]-48; } x=x*flag; scanf("%d",&y); if(x<0) x=-x; if(y<0) y=-y; if(y<x) { k=x;x=y; y=k; } if(y<=2*x) { if(x==1&&y==1) printf("2\n"); else if(x==2&&y==2) printf("4\n"); else printf("%d\n",(x+y)/3+(x+y)%3); } else{ int ans=x; int cc=(y-2*x)%4; ans+=cc; ans+=(y-2*x-cc)/2; if(y==1&&x==0) ans=3; printf("%d\n",ans); } } return 0; } |
相关文章推荐
- 字符串-查找第一个只出现一次的字符
- B - 四月纲领 light oj 1245【数论】
- leetcode-Remove Linked List Elements
- 2,XCode的快捷键
- 【Oracle】无宕机迁移更换asm磁盘
- JavaScript高级程序设计之DOM2和DOM3之样式之访问元素的样式第12.2.1讲
- 剑指offer之65 滑动窗口的最大值问题
- Andrid的网络开发技术
- 3.0MongoDB对一个collection的常用操作
- 汉字转拼音
- D - 粉碎叛乱F - 其他起义
- gradle eclipse web模板项目构建
- 2817:木棒 【递归+剪枝】
- exej4打包jar图文教程
- Java String之intern()方法深入分析
- 3.0创建mongoDB客户端链接
- DNS弹窗广告遭遇
- 关于html中CSS的小结
- Swift开发基础篇—基本语法
- codeforces 592D 树链剖分