大体题意:
告诉你一个n*m的矩形,在告诉你 有k 个传感器位于矩形内部,你从(0,0)位置发射一个东西,只能斜着跑,求到达每一个传感器的最短时间,不能到达输出-1,碰到四个角落退出!否则一指弹!
思路:
充分反应了自己能力的一道题目= =~
没有瞎搞出来,赛后补得!
可以建立一个map<pair<int,int>,set<int> >的东西,前一个pair 表示直线,记录直线的斜率k和b(忘了叫啥了= =)
第二个set记录的是 这个直线经过的输入的传感器标号,传感器放到结构体Node里面!
为了快速计算!我们可以知道 起始坐标和 起始方向,来算出终点坐标,在把这条线所对应所有的传感器 都给他们赋值一个时间,这个时间肯定是最短的,然后删掉这些点,这样 当下次在访问到这条线时,他已经空了,空了表示 重复访问这条线了 ,会形成死循环,需要break了!
在一点注意时间开long long 即可!
就是知道起始坐标和方向算终点坐标比较麻烦!
#include <bits/stdc++.h>
#define fi first
#define se second
#define mr make_pair
using namespace std;
const int maxn = 100000 + 10;
const int inf = 0x3f3f3f3f;
bool vis[maxn][4];
typedef long long ll;
struct Node{
int x,y;
ll v;
}p[maxn];
int n,m,K;
map<pair<int,int> ,set<int> >mp;
bool out(int &x,int &y){
if (x == 0 && y == 0 || x == 0 && y == m || x == n && y == 0 || x == n && y == m)return 1;
return 0;
}
set<int>::iterator it;
//============================================getnode
Node getNode(int x,int y,int k){
Node e;
int b = y-k*x;
if (x>=0 && x <= n && y == 0){
if (k == 1){
int ty = k*n+b;
if (ty > m){
e.y = m;
e.x = (m-b)/k;
}
else {
e.x = n;
e.y = ty;
}
}
else {
int ty = b;
if (ty > m){
e.y = m;
e.x = (m-b)/k;
}
else {
e.y = ty;
e.x = 0;
}
}
}
else if (x == n && y >= 0 && y <= m){
if (k == 1){
int tx = (-b)/k;
if (tx < 0){
e.x = 0;
e.y = b;
}
else {
e.x = tx;
e.y = 0;
}
}
else {
int ty = b;
if (ty > m){
e.y = m;
e.x = (m-b)/k;
}
else {
e.x = 0;
e.y = ty;
}
}
}
else if (y == m && x >= 0 && x <= n){
if (k == 1){
int tx = (-b)/k;
if (tx < 0){
e.x = 0;
e.y = b;
// printf("hjah\n");
}
else {
e.y = 0;
e.x = tx;
// printf("hha\n");
}
}
else {
int tx = (-b)/k;
if (tx > n){
e.x = n;
e.y = k*n+b;
}
else {
e.x = tx;
e.y = 0;
}
}
}
else if (x == 0 && y >= 0 && y <= m){
if (k == 1){
int ty = k*n+b;
if (ty > m){
e.y = m;
e.x = (m-b)/k;
}
else {
e.x = n;
e.y = ty;
}
}
else {
int ty = k*n+b;
if (ty < 0){
e.y = 0;
e.x = (-b)/k;
}
else {
e.x = n;
e.y = ty;
}
}
}
return e;
}
//============================================= 以上的函数 是知道初始坐标 和方向 计算终止坐标 分类讨论即可!
int main(){
scanf("%d %d %d",&n,&m,&K);
for (int i = 0; i < K; ++i){
int u,v;
scanf("%d %d",&u,&v);
p[i].x = u;
p[i].y = v;
p[i].v = -1; // 时间初始化为-1,表示没赋值!
mp[mr(1,v-u)].insert(i); // 经过这个点的两条直线 加入这个点
mp[mr(-1,u+v)].insert(i);
}
int x=0,y = 0,k = 1;
int lef=K;
ll tim=0ll;
while(1){
Node e = getNode(x,y,k);
if (!mp.count(mr(k,y-k*x)) ){//这条直线并没有经过传感器 就只增加时间 继续下次运动
tim+=abs(e.x-x);
k = -k;
x=e.x;
y=e.y;
continue;
}
if (mp[mr(k,y-k*x)].empty())break; // 再次访问到这条直线,已经变空了,说明重复访问了!
set<int>& tmp = mp[mr(k,y-k*x)];
for (;!tmp.empty() ;){
int id = *(tmp.begin());
--lef;
if (p[id].v == -1)p[id].v = tim + abs(p[id].x-x);
tmp.erase(tmp.begin());//统计一个删除一个!
}
tim += abs(e.x-x);
x = e.x; // 移动起点 ,更换方向 继续下次运动!
y = e.y;
k = -k;
if (out(e.x,e.y))break;// 到达四个角落退出
}
for (int i = 0; i < K; ++i){
printf("%lld\n",p[i].v);
}
return 0;
}
There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.
Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.
At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
The first line of the input contains three integers n, m and k (2 ≤ n, m ≤ 100 000, 1 ≤ k ≤ 100 000) — lengths of the room's walls and the number of sensors.
Each of the following k lines contains two integers xi and yi (1 ≤ xi ≤ n - 1, 1 ≤ yi ≤ m - 1) — coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
3 3 4 1 1 1 2 2 1 2 2
1 -1 -1 2
3 4 6 1 1 2 1 1 2 2 2 1 3 2 3
1 -1 -1 2 5 -1
7 4 5 1 3 2 2 5 1 5 3 4 3
13 2 9 5 -1
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after3 seconds.
![](https://i-blog.csdnimg.cn/blog_migrate/37f4c4d0ade4421ad4b6787faaaaad0e.png)
In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2),(1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4),(0, 2), (2, 0) and (3, 1).
![](https://i-blog.csdnimg.cn/blog_migrate/855bb42b1b9cd536c2c1d9d529896208.png)