Codeforces 724C Ray Tracing 模拟

题意:给你一张n*m的图,横坐标从0到m,纵坐标从0到n,直线y=n,y=0,x=0,x=m都是墙,现有光束从0,0出发向1,1射去,每进过一个点耗费1秒,当遇到墙角时,光束停止,当射到墙面上时,反射,入射角等于出射角,先有k个传感器在不同的点上,问光束第一次经过这些点的时间,若不经过输出-1。

题解:这题真是卡了我半天,真的是半天,我先按点一个一个搜t在test7,再按边搜然后对于每条边找一遍有没有传感器在上面,又t在了8,当然啦,这样的姿势一看就是不正确的,然而蒟蒻的我还是连踩两发坑,那么正确的思路应该是先求出每个传感器所在直线y=x+b,y=-x+b的截距。然后按边搜,找出每条边的截距,然后再通过截距找对应的传感器.

下面是正确的代码。

#include
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
const LL INF=0x3f3f3f3f3f3f3f3fll;
struct node {
  int x,y;
  LL step;
  int dir;
  int id;
};
int vis[MAXN];
typedef pair PAIR;
LL ans[MAXN];
map > >sz,sf;
void judge(node now) {
  int b;
  int flag=0;
  if (now.dir==1||now.dir==2) b=now.y-now.x;
  else b=now.x+now.y,flag=1;
  if (flag==1) {
    for (auto it :sf[b]) {
      int rx=it.first,id=it.second;
      ans[id]=min(ans[id],now.step+abs(now.x-rx));
    }
    sf.erase(b);
  }
  else {
    for (auto it : sz[b]) {
      int rx=it.first,id=it.second;
      ans[id]=min(ans[id],now.step+abs(now.x-rx));
    }
    sz.erase(b);
  }
}


int main() {
  int n,m,k;
  scanf("%d %d %d",&m,&n,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    swap(x,y);
    sz[y-x].push_back(PAIR(x,i));
    sf[y+x].push_back(PAIR(x,i));
  }
  MEMINF(ans);
  node now;
  now.x=0,now.y=0,now.dir=1,now.step=0;
  int mx,my;
  bool flag=false;
  int cnt=0;
  while (true) {
    node lim;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u


接下来是两发t的,写了比较长时间不舍得删掉,哈哈。

#include
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
  int id;
};
int dx[4]={1,1,-1,-1};
template
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template 
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
int dy[4]={-1,1,-1,1};

int vis[MAXN];
typedef pair PAIR;
typedef map mpp;
mpp mp;
queueq;
int qsize;
double k1,k2;

void judge(node u,node lim) {
  int k=qsize;
  for(int i=0; i1e5) break;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u


#include
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
};
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int ans[MAXN];
bool vis[MAXN];
typedef pair PAIR;
typedef map mpp;
mpp mp;

int main() {
  int n,m,k;
  scanf("%d %d %d",&n,&m,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    mp[PAIR(x,y)]=i;
  }
  node pre,now;
  MEM(vis,0);
  MEM(ans,0);
  now.x=1,now.y=1,now.step=1,now.dir=1;
  int flag=0;

  int cnt=0;
  
  while(true) {
    mpp::iterator it; 
    it=mp.find(PAIR(now.x,now.y));
    int v=it->second;
    if (v&&!vis[v]) {
      ans[v]=now.step;
      vis[v]=1;
      flag++;
    }
    if (cnt>1e5)break;
    if (flag==k) break;
    now.x+=dx[now.dir];
    now.y+=dy[now.dir];
    now.step++;
    if (now.x==0&&now.y==0)break;
    if (now.x==n&&now.y==m) break;
    if (now.x==0&&now.y==m) break;
    if (now.x==n&&now.y==m)break;

    if (now.x==0) {
      if (now.dir==3) now.dir=1;
      else if (now.dir==2) now.dir=0;
    }
    else if (now.x==n) {
      if (now.dir==1) now.dir=3;
      else if (now.dir==0) now.dir=2;
    }
    else if (now.y==0) {
      if (now.dir==2) now.dir=3;
      else if (now.dir==0) now.dir=1;
    }
    else if (now.y==m) {
      if (now.dir==3) now.dir=2;
      else if (now.dir==1) now.dir=0;
    }
  }
  for (int i=1; i<=k; ++i) {
    if (ans[i]==0)puts("-1");
    else printf("%d\n",ans[i]);
  }
}




你可能感兴趣的:(模拟)