Codeforces 724 C. Ray Tracing

Codeforces 724 C. Ray Tracing

题目来源

题意:

有一些传感器按输入坐标分布在图上,有一道光从(0,0)沿45°角出射,遇到边按反射法则反射,遇到角落结束。
问每一个点被经过的时间,并按照输入顺序输出。

题解:

图上的每一个点都可以按照一定的对应关系对应到 y=x 直线上的点上去,模拟一遍可过
或者按照大牛们的做法,同余方程组(扩展欧几里得)

瓦日,辣么久了才发现这里发的题解代码贴错了。。。MMP哦

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;
vector<int> a[maxn*2],b[maxn*2];
ll ans[maxn];
pair<int,int> po[maxn];
const int dir[4][2]={1,1,1,-1,-1,-1,-1,1};

int main()
{
#ifndef ONLINE_JUDGE
    //freopen("C.in","r",stdin);
    //freopen("C.out","w",stdout);
#endif
	int n,m,k;
	cin >> n >> m >> k;
	memset(ans,-1,sizeof(ans));
	for(int i=0,x,y;i<k;i++)
	{
		cin >> x >> y;
		po[i]=make_pair(x,y);
		a[x-y+maxn].push_back(i);
		b[x+y].push_back(i);
	}
	int x=0,y=0,id=0;
	ll t=0;
	while(1)
	{
		if(id&1)
		{
			for(auto u:b[x+y])
				if(!~ans[u])
					ans[u]=t+abs(po[u].first-x);
			b[x+y].clear();
		}
		else
		{
			for(auto u:a[x-y+maxn])
				if(!~ans[u])
					ans[u]=t+abs(po[u].first-x);
			a[x-y+maxn].clear();
		}
		int u=dir[id][0];
		int v=dir[id][1];
		int tx=u>0?n-x:x;
		int ty=v>0?m-y:y;
		if(tx==ty)
			break;
		if(tx<ty)
		{
			if(id&1)
				(id+=1)&=3;
			else
				(id+=3)&=3;
			x+=tx*u;
			y+=tx*v;
			t+=tx;
		}
		else
		{
			if(id&1)
				(id+=3)&=3;
			else
				(id+=1)&=3;
			x+=ty*u;
			y+=ty*v;
			t+=ty;
		}
	}
	for(int i=0;i<k;i++)
		cout << ans[i]<<endl;
	return 0;
}
posted @ 2016-10-09 14:41  里巴鲁鲁  阅读(746)  评论(0编辑  收藏  举报