sicily 1187. Laserbox

来源:互联网 发布:cs1.5参数优化 编辑:程序博客网 时间:2024/05/22 15:14

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

22 31 11 22 23 13 61 11 32 22 33 13 22 0

Sample Output

2 00 2

题目分析

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

#include <stdio.h>#include <memory.h>int dirs[4][2] = {0,1, 1,0, 0,-1, -1,0}; // N E S Wint 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