点击打开链接

There is an infinite beehive like the one given in the figure. We consider two cells to be adjacent if andonly if they share a side. A path of length k from cell c0 to cell ck is a sequence of cells c0, c1, . . . , cksuch that ci and ci+1 are adjacent for all 0 ≤ i < k. The distance between cells i and j is the length ofthe shortest path from cell i to cell j

The cells of the beehive are indexed using positive integers as shown. The cells with larger distancefrom cell 1 are given larger indices. The indices of cells with the same distance from cell 1 increasesfrom left to right. Each positive integer is the index of exactly one cell.We want to know the distance of two cells whose indices are given.

Input

There are multiple test cases in the input. Each test case is a single line containing two space-separatedintegers i and j as the indices of two cells (1 ≤ i, j ≤ 104). The input terminates with a line containing‘0 0’ which should not be processed as a test case.

output

For each test case, output a single line containing the distance of the given cells.

Sample Input

8 4

11 12

365 365

0 0

Sample Output

2

5

0

Ps:皇天不负有心人,调试了两天的程序终于ac出来了

题目大意:

在如上图所示的规律去摆放数字,求任意两个数字之间的最小距离

题解:首先根据这个图求出每个数的横纵坐标,可以发现是一圈一圈的摆放的,对于第一圈(也就是1),第二圈和第一圈相同,都是先在最左边的一列上放一个数,然后在往右放,到最右边的一列的时候在放一个,对于第2,3圈,可以看出是在最左边和最右边的列放两个,一次类推出其他数字的摆放过程,根据这个过程求出每个数字的坐标。

定义在一列上的两个相邻的数字相差2,斜着相邻的数字相差1

当两个数字在同一列上时dx==0,其最短距离就是dy/2,(除以2是因为我记录坐标是相差2),在同一行上时,最短距离就是dx.(特殊考虑)

如果dx=0,或者dy=0,或者两个格子在同一斜排(dx=dy),直接算即可dis=max(dx,dy)。 
可以看出来,任意不在一排的两个点都是可以看做一个平行四边形的对角顶点,而最近的走法就是在整个平行四边形内部走,而且,只要是在内部,不走回头路的话,走的距离都是一样的。 
所以,对于dx>dy的情况,距离dis=dx 
对于dy>dx的情况,dis=dx+(dy-dx)/2

#include <bits/stdc++.h>using namespace std;
struct node
{int lie;int hang;
} vis[211111];int d[211111];///记录每一列的当时高度(即hang )
int main()
{memset(vis,0,sizeof(vis));memset(d,0,sizeof(d));///初始化前两圈int n,m;vis[1].lie=100000;vis[1].hang=1;d[10000]=1;vis[2].lie=99999;vis[2].hang=2;d[99999]=2;vis[3].lie=100000;vis[3].hang=3;d[100000]=3;vis[4].lie=100001;vis[4].hang=2;d[100001]=2;int cnt=5;int ans=2;int l=99998,r=100002;while(1){///求出每个数字的坐标if(cnt>111111)break;for(int j=1; j>=0; j--)///每两圈最左边的数字高度加1{if(j==1)d[l]=-1;///数字高度(从1开始)else d[l]=0;///(从0开始)for(int i=0; i<ans; i++)///数字高度为ans加在最左列{
//                if(cnt==14)
//                {
//                    cout<<"lie="<<i<<endl;
//                    cout<<ans<<endl;
//                }d[l]+=2;vis[cnt].hang=d[l];vis[cnt++].lie=l;}for(int i=l+1; i<r; i++){
//                if(cnt==14)
//                {
//                    cout<<"lie="<<i<<endl;
//                }d[i]+=2;vis[cnt].hang=d[i];vis[cnt++].lie=i;}int k;if(j)k=ans*2-1;///每次上升2else k=ans*2;for(int i=0; i<ans; i++)///数字高度为ans加在最右列{
//                if(cnt==66)
//                {
//                    cout<<ans*2-1<<endl;
//                    cout<<k<<endl;
//                    cout<<ans<<endl;
//                }vis[cnt].hang=k;vis[cnt++].lie=r;k=k-2;}if(j)d[r]=ans*2-1;else d[r]=ans*2;l--;r++;}ans++;}//for(int i=1; i<=100; i++)printf("%d %d %d\n",i,vis[i].hang,vis[i].lie);while(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;int dx=abs(vis[n].lie-vis[m].lie);int dy=abs(vis[n].hang-vis[m].hang);if(dx==0)printf("%d\n",dy/2);else if(dy==0||dx==dy)printf("%d\n",max(dx,dy));else{if(dx>dy)printf("%d\n",dx);else printf("%d\n",dx+(dy-dx)/2);}}return 0;
}

