【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J】【暴力模拟】Cleaner Robot 机器人行走 决策固定

J. Cleaner Robot
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
2 3
U..
.*.
output
4
input
4 4
R...
.**.
.**.
....
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.

【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J】【暴力模拟】Cleaner Robot 机器人行走 决策固定_第1张图片


#include<stdio.h>
#include<string.h>
const int dy[4]={-1,0,1,0};
const int dx[4]={0,1,0,-1};
// U R D L
char a[12][12];
bool e[12][12][4];
bool f[12][12];
int ans;
int n,m;
void dfs(int y,int x,int d)
{
    if(e[y][x][d])return;
    e[y][x][d]=1;
    if(f[y][x]==0)
    {
        f[y][x]=1;
        ++ans;
    }
    int yy=y+dy[d];
    int xx=x+dx[d];
    if(yy>=1&&yy<=n&&xx>=1&&xx<=m&&a[yy][xx]!='*')dfs(yy,xx,d);
    else dfs(y,x,(d+1)%4);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=n;i++)scanf("%s",a[i]+1);
        memset(e,0,sizeof(e));
        memset(f,0,sizeof(f));
        ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='U')dfs(i,j,0);
                else if(a[i][j]=='R')dfs(i,j,1);
                else if(a[i][j]=='D')dfs(i,j,2);
                else if(a[i][j]=='L')dfs(i,j,3);
            }
        }
        printf("%d\n",ans);
    }
}
/*
【题意】
给你一个n(10)*m(10)的地图,格子的障碍物为'*'。
有一个机器人,初始是'U'|'R'|'D'|'L'|中的一个,代表方向。
这个机器人每次都就向前走,遇到障碍物就右转。
在经过无限时间后,能走过的格子数是多少

【类型】
模拟

【分析】
这题虽然给的是迷宫,但不是bfs-迷宫搜索,而是暴力模拟。
因为——我们的决策是固定的!
状态数只有4nm,重复的话就一定是遇到循环节了。
这样就可以AC啦。
但是,对于方向的定义,一定要想好,顺时针,上右下左=0123,不要在这里浪费时间。

【时间复杂度&&优化】
O(4nm)

*/


你可能感兴趣的:(【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J】【暴力模拟】Cleaner Robot 机器人行走 决策固定)