UVA 10620 - A Flea on a Chessboard(鸽笼原理)
UVA 10620 - A Flea on a Chessboard
题意:给定一个跳蚤位置和移动方向。如今在一个国际象棋棋盘上,左下角为黑格,一个格子为s*s,推断是否能移动到白格子。问要移动多少次才干到白格,边界不算白格。
思路:利用鸽笼原理落在黑格子和边界上的一共同拥有(s + 1)^2个点,也就是说。假设形成循环,周期肯定在这之内。所以能够不断去模拟跳格子,直到踩到白格,或者踩到之前落到过的黑格就结束
只是事实上还有更优的方法,由于跳蚤跳跃路径为直线,观察下能够发现假设能够到白格,最多连成一条小于(2 x 2)格子对角线长度的线,那么上面一共同拥有不超过2 * s个黑点,于是事实上仅仅须要推断前2s步能不能到白格就能够了,假设不能。说明必定中间形成了周期,也就是始终到不了白格了
代码:
原来的:
#include <stdio.h>
#include <string.h> const int N = 1005;
bool vis[N][N];
long long s, x, y, dx, dy; bool white() {
if (x % s == 0 || y % s == 0) return false;
if (((x / s)&1)^((y / s)&1)) return true;
return false;
} bool in() {
if (x > s || y > s) {
x -= s; y -= s;
if (x < 0) x = s;
if (y < 0) y = s;
}
if (vis[x][y]) return false;
vis[x][y] = true;
return true;
} int main() {
while (~scanf("%lld%lld%lld%lld%lld", &s, &x, &y, &dx, &dy) && s) {
memset(vis, false, sizeof(vis));
long long ans = 0, ansx = x, ansy = y;
while (1) {
x %= 2 * s;
y %= 2 * s;
if (white()) {
printf("After %lld jumps the flea lands at (%lld, %lld).\n", ans, ansx, ansy);
break;
}
if (!in()) {
printf("The flea cannot escape from black squares.\n");
break;
}
x += dx;
y += dy;
ansx += dx;
ansy += dy;
ans++;
}
}
return 0;
}
简化后的:
#include <stdio.h>
#include <string.h> long long s, x, y, dx, dy; bool judge(long long x, long long y) {
return (x % s && y % s && (x / s + y / s) % 2);
} int main() {
while (~scanf("%lld%lld%lld%lld%lld", &s, &x, &y, &dx, &dy) && s) {
int i;
for (i = 0; i < 2 * s; i++) {
if (judge(x, y)) {
printf("After %d jumps the flea lands at (%lld, %lld).\n",i,x,y);
break;
}
x += dx; y += dy;
}
if (i == 2 * s) printf("The flea cannot escape from black squares.\n");
}
return 0;
}
UVA 10620 - A Flea on a Chessboard(鸽笼原理)的更多相关文章
- HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场
题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...
- Gym 100851G Generators (vector+鸽笼原理)
Problem G. Generators Input file: generators.in Output file: generators.outLittle Roman is studying li ...
- POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)
描述 http://poj.org/problem?id=1065 木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次). Wo ...
- poj 3370 鸽笼原理知识小结
中学就听说过抽屉原理,可惜一直没机会见识,现在这题有鸽笼原理的结论,但其实知不知道鸽笼原理都可以做 先总结一下鸽笼原理: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两 ...
- poj 2356鸽笼原理水题
关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...
- CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)
题目链接 Points Inside A Polygon 题意 给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...
- 1393 0和1相等串 鸽笼原理 || 化简dp公式
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...
- Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)
To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...
- UVA 11237 - Halloween treats(鸽笼原理)
11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...
随机推荐
- HDU 4612 Warm up tarjan 树的直径
Warm up 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4612 Description N planets are connected by ...
- HDU 5296 Annoying problem dfs序 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5296 Description Coco has a tree, w ...
- Markdown 简明语法手册 - 作业
目录 Cmd Markdown 简明语法手册 1. 内容目录 2. 标签分类 3. 删除线 水平线--- 1. 斜体和粗体 2. 分级标题 标题1 标题2 标题3 3. 外链接 4. 无序列表 5. ...
- JavaScript Diagramming
JavaScript Diagramming Optensity
- Django Pagination
Django provides a few classes that help you manage paginated data – that is, data that’s split acros ...
- SharePoint 2013 排错之"Code blocks are not allowed in this file"
今天,设置页面布局的自己定义母版页时,设置完了以后保存,然后预览报错,错误例如以下截图:删掉自己定义母版页的MasterPageFile属性,页面依旧报错:感觉甚是奇怪,由于有版本号控制,还原为最初的 ...
- Programmable current source requires no power supply
Engineering labs are usually equipped with various power supplies, voltmeters, function generators, ...
- 擦亮自己的眼睛去看SQLServer之谈谈锁机制
http://www.cnblogs.com/yueyue_jwfm/category/292724.html
- WinForm中DataGridView验证单元格输入的是数字
转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGr ...
- windows系统上安装与使用Android NDK r8d(二)
四. 在eclipse中集成c/c++开发环境 1. 装Eclipse的C/C++环境插件:CDT,这里选择在线安装. 首先登录http://www.eclipse.or ...