2013
11-12

# Laserbox

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.

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.

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.

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


2 0
0 2


/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int g[][]=new int[52][52];
int ca,i,j,k,n,r,x,y,dir;
ca=sc.nextInt();
while ((ca--)!=0)
{
n=sc.nextInt();
r=sc.nextInt();
for(i=0;i< g.length;i++)
Arrays.fill(g[i],0);
for (i=0;i< r;i++)
{
x=sc.nextInt();
y=sc.nextInt();
g[x][y]=1;
}
for (i=0;i<=n+1;i++)
g[0][i]=g[n+1][i]=g[i][0]=g[i][n+1]=-1;
x=sc.nextInt();
y=sc.nextInt();
if (y==0) dir=0; else
if (x==0) dir=1; else
if (y==n+1) dir=2; else dir=3;
x+=dx[dir];
y+=dy[dir];
while (g[x][y]!=-1)
{
if (g[x][y]!=0) dir=(dir+1)%4;
x+=dx[dir];
y+=dy[dir];
}
System.out.printf("%d %d\n",x,y);
}
}
}

1. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

2. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法