sicily 1187. Laserbox
2015-11-25 14:19
429 查看
1187. Laserbox
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
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 and1 ≤ 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 caughtand 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
a0d6
; }
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");
}
}
相关文章推荐
- 破解C++ Redistributable Package版本之谜
- Android四大基本组件介绍与生命周期
- css定位
- dismissViewController 层次问题
- 数据库版本管理工具flyway的使用心得
- linux ssh ip 超时,ssh 内网ip 和 外网ip的情况
- [Rman]Rman Level012备份实验
- jcifs登录域账户 下载文件 等
- c++ ftream
- (原)Understand中查看函数调用关系
- apache开启虚拟主机功能与httpd-vhosts.conf 配置
- JavaScript编写简单的计算器
- Git配置及GitHub的基础使用(Win及Ubuntu)
- 中国货币政策“新常态”
- Android之手机出现"已安装了存在签名冲突的同名数据包"的原因及解决办法
- FileInputStream 读取txt文件内容
- ecshop 权限系统设计原理分析。
- Javascript 实现复制(Copy)动作方法大全
- 宽字符转 Byte WideCharToMultiByte
- 用 WEKA 进行数据挖掘,第 3 部分: 最近邻和服务器端库