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& ...
随机推荐
- Java解释执行和编译执行
以前有句话说:“Java是解释执行的 ” .现在看来确实不是很准确,至于原因,在此简略解释: 首先,我们先解释一下在Java中解释执行和编译执行的区别. 解释执行:将编译好的字节码一行一行地翻译为机器 ...
- bzoj 2342: [Shoi2011]双倍回文 -- manacher
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符 ...
- bzoj1123 Blockade
Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通. Input 输入n&l ...
- Map中keySet和entrySet的区别
在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...
- ExtJS中实现嵌套表格
先看效果: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
- STM32 Hardware Development
http://www.st.com/web/en/resource/technical/document/application_note/CD00164185.pdf AN2586 http://w ...
- HTTP Error 404.2 - Not Found The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server(转)
今天公司的同事問我,為什麼同一支程式在自己的電腦OK,部署到Server上會出現下面的錯誤 我想,沒有錯啊~ 我在這台Server所部署的程式一向都是OK的 看了錯誤的Error page, 發現是I ...
- The maximum number of processes for the user account running is currently , which can cause performance issues. We recommend increasing this to at least 4096.
[root@localhost ~]# vi /etc/security/limits.conf # /etc/security/limits.conf # #Each line describes ...
- 将Maven2项目转为MyEclipse项目
现在项目中,大家开始用jetty.它不用像在MyEclipse中使用Tomcat那样要部署,也不用像在Tomcat中那样,要把应用都放到webapp文件夹下.jetty可以直接用你的项目的目录结构. ...
- 深入JavaScript模块化编程
今天看requirejs官网的manual,发现了下面这篇好文章,于是花点时间翻译了一下,翻译不好的地方请指正,谢谢! 推荐阅读原文:) http://www.adequatelygood.com ...