在一个nxm的镜面二维空间内,向(1,1)发射一条射线,来回反射,当遇到四个角之一时光线消失。

给K个点,问K个点第一次被射中是什么时候(v = sqrt(2))

解:注意到只有 2*(n+m)个对角线,从而从(1,1)发射光线后,最多折射O(n)此。

只需要模拟光线的折射即可。

具体实现时,记录光线的起点,终点。记录直线方程(截距和斜率的正负)

每一次用上一个终点和直线方程算出新的光线的终点。

(分为2*2*2 = 8种情况讨论)

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector> #define N 100010
#define INF 0x3f3f3f3f3f3f3f3fLL
#define LL long long using namespace std; struct node{
int x,y,id;
void scan(int i){
id=i;
scanf("%d%d",&x,&y);
}
}a[N]; struct line{
node S,T;
int typ,t;
}b[N]; int n,m,K;
LL ansv[N];
vector<node> A[][*N];
queue<line> q;
bool vis[][*N]; int Abs(int x){
if(x<) return -x;
return x;
} void update_min(LL &a,LL b){
if(b<a) a=b;
} bool get_next(line now){
if((now.T.x==||now.T.x==n) && (now.T.y==||now.T.y==m)) return ;
line ans;
ans.S=now.T;
ans.typ=now.typ^; if(ans.typ==){
ans.t = ans.S.x+ans.S.y; if(now.S.x < now.T.x){
if(now.T.y == m){
if(ans.t-n >= ) ans.T = (node){n,ans.t-n};
else ans.T = (node){ans.t,};
}
else ans.T = (node){ans.t-m,m};
}
else{
if(now.T.y == ){
if(ans.t <= m) ans.T = (node){,ans.t};
else ans.T = (node){ans.t-m,m};
}
else ans.T = (node){ans.t,};
}
} else{
ans.t = ans.S.y-ans.S.x+n; if(now.S.x < now.T.x){
if(now.T.y == ){
if(ans.t <= m) ans.T = (node){n,ans.t};
else ans.T = (node){m+n-ans.t,m};
}
else ans.T = (node){n-ans.t,};
}
else{
if(now.T.y == m){
if(ans.t <= n) ans.T = (node){n-ans.t,};
else ans.T = (node){,ans.t-n};
}
else ans.T = (node){m+n-ans.t,m};
}
}
// cout << "node " << ans.T.x << ' ' << ans.T.y << endl;
// cout << ans.typ << ' ' << ans.t << endl;
if(vis[ans.typ][ans.t]) return ;
q.push(ans);
return ;
} int main(){
// freopen("test.txt","r",stdin);
scanf("%d%d%d",&n,&m,&K);
for(int i=;i<=K;i++) a[i].scan(i);
if(n<m){
swap(n,m);
for(int i=;i<=K;i++)
swap(a[i].x,a[i].y);
} for(int i=;i<=K;i++){
A[][a[i].x+a[i].y].push_back(a[i]);
A[][a[i].y-a[i].x+n].push_back(a[i]);
ansv[i]=INF;
} vis[][n]=;
q.push((line){(node){,,}, (node){m,m,}, ,n});
LL ansnow=; while(!q.empty()){
line tmp=q.front(); q.pop(); vector<node> &now = A[tmp.typ][tmp.t];
for(int i=,ln=now.size();i<ln;i++)
update_min(ansv[now[i].id], ansnow + (LL)Abs(now[i].x-tmp.S.x) ); ansnow += (LL)Abs(tmp.T.x-tmp.S.x);
if(!get_next(tmp)) break;
} for(int i=;i<=K;i++)
if(ansv[i]!=INF) printf("%I64d\n",ansv[i]);
else puts("-1");
return ;
}

codeforces 724C的更多相关文章

  1. Codeforces 724C [坐标][乱搞][模拟]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 从(0,0)出发与x轴正方向呈45度角的射线,在给定的矩形区域内不断发射,直到射入矩形的某个角停止. 给出多个坐标,问光线最早经过某坐标的时间. ...

  2. codeforces 724c Ray Tracing

    好题 原题: There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is ...

  3. Codeforces 724C Ray Tracing 扩展欧几里得

    吐槽:在比赛的时候,压根就没想到这题还可以对称: 题解:http://blog.csdn.net/danliwoo/article/details/52761839 比较详细: #include< ...

  4. CodeForces 724C Ray Tracing(碰撞类,扩展gcd)

    又一次遇到了碰撞类的题目,还是扩展gcd和同余模方程.上次博客的链接在这:http://www.cnblogs.com/zzyDS/p/5874440.html. 现在干脆解同余模直接按照套路来吧,如 ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】

    呵呵哒,上分~ CodeForces 724A: 题意: 给你两个星期几,问连续两个月的头一天是否满足: #include <iostream> #include <stdio.h& ...

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. 算法 - 求两个自然数的最大公约数(C++)

    //************************************************************************************************** ...

  2. 完整的MVC框架(前端、后台和数据库)

    终于学完了数据库的连接,可以做一个完整的项目了,以前做的练习都没有关联到数据库,没法进行事务. MVC框架 先上图: 老师画的图,有点乱,但是大概意思还是可以理解. 这个练习是简单的存储一个学生读了哪 ...

  3. error LNK2019 无法解析的外部符号------类模板和内敛函数

    今天用类模型实现一个单链表,开始是.h和.cpp将类模板的声明与实现分开写的,结果总是报错: 错误 error LNK2019: 无法解析的外部符号 ?$SingleList@H@@QAE@XZ),该 ...

  4. 【Android小项目】找不同,改编自&quot;寻找房祖名&quot;的一款开源小应用。

    近期在微信朋友圈"寻找房祖名"和"万里寻刀"这类小游戏比較火.我试着写了一个android版本号的,里面全是一系列的形近字,实现原理非常easy:用一个Grid ...

  5. ZT:CSS实现水平|垂直居中漫谈

    有篇博客园网友‘云轩奕鹤’的文章不错,转载在这里以供需要时查阅. http://www.cnblogs.com/jadeboy/p/5107471.html

  6. 深入研究Clang(五) Clang Lexer代码阅读笔记之Lexer

    作者:史宁宁(snsn1984) Clang的Lexer(词法分析器)的源代码的主要位置例如以下: clang/lib/Lex    这里是基本的Lexer的代码: clang/include/cla ...

  7. PJzhang:python基础入门的7个疗程-two

    猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课-售价29元人民币 https://www.liaoxuefeng.com/wiki/1016959663602400 htt ...

  8. Mesa (computer graphics)

    http://en.wikipedia.org/wiki/Mesa_(computer_graphics) Mesa (computer graphics) From Wikipedia, the f ...

  9. libusb 源码阅读

    libusb_init(NULL), 如果传入一个NULL, 则libusb 内部会有一个 usbi_default_context 变量在内部保存上下文. 这样以后调用 libusb 函数时可以不指 ...

  10. 浅谈JavaScript的事件(事件委托)

    事件处理程序为Web程序提供了系统交互,但是如果页面中的事件处理程序太多,则会影响页面的性能.每个函数都是对象,都会占用内存,内存中对象越多,性能越差.需要事先为DOM对象指定事件处理程序,导致访问D ...