CodeForces 589 J Cleaner Robot
2015-12-18 12:56
465 查看
Description
Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance.
Schematically Masha's room is a rectangle, consisting of w × h square cells of size 1 × 1. Each cell of the room is either empty
(represented by character '.'), or occupied by furniture (represented by character '*').
A cleaner robot fully occupies one free cell. Also the robot has a current direction (one of four options), we will say that it looks in this direction.
The algorithm for the robot to move and clean the floor in the room is as follows:
clean the current cell which a cleaner robot is in;
if the side-adjacent cell in the direction where the robot is looking exists and is empty, move to it and go to step 1;
otherwise turn 90 degrees clockwise (to the right relative to its current direction) and move to step 2.
The cleaner robot will follow this algorithm until Masha switches it off.
You know the position of furniture in Masha's room, the initial position and the direction of the cleaner robot. Can you calculate the total area of the room that the robot will clean if it works infinitely?
Input
The first line of the input contains two integers, w and h(1 ≤ w, h ≤ 10) —
the sizes of Masha's room.
Next w lines contain h characters each — the description of the room. If a cell of a room is empty, then the corresponding character
equals '.'. If a cell of a room is occupied by furniture, then the corresponding character equals '*'. If a cell has the robot, then it is empty, and the corresponding character
in the input equals 'U', 'R', 'D' or 'L', where the letter represents the direction of the cleaner robot.
Letter 'U' shows that the robot is looking up according to the scheme of the room, letter 'R' means it is looking to the right, letter 'D'
means it is looking down and letter 'L' means it is looking to the left.
It is guaranteed that in the given w lines letter 'U', 'R', 'D' or 'L'
occurs exactly once. The cell where the robot initially stands is empty (doesn't have any furniture).
Output
In the first line of the output print a single integer — the total area of the room that the robot will clean if it works infinitely.
Sample Input
Input
Output
Input
Output
Input
Output
Hint
In the first sample the robot first tries to move upwards, it can't do it, so it turns right. Then it makes two steps to the right, meets a wall and turns downwards. It moves down, unfortunately tries moving left and locks itself moving from cell (1, 3) to
cell (2, 3) and back. The cells visited by the robot are marked gray on the picture.
Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance.
Schematically Masha's room is a rectangle, consisting of w × h square cells of size 1 × 1. Each cell of the room is either empty
(represented by character '.'), or occupied by furniture (represented by character '*').
A cleaner robot fully occupies one free cell. Also the robot has a current direction (one of four options), we will say that it looks in this direction.
The algorithm for the robot to move and clean the floor in the room is as follows:
clean the current cell which a cleaner robot is in;
if the side-adjacent cell in the direction where the robot is looking exists and is empty, move to it and go to step 1;
otherwise turn 90 degrees clockwise (to the right relative to its current direction) and move to step 2.
The cleaner robot will follow this algorithm until Masha switches it off.
You know the position of furniture in Masha's room, the initial position and the direction of the cleaner robot. Can you calculate the total area of the room that the robot will clean if it works infinitely?
Input
The first line of the input contains two integers, w and h(1 ≤ w, h ≤ 10) —
the sizes of Masha's room.
Next w lines contain h characters each — the description of the room. If a cell of a room is empty, then the corresponding character
equals '.'. If a cell of a room is occupied by furniture, then the corresponding character equals '*'. If a cell has the robot, then it is empty, and the corresponding character
in the input equals 'U', 'R', 'D' or 'L', where the letter represents the direction of the cleaner robot.
Letter 'U' shows that the robot is looking up according to the scheme of the room, letter 'R' means it is looking to the right, letter 'D'
means it is looking down and letter 'L' means it is looking to the left.
It is guaranteed that in the given w lines letter 'U', 'R', 'D' or 'L'
occurs exactly once. The cell where the robot initially stands is empty (doesn't have any furniture).
Output
In the first line of the output print a single integer — the total area of the room that the robot will clean if it works infinitely.
Sample Input
Input
2 3 U.. .*.
Output
4
Input
4 4R...
.**.
.**.
....
Output
12
Input
3 4***D
..*.
*...
Output
6
Hint
In the first sample the robot first tries to move upwards, it can't do it, so it turns right. Then it makes two steps to the right, meets a wall and turns downwards. It moves down, unfortunately tries moving left and locks itself moving from cell (1, 3) to
cell (2, 3) and back. The cells visited by the robot are marked gray on the picture.
#include <stdio.h> int n, m, ans; char map[15][15]; bool vis[15][15]; int sx, sy; int dirr[15][15]; int dirx[] = {0, -1, 0, 1, 0}, diry[] = {0, 0, 1, 0, -1}; void dfs(int x, int y, int dir){ if (!vis[x][y]){ ans++; dirr[x][y] = dir; } vis[x][y] = 1; int dir1; for (int i = 0; i < 4; i++){ dir1 = (dir + i) % 4; if (dir1 == 0) dir1 = 4; int dx = x + dirx[dir1], dy = y + diry[dir1]; if (dx >= 0 && dx < n && dy >= 0 && dy < m && map[dx][dy] == '.'){ if (dirr[dx][dy] == dir1) break; dfs(dx, dy, dir1); break; } } } int main(){ while (scanf("%d%d", &n, &m) != EOF){ int dir; ans = 0; for (int i = 0; i < n; i++){ scanf("%s", map[i]); for (int j = 0; j < m; j++){ if (map[i][j] == 'U'){ sx = i, sy = j; dir = 1; map[i][j] = '.'; } else if (map[i][j] == 'R'){ sx = i, sy = j; dir = 2; map[i][j] = '.'; } else if (map[i][j] == 'D'){ sx = i, sy = j; dir = 3; map[i][j] = '.'; } else if (map[i][j] == 'L'){ sx = i, sy = j; dir = 4; map[i][j] = '.'; } } } dfs(sx, sy, dir); printf("%d\n", ans); } return 0; }
相关文章推荐
- 103,使用typedef为block数据类型起别名
- ios 除了po命令之外的调试命令
- vim自动补全:go
- 第10周知原理
- 经典源码,近乎的安装说明。
- 暗时间
- Python 手记 - 8
- html5 meta标签属性整理
- Centos 装系统 配置网卡,校准时间
- VisualStudio之Run-Time Library选项
- myEclipse项目转成Eclipse开发
- HDOJ 1175 连连看
- Android创建旋转箭头-不恢复原位-旋转动画的使用
- 常用的正则表达式
- CentOS 7安装配置Samba服务器
- php http 缓存(客户端缓存)
- android studio module 如何生成jar包并进行混淆
- CentOS 7 安装Mono 和 MonoDevelop
- HADOOP的那些坑
- OCR应用:OCR识图取字