UVa 808 (建坐标系、找规律) Bee Breeding
题意:
如图,按照图中的规律给这些格子编号。给出两个格子的编号,求从一个格子到另一个格子的最少步数。(一步只能穿过有有公共边的格子)

分析:
根据高中数学知识,选任意两个不共线的向量,就能表示平面上所有点。

像这样建立坐标系,根据图中的规律算出所有点的坐标。
推理也好,找找规律也好,不难发现
- 第一、三象限的点(x, y)到原点的距离为 |x| + |y|
- 第二、四象限的点(x, y)到原点的距离为 max{|x|, |y|}
递推点的坐标的时候也是比较注意细节的,否则很容易出错。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = ; struct Point
{
int x, y;
Point(int x=, int y=):x(x), y(y) {}
}p[maxn + ]; int dx[] = {-, -, , , , }; //六个方向
int dy[] = { , , , , -, -};
int pos; void cal(int dir, int l) //按照dir方向递推l个格子的坐标
{
pos++;
while(l--)
{
p[pos] = Point(p[pos-].x+dx[dir], p[pos-].y+dy[dir]);
pos++;
}
pos--;
} void Init()
{
p[] = Point(, -);
pos = ;
for(int l = ; l <= ; ++l)
{
for(int dir = ; dir < ; ++dir)
cal(dir, l);
cal(, l+);
cal(, l);
}
} int main()
{
Init(); int n, m;
while(scanf("%d%d", &n, &m) == && n)
{
int x = p[n].x - p[m].x;
int y = p[n].y - p[m].y;
int ans;
if((x<&&y>) || (x>&&y<)) ans = max(abs(x), abs(y));
else ans = abs(x+y); printf("The distance between cells %d and %d is %d.\n", n, m, ans);
} return ;
}
代码君
UVa 808 (建坐标系、找规律) Bee Breeding的更多相关文章
- UVA - 808 Bee Breeding (建立坐标系&找规律)
题目: 输入两个格子的编号a和b(a,b≤10000),求最短距离.例如,19和30的距离为5(一条最短路是19-7-6-5-15-30). 思路: 如图建立坐标系,然后看两个点的向量如果位于二四象限 ...
- UVa 1620 Lazy Susan (找规律)
题意:给 n 个数,每次可以把4个连续的数字翻转,问你能不能形成1-n的环状排列. 析:找一下奇偶性,写几个数试试,就会找到规律. 代码如下: #include <cstdio> #inc ...
- Uva 11549 - Calculator Conundrum 找规律加map
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 12627 (递归 计数 找规律) Erratic Expansion
直接说几个比较明显的规律吧. k个小时以后,红气球的个数为3k. 单独观察一行: 令f(r, k)为k个小时后第r行红气球的个数. 如果r为奇数,f(r, k) = f((r+1)/2, k-1) * ...
- UVA 10706 Number Sequence (找规律 + 打表 + 查找)
Problem B Number Sequence Input: standard input Output: standard output Time Limit: 1 second A singl ...
- UVA 11489 - Integer Game(找规律)
题目链接 #include <cstdio> #include <cstring> #include <string> #include <cmath> ...
- Paper Folding UVA - 177 模拟+思路+找规律
题目:题目链接 思路:1到4是很容易写出来的,我们先考虑这四种情况的绘制顺序 1:ru 2:rulu 3:rululdlu 4:rululdluldrdldlu 不难发现,相较于前一行,每一次增加一倍 ...
- 【数论,找规律】Uva 11526 - H(n)
原来做过的题再看还是没想出来,看来当时必然没有真正理解.这次回顾感觉理解更透彻了. 网上的题解差不多都是一个版本,而且感觉有点扯.根据n=20猜出来的? 好吧哪能根据一个就猜到那么变态的公式.其实这题 ...
- GCD XOR UVA 12716 找规律 给定一个n,找多少对(a,b)满足1<=b<=a<=n,gcd(a,b)=a^b;
/** 题目:GCD XOR UVA 12716 链接:https://vjudge.net/problem/UVA-12716 题意:给定一个n,找多少对(a,b)满足1<=b<=a&l ...
随机推荐
- MSP430F4152串口操作
/**********************************************************************/ /* 名称:串口通讯 功能:将接到的数据组后原封不 ...
- Objective-C中class、Category、Block的介绍
@class 当定义一个类,必须为编译器提供两组消息,第一组(接口部分.h):构建类的实例的一个基本蓝图.必须指定类名,类的超类,类的实例变量和类型的列表,最后是类的方法的声明.第二组(实现部分.m) ...
- 4、WPF应用程序的启动
启动第一步: 启动第二步 启动第三步:
- Create a SharePoint Application Page for Anonymous Access
http://dishasharepointworld.blogspot.com/2011/07/how-to-create-sharepoint-application_1072.html http ...
- JAVA-前台编码,后台解码
前台 var objValue =window.encodeURI(window.encodeURI(queryObj)); alert(objValue); 后台 String descStr = ...
- [Oracle]any, all解析
因为很少用到, 所以几乎忘记了这几个函数, 不过它们还是很有用的使用它们可以大大简化一些SQL文的语法, 至于效率问题, 如CCW所说它们和EXISTS, IN 之类没有什么差别, 而且要具体问题具体 ...
- install Nagios on Unbuntu Unix
Ubuntu Quickstart Up To: ContentsSee Also: Quickstart Installation Guides, Security Considerations I ...
- Howto: Deploy VC2008 apps without installing vcredist_x86.exe
There are several reasons for xcopy deployment of an application (also known as application local). ...
- Ubuntu Server 中resolv.conf重启时被覆盖的问题
/etc/resolv.conf中设置dns之后每次重启Ubuntu Server时该文件会被覆盖,针对这种情况找了一些个解决方法 防止/etc/resolv.conf被覆盖的方法 方法一 1.需要创 ...
- 封装SqlHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...