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

// Problem#: 1187
// Submission#: 3201685
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;

#define MAX_N 55

char G[MAX_N][MAX_N];
int n, r;
bool vis[MAX_N][MAX_N][4];
int X, Y, dir;  // 0->east, 1->south, 2->west, 3->north
int endX, endY;
bool death;

void Go() {
    while (1) {
        if (dir == 0) {
            int i;
            for (i = X + 1; i <= n; i++) {
                if (G[Y][i] == 'M') break;
            }
            if (i > n) {
                endX = i;
                endY = Y;
                break;
            }
            if (vis[Y][i][1]) {
                death = true;
                break;
            }
            vis[Y][i][1] = true;
            X = i;
            dir = 1;
        } else if (dir == 1) {
            int i;
            for (i = Y - 1; i > 0; i--) {
                if (G[i][X] == 'M') break;
            }
            if (i == 0) {
                endX = X;
                endY = i;
                break;
            }
            if (vis[i][X][2]) {
                death = true;
                break;
            }
            vis[i][X][2] = true;
            Y = i;
            dir = 2;
        } else if (dir == 2) {
            int i;
            for (i = X - 1; i > 0; i--) {
                if (G[Y][i] == 'M') break;
            }
            if (i == 0) {
                endX = i;
                endY = Y;
                break;
            }
            if (vis[Y][i][3]) {
                death = true;
                break;
            }
            vis[Y][i][3] = true;
            X = i;
            dir = 3;
        } else {
            int i;
            for (i = Y + 1; i <= n; i++) {
                if (G[i][X] == 'M') break;
            }
            if (i > n) {
                endX = X;
                endY = i;
                break;
            }
            if (vis[i][X][0]) {
                death = true;
                break;
            }
            vis[i][X][0] = true;
            Y = i;
            dir = 0;
        }
    }
}

int main() {

    std::ios::sync_with_stdio(false);

    int caseNum;
    cin >> caseNum;

    while (caseNum--) {
        cin >> n >> r;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                G[i][j] = '.';
            }
        }
        memset(vis, false, sizeof(vis));
        for (int i = 0; i < r; i++) {
            int x, y;
            cin >> x >> y;
            G[y][x] = 'M';
        }
        cin >> X >> Y;
        if (X == n + 1) dir = 2;
        if (X == 0) dir = 0;
        if (Y == n + 1) dir = 1;
        if (Y == 0) dir = 3;

        death = false;
        Go();

        if (death) cout << "0 0" << endl;
        else cout << endX << " " << endY << endl;
    }


    //getchar();
    //getchar();
    
    return 0;
}                                 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值