Beehive UVALive - 7528 (找规律+数学思维)相关推荐

  1. 【每日一题Day35】LC878第N个神奇数字 | 二分查找 找规律 + 数学

    第N个神奇数字[LC878] 一个正整数如果能被 a 或 b 整除,那么它是神奇的. 给定三个整数 n , a , b ,返回第 n 个神奇的数字.因为答案可能很大,所以返回答案 对 109 + 7 ...

  2. [E找规律] lcLCP29. 乐团站位(找规律+数学)

    文章目录 1. 题目来源 2. 题目解析 1. 题目来源 链接:LCP 29. 乐团站位 相关:[Mbfs] lc54. 螺旋矩阵(bfs+方向数组) 2. 题目解析 经典问题,一看有手就行,结果不知 ...

  3. c语言摆动序列找规律,数学探秘【数学探秘数学头条】- 数学探秘知识点 - 中企动力...

    2018.4.13 二月二十七周⑤ "形与神俱"之科学和哲学实质探秘 作者:田合禄 北京中医药大学临床特聘教授 摘要 [目的]阐发"形与神俱"的由来.内涵及其科 ...

  4. 【HDU - 5882】Balanced Game (找规律,思维)

    题干: Rock-paper-scissors is a zero-sum hand game usually played between two people, in which each pla ...

  5. 【计蒜客 - 2019南昌邀请赛网络赛 - H】Coloring Game(找规律,思维dp)

    题干: David has a white board with 2 \times N2×N grids.He decides to paint some grids black with his b ...

  6. 【CodeForces - 735B】Urbanization (找规律,思维)

    题干: Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided ...

  7. 思维、找规律题目汇总

    转自:http://hi.baidu.com/liuzhe/blog/item/d2dc0fd10bf1eadb572c843d.html 感想: 1.还是数学有前途 2.猜想很重要 3.暴力打表可以 ...

  8. 数学/找规律/sgu 118 Digital root

    题意 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根 现在给出n个Ai,求出A1*A2*-*AN + A1*A2*-*AN-1 + - + A1*A2 + A ...

  9. 找出所有层级_数轴上的行程问题1:你的数学思维水平在哪一个层级?

    前言:对于很多学生来说,从小学六年级到初中一年级,数学学习仿佛一下子迷失了方向,相当长时间找不到切入点,无法适应初中数学的学习.究其根本原因,是思考问题的方式出了问题,小学熟知的算术思维迟迟不能跨越. ...

最新文章

  1. 简单使用DESeq2/EdgeR做差异分析
  2. SQL Server中的角色(服务器级别和数据库级别角色)
  3. 2020-11-24(Windows保护模式学习笔记(1)—— 段寄存器与段权限检查)
  4. Java面试必备知识点梳理:二分查找算法
  5. F2工作流引擎之 概述(一)
  6. Jmeter 接口自动化执行报错 无法找到类或者类的方法
  7. 三大运营商发5G白皮书:传统短信迎大升级 推出5G消息
  8. 【学习笔记】dubbo 控制台的部署
  9. 让生活服务“说到做到”,美团语音应用平台的底气在哪里?
  10. pe修改服务器2003密码,一文教你3分钟搞定忘记的windows密码
  11. matlab和ansys联合优化,MATLAB与HFSS联合建模与仿真优化详细介绍
  12. 在Fragment中使用Listview
  13. 创造与魔法怎么自建服务器,创造与魔法如何搭建出梦想中的房屋
  14. 计算机视觉领域常见期刊和会议
  15. 【通州爆料】通州台湖一村被征地建设轻轨L2线
  16. 黑名单电话自动拦截【Android】
  17. 由MAVEN入手浅谈项目构建与管理
  18. python发票二维码条码识别_Python zxing 库解析(条形码二维码识别)
  19. 封装的含义,作用,如何封装,什么情况下进行封装?
  20. 东方财富网爬虫及可视化

热门文章

  1. K8S之Deployment控制器管理应用(十一)
  2. win10显示服务器登录失,win10电脑user profile service服务登录失败怎么办
  3. matlab中ndgrid的意思,【ndgrid】什么意思_英语ndgrid的翻译_音标_读音_用法_例句_在线翻译_有道词典...
  4. maven多仓库配置——公司仓库和阿里仓库
  5. php中的文字排版问题,CSS布局中常用的文字排版相关属性详解
  6. 为什么找到的基址,是进程名+16进制的数
  7. 威联通QTS登录画面内容动态变更
  8. Java 性能监控和调优
  9. 解决360等杀毒软件阻止文件替换的问题!
  10. 算法工程师9——机器学习概述(下篇-算法进阶)