Delta-wave

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

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
 
Source
 
 
 
 
思路:

比如6 16 向右移一格..经过两条边即可...然后将其分层,确定他们的行列来计算相应的值即可。。哎呀,说不清了..
来看代码ba!。。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
int sum(int m,int &cc)
{ int rr=(int)sqrt(1.0*m); //确定她所在第几行..一右边为参数
if(rr*rr!=m) rr++;
cc=rr*rr-m;
if(cc&)
return *(rr-);
else
return *rr-;
} int main()
{
int n,m,ncc,mcc,add;
while(cin>>m>>n)
{
int rankn=sum(n,ncc); //得到n的所在第几行
int rankm=sum(m,mcc); //得到m的所在第几行
ncc>>=; //得到n所在第几列,以右边为参数
mcc>>=; //得到m所在第几列,以右边为参数
//此处可以进一步优化...现在就不优化了..
if(rankn==rankm)
cout<<abs(n-m)<<endl;
else
if(rankn<rankm)
{ if(mcc>=ncc&&mcc<=(rankm-rankn)/+ncc)
cout<<(rankm-rankn)<<endl;
else
{ if(*mcc<(rankm-rankn)/+*ncc)
{
add=ncc-mcc;
cout<<*add+(rankm-rankn)<<endl;
}
else
{
add=mcc-((rankm-rankn)/+ncc);
cout<<*add+(rankm-rankn)<<endl;
} }
}
else
{
//rankn>rankm
if(ncc>=mcc&&ncc<=(rankn-rankm)/+mcc)
cout<<(rankn-rankm)<<endl;
else
{
if(*ncc<(rankn-rankm)/+*mcc)
{
add=mcc-ncc;
cout<<*add+(rankn-rankm)<<endl;
}
else
{
add=ncc-((rankn-rankm)/+mcc);
cout<<*add+rankn-rankm<<endl;
}
}
}
/* cout<<0&1<<endl;*/
} return ;
}

最新文章

  1. jgGrid中的editrules使用函数来进行验证
  2. 关于spring AOP的学习
  3. MQTT协议简记
  4. reorderList使用
  5. IE8/9的console之坑
  6. java 连接msql数据库
  7. Java内部类的使用小结
  8. 在Activity之间传递数据—传递值对象
  9. Quercus
  10. 【总算解决了】A network-related or instance-specific error occurred while establishing a connection to SQL Server
  11. Oracle第一波
  12. 【一天一道LeetCode】#35. Search Insert Position
  13. [Swift]LeetCode118. 杨辉三角 | Pascal&#39;s Triangle
  14. c++11の死锁
  15. 移动端调试神器 whistle
  16. Mvc校验用户没有登录就跳转的实现
  17. WEB 前端插件整理
  18. Mac OS X 下优化 Terminal,一篇就够了!
  19. 【LeetCode232】 Implement Queue using Stacks★
  20. springmvc常用注解标签详解-推荐

热门文章

  1. Ip和long互转
  2. 简明python教程 --C++程序员的视角(七):异常
  3. Unix/Linux环境C编程新手教程(30) 字符串操作那些事儿
  4. 第十四章 Executors源码解析
  5. uva 10160 Servicing Stations(DFS+剪枝)
  6. 【Other】最近在研究的, Java/Springboot/RPC/JPA等
  7. Reinvent the Wheel Often
  8. C++ RegCreateKeyEx成功了,但是注册表并没有这一项
  9. PHP OAuth 2.0 Server
  10. PHPCMS部件