sicily 1187. Laserbox

1187. Laserbox

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

A laserbox is a game involving some optical equipment. The game board is a square n×n grid. On each grid point, a gadget called a right-turner can be placed and several such gadgets are included. Finally, there is a ruby laser, and if the laser is mounted at the bottom end of a column, the beam will be directed northwards through that column. Analogously, the laser beam may be directed southwards from the top of a column, eastwards from the start of a row or westwards from the end of the row.
The game starts with some right-turners being spread out on some grid points and the laser (switched off) being mounted somewhere along the border of the rectangle. The player then tries to deduce where the beam will emerge when the laser is switched on. The effect of a right-turner is to deflect the beam ninety degrees to the right, regardless of from which of the four directions it enters.

Your program must do exactly what the player is supposed to do.

Input

On the first line of the input is a single positive integer, telling the number of test cases to follow. The first line of each test case consists of two integers n r, where 1 ≤ n ≤ 50 is the size of the board and 1 ≤ r ≤ 50 the number of right-turners. The following r lines contain the coordinates x y of the right-turners. No two right-turners will have the same coordinates.
Finally, a line with two integers indicating the laser position follows. The bottom of column six is denoted by 6 0 and the start of row seven by 0 7. If the zeroes are replaced by n+1, the laser is placed at the top of column six and the end of row seven, respectively.

Output

For each test case, output one line containing the coordinates X Y of the beam as it leaves the board. The same rules as for the laser apply, so X may equal 0 or n+1 or else Y equal 0 or n+1. If the beam gets caught and does not leave the board, the output should be 0 0.

Sample Input

2
2 3
1 1
1 2
2 2
3 1
3 6
1 1
1 3
2 2
2 3
3 1
3 2
2 0

Sample Output

2 0
0 2

题目分析

模拟激光反射的情况
激光从某个位置射进来,遇到机器人就向右反射
注意到这道题的横纵坐标是模拟数轴的,自己拿一个样例画个图就知道了
开三维数组来记录访问过的状态

#include <stdio.h>
#include <memory.h>

int dirs[4][2] = {0,1, 1,0, 0,-1, -1,0}; // N E S W
int x, y, dir;

void deal(int size) {
  if (x == 0) { x = 1; dir = 1; }
  else if (x == size+1) { x = size; dir = 3; }
  else if (y == 0) { y = 1; dir = 0; }
  else if (y == size+1) { y = size; dir = 2; }
}

int main()
{
  int test;
  scanf("%d", &test);
  while (test--) {
    int size, num;
    scanf("%d%d", &size, &num);
    int board[size+1][size+1];
    memset(board, 0, sizeof(board));
    for (int i = 0; i < num; ++i) {
      scanf("%d%d", &x, &y);
      board[x][y] = 1;
    }
    scanf("%d%d", &x, &y);
    deal(size);

    bool visited[size+1][size+1][4];
    memset(visited, false, sizeof(visited));
    int doit = 0;
//printf("%d %d %d\n", x, y, dir);
    while (doit == 0) {
      visited[x][y][dir] = true;
      if (board[x][y]) {
        dir = (dir+1) % 4;
      } 
      x += dirs[dir][0];
      y += dirs[dir][1];
//printf("%d %d %d\n", x, y, dir);
      if (x == 0 || x == size+1 || y == 0 || y == size+1) {
        doit = 1;
        break;
      }
      if (visited[x][y][dir]) {
        doit = 2;
        break;
      }
    }
    if (doit == 1) printf("%d %d\n", x, y);
    else printf("0 0\n");
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值