CodeForce 589J Cleaner Robot

Cleaner Robot

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:

  1. clean the current cell which a cleaner robot is in;
  2. 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;
  3. 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.

Examples

Input

2 3U...*.

Output

4

Input

4 4R....**..**.....

Output

12

Input

3 4***D..*.*...

Output

6

Note

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.

题意:

  给你一个地图, ‘ . ’空地板,‘*’表示家具,字母表示机器人的位置,‘R‘‘L‘‘D’‘U’表示方向,

  当机器人不能前进时,就顺时针旋转九十度,继续前进,注意相同的路,可以被重复访问

思路:

  模拟题,dfs模拟每个状态,当经过某地的次数很大时退出

AC代码:

  1 # include <iostream>
  2 # include <cstring>
  3 # include <cstdio>
  4 using namespace std;
  5
  6 const int MAX = 12;
  7
  8 char map[MAX][MAX];
  9 int vis[MAX][MAX];
 10 int n, m;
 11
 12 bool ok(int x, int y)
 13 {
 14     if(x >= 1 && x <= n && y >= 1 && y <= m)
 15         return true;
 16     return false;
 17 }
 18
 19 void dfs(int x, int y, char ch)
 20 {
 21     if(vis[x][y] > 10)
 22         return;
 23     vis[x][y]++;
 24
 25     if(ch == ‘U‘)
 26     {
 27         int newx = x - 1;
 28         int newy = y;
 29         if(!ok(newx, newy) || map[newx][newy] == ‘*‘)
 30             dfs(x, y, ‘R‘);
 31         else
 32             dfs(newx, newy, ‘U‘);
 33     }
 34     else if(ch == ‘R‘)
 35     {
 36         int newx = x;
 37         int newy = y + 1;
 38         if(!ok(newx, newy) || map[newx][newy] == ‘*‘)
 39             dfs(x, y, ‘D‘);
 40         else
 41             dfs(newx, newy, ‘R‘);
 42     }
 43     else if(ch == ‘D‘)
 44     {
 45         int newx = x + 1;
 46         int newy = y;
 47         if(!ok(newx, newy) || map[newx][newy] == ‘*‘)
 48             dfs(x, y, ‘L‘);
 49         else
 50             dfs(newx, newy, ‘D‘);
 51     }
 52     else if(ch == ‘L‘)
 53     {
 54         int newx = x;
 55         int newy = y - 1;
 56         if(!ok(newx, newy) || map[newx][newy] == ‘*‘)
 57             dfs(x, y, ‘U‘);
 58         else
 59             dfs(newx, newy, ‘L‘);
 60     }
 61 }
 62
 63
 64
 65 int main()
 66 {
 67
 68     while(cin >> n >> m)
 69     {
 70         int h, l;
 71         char ch;
 72
 73         memset(vis, 0, sizeof(vis));
 74         for(int i = 0; i <= MAX; i++)
 75             for(int j = 0; j <= MAX; j++)
 76                 map[i][j] = ‘*‘;
 77
 78         for(int i = 1; i <= n; i++)
 79             for(int j = 1; j <= m; j++)
 80             {
 81                 cin >> map[i][j];
 82                 if(map[i][j] == ‘U‘ || map[i][j] == ‘R‘ || map[i][j] == ‘D‘ || map[i][j] == ‘L‘)
 83                 {
 84                     h = i;
 85                     l = j;
 86                     ch = map[i][j];
 87                 }
 88             }
 89
 90         dfs(h, l, ch);
 91
 92         int jishu = 0;
 93         for(int i = 1; i <= n; i++)
 94             for(int j = 1; j <= m; j++)
 95             {
 96                 if(vis[i][j])
 97                     jishu++;
 98             }
 99         cout << jishu << endl;
100
101     }
102     return 0;
103 }

时间: 2024-07-30 20:21:09

CodeForce 589J Cleaner Robot的相关文章

Codeforces 589J Cleaner Robot(DFS)

Cleaner Robot Time Limit: 2000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Submit Status Description Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schematically Masha's room is a rectang

CodeForces 589J Cleaner Robot

有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面积是多少 一开始认为是走到之前清扫过的就停止搜索 后来知道是走到一个四个方向都走过的点才停止搜索 #include<stdio.h> #include<string.h> const int MAXN = 17; ///'U', 'R', 'D' 'L' int dir[4][2] =

CodeForces 589J Cleaner Robot (DFS,或BFS)

题意:给定n*m的矩阵,一个机器人从一个位置,开始走,如果碰到*或者边界,就顺时针旋转,接着走,问你最后机器人最多能走过多少格子. 析:这个题主要是题意读的不大好,WA了好几次,首先是在*或者边界才能转向,其次就是走过的地方也能走,注意这两点,就可以AC了,可以用DFS,也可以用BFS, 我用的DFS. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #inclu

Cleaner Robot - CodeForces 589J(搜索)

有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面积是多少. 分析:直接搜就行了....... 代码如下: ---------------------------------------------------------------------------------------------------------------- #include

[LeetCode] 489. Robot Room Cleaner 扫地机器人

Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. When it tries to move into a blocked cel

leetcode489 - Robot Room Cleaner - hard

Given a robot cleaner in a room modeled as a grid.Each cell in the grid can be empty or blocked.The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees.When it tries to move into a blocked cell,

489. Robot Room Cleaner - Hard

Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. When it tries to move into a blocked cel

Leetcode: Robot Room Cleaner

Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. When it tries to move into a blocked cel

CodeForce Educational round Div2 C - Vasya and Robot

C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell (0,0)(0,0). Robot can perfor