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& ...
随机推荐
- ElasticSearch学习笔记--1、安装以及运行
Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎,多的我就不细说了. 相关实验环境 Centos:7.3 ElasticSearch:5.6 java:1.8 1. ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...
- 两个函数彻底理解Lua中的闭包
本文通过两个函数彻底搞懂Lua中的闭包,相信看完这两个函数,应该能理解什么是Lua闭包.废话不多说,上 code: --[[************************************** ...
- HDU 5656 CA Loves GCD dp
CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...
- hdu 5232 Shaking hands 水题
Shaking hands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- jquery 常用获取值得方法汇总
jquery取radio单选按钮的值$("input[name='items']:checked").val();jquery radio取值,checkbox取值,select取 ...
- 原生+H5开发之:js交互【location方式】
1. 交互方式总结 1Android与JS通过WebView互相调用方法,实际上是: Android去调用JS的代码 JS去调用Android的代码 二者沟通的桥梁是WebView 对于Android ...
- object-c的http post请求之 ASIFormDataRequest使用
ASIHTTPRequest类库中的ASIFormDataRequest是实现HTTP协议中的处理POST表单的很好的类库.使用起来非常简单. 在说明之前先需要了解HTTP请求的Get和Post方法. ...
- SQL SERVER 锁2
http://blog.csdn.net/huwei2003/article/details/4047191 http://www.cnblogs.com/huangxincheng/category ...
- [js插件]学习Highcharts
引言 放了三天假,在家闲着没事,做了一个个人记账的web应用程序,其中一块就是数据统计的功能,也就学习了一下Highcharts. Highcharts Highcharts 是一个用纯JavaScr ...