Intel Code Challenge Final Round C 大模拟
来源:互联网 发布:淘宝银泰百货是正品吗 编辑:程序博客网 时间:2024/04/29 16:04
题目传送门:http://codeforces.com/contest/724/problem/C
题意:一个n*m(10^5)的矩阵,坐标分别是0......m和0......n,一信号从(0,0)点向(1,1)方向以sqrt(2)的速度发射。碰到墙壁会反射,碰到墙角会停止。矩阵内有K个信号器,问到达每个信号器的时间(如果多次到达,以第一次为准,没到达输出-1)
思路:这题是个大模拟,先预处理求出到达每条边上点的时间(四个10^5数组),然后再对每个询问进行处理。
我写的代码巨长,一共四个方向,每个方向里又有很多情况,我全枚举出来可,里面重复的代码太多,应该写成函数可以减少点代码量。
代码如下:
#include <iostream>#include <algorithm>#include <cstring>#include <stdio.h>#include <string>#include <cmath>#include <queue>#include <set>#include <map>#include <stack>#include <bitset>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define ull unsigned long long#define mem(n,v) memset(n,v,sizeof(n))#define MAX 100005#define MAXN 10005#define PI 3.1415926#define E 2.718281828459#define opnin freopen("text.in.txt","r",stdin)#define opnout freopen("text.out.txt","w",stdout)#define clsin fclose(stdin)#define clsout fclose(stdout)#define haha1 cout << "haha1"<< endl#define haha2 cout << "haha2"<< endl#define haha3 cout << "haha3"<< endlconst int INF = 0x3f3f3f3f;const ll INFF = 0x3f3f3f3f3f3f3f3f;const double pi = 3.141592653589793;const double inf = 1e18;const double eps = 1e-8;const ll mod = 1e18;const ull mx = 133333331;/**************************************************************************/ll border[4][MAX];int n,m;void getdir(int &x,int &y){ if(x == 1) x = 0; else if(x == n) x= 2; else if(y == m){ y = x,x = 1; } else if(y == 1){ y = x,x = 3; }}void solve(int &x,int &y,int &x1,int &y1,int &temp){ if(x == 1){ if(y1 > y){ temp = min(m-y,n-x); y += temp; x += temp; if(m-y >= n-x){ y1 = y+1; x1 = x-1; } else{ y1 = y-1; x1 = x+1; } } else{ temp = min(y-1,n-x); y -= temp; x += temp; if(y-1 >= n-x){ y1 = y-1; x1 = x-1; } else{ y1 = y+1; x1 = x+1; } } } else if(y == m){ if(x1 > x){ temp = min(y-1,n-x); y -= temp; x += temp; if(y-1 >= n-x){ y1 = y-1; x1 = x-1; } else{ y1 = y+1; x1 = x+1; } } else{ temp = min(y-1,x-1); y -= temp; x -= temp; if(y-1 >= x-1){ y1 = y-1; x1 = x+1; } else{ y1 = y+1; x1 = x-1; } } } else if(x == n){ if(y1 > y){ temp = min(m-y,x-1); y += temp; x -= temp; if(m-y >= x-1){ y1 = y+1; x1 = x+1; } else{ y1 = y-1; x1 = x-1; } } else{ temp = min(y-1,x-1); y -= temp; x -= temp; if(y-1 >= x-1){ y1 = y-1; x1 = x+1; } else{ y1 = y+1; x1 = x-1; } } } else if(y == 1){ if(x1 > x){ temp = min(m-y,n-x); y += temp; x += temp; if(m-y >= n-x){ y1 = y+1; x1 = x-1; } else{ y1 = y-1; x1 = x+1; } } else{ temp = min(m-y,x-1); y += temp; x -= temp; if(m-y >= x-1){ y1 = y+1; x1 = x+1; } else{ y1 = y-1; x1 = x-1; } } }}int main(){ int k; mem(border,-1); cin >> n >> m >> k; n += 1; m += 1; int x = 1,y =1; int x1= 2,y1 = 2; ll time = 0; border[0][1] = 0; border[3][1] = 0; do{ int temp; solve(x,y,x1,y1,temp); time += temp; if(x == 1 && border[0][y] == -1) border[0][y] = time; if(y == m && border[1][x] == -1) border[1][x] = time; if(x == n && border[2][y] == -1) border[2][y] = time; if(y == 1 && border[3][x] == -1) border[3][x] = time; }while((!((x == 1 && y == 1) || (x == n && y == m) || (x == 1 && y == m) || (x == n && y == 1)))); for(int i=1;i<=k;i++){ scanf("%d %d",&x,&y); x += 1; y += 1; int q = x -1,w = m-y,e = n-x,r = y-1; int Min = min(min(min(q,w),e),r); ll ans = INFF; if(q == Min){ int x1 = x,y1 = y,x2 = x,y2 = y,temp; x1 -= Min; y1 -= Min; int x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[0][y11] != -1 && border[x1][y1] != -1){ if(border[0][y11] > border[x1][y1]){ ans = min(ans,border[0][y11]-Min); } else ans = min(ans,border[0][y11]+Min); } x1 = x,y1 = y,x2 = x,y2 = y; x1 -= Min; y1 += Min; x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[0][y11]!= -1 && border[x1][y1]!= -1){ if(border[0][y11] > border[x1][y1]){ ans = min(ans,border[0][y11]-Min); } else ans = min(ans,border[0][y11]+Min); } } else if(w == Min){ int x1 = x,y1 = y,x2 = x,y2 = y,temp; x1 -= Min; y1 += Min; int x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[1][x11]!= -1 && border[x1][y1]!= -1){ if(border[1][x11] > border[x1][y1]){ ans = min(ans,border[1][x11]-Min); } else ans = min(ans,border[1][x11]+Min); } x1 = x,y1 = y,x2 = x,y2 = y; x1 += Min; y1 += Min; x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[1][x11]!= -1 && border[x1][y1]!= -1){ if(border[1][x11] > border[x1][y1]){ ans = min(ans,border[1][x11]-Min); } else ans = min(ans,border[1][x11]+Min); } } else if(e == Min){ int x1 = x,y1 = y,x2 = x,y2 = y,temp; x1 += Min; y1 -= Min; int x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[2][y11]!= -1 && border[x1][y1]!= -1){ if(border[2][y11] > border[x1][y1]){ ans = min(ans,border[2][y11]-Min); } else ans = min(ans,border[2][y11]+Min); } x1 = x,y1 = y,x2 = x,y2 = y; x1 += Min; y1 += Min; x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[2][y11]!= -1 && border[x1][y1]!= -1){ if(border[2][y11] > border[x1][y1]){ ans = min(ans,border[2][y11]-Min); } else ans = min(ans,border[2][y11]+Min); } } else if(r == Min){ int x1 = x,y1 = y,x2 = x,y2 = y,temp; x1 -= Min; y1 -= Min; int x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[3][x11] != -1&& border[x1][y1]!= -1){ if(border[3][x11] > border[x1][y1]){ ans = min(ans,border[3][x11]-Min); } else ans = min(ans,border[3][x11]+Min); } x1 = x,y1 = y,x2 = x,y2 = y; x1 += Min; y1 -= Min; x11 = x1,y11 = y1; solve(x1,y1,x2,y2,temp); getdir(x1,y1); if(border[3][x11] != -1 && border[x1][y1] != -1){ if(border[3][x11] > border[x1][y1]){ ans = min(ans,border[3][x11]-Min); } else ans = min(ans,border[3][x11]+Min); } } if(ans != INFF) cout << ans << endl; else cout << "-1" <<endl; }}
0 0
- Intel Code Challenge Final Round C 大模拟
- Intel Code Challenge Final Round C. Ray Tracing【模拟】
- Codeforces Round Intel Code Challenge Final Round C. Ray Tracing
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 模拟
- Intel Code Challenge Final Round C. Ray Tracing 拓展欧几里得或模拟 (好题)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 模拟+预处理、几何
- Codeforces Intel Code Challenge Final Round C.Ray Tracing
- Intel Code Challenge Final Round C.Ray Tracing
- Intel Code Challenge Final Round B 暴力
- Intel Code Challenge Final Round D 贪心
- Codeforces Round Intel Code Challenge Final Round B.Batch Sort
- Codeforces Round Intel Code Challenge Final Round D. Dense Subsequence
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)【A,B,C,D】
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- C. Ray Tracing(STL乱搞)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
- fenzhifa
- Part 3 - Using custom view engines with asp.net mvc
- Ajax请求中的async:false/true的作用
- AsyncTask用法
- 【ReLU】Rectified Linear Units, 线性修正单元激活函数
- Intel Code Challenge Final Round C 大模拟
- python 根据网址获取股票
- SpringMVC - 方法中的各种参数绑定方式
- UTL_RAW.CAST_TO_RAW解决不同字符集的数据库之间的相互访问
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot exe
- 相邻元素差的绝对值都是1,在这样的数组中找目标元素
- 包含继承的初始化顺序总结
- MySQL/Mybatis多个AND和OR混用注意事项
- android studio点击运行按钮出现:Unable to locate adb