CodeForces 589 J Cleaner Robot

机器人清扫路径算法
本文介绍了一个基于DFS的机器人清扫路径算法。机器人在一个n*m的表格中进行清扫工作,需要避开障碍物并尽可能多地覆盖空地。文章详细解释了算法的工作原理,并提供了完整的AC代码。

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.

Examples

Input

Copy

2 3
U..
.*.

Output

Copy

4

Input

Copy

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

Output

Copy

12

Input

Copy

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

Output

Copy

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.

题目大意:给你一个n*m的表格,表格里面有一个大写字母('D','U','R','L')表示机器人的初始化方向,还有若干'  *  '表示家具,其余的是一个点'  。',当机器人遇到障碍时机器人只会顺时针转动,问你机器人可以清扫的区域有多少。

思路:dfs,遍历整幅图,直到一个地方的访问次数为4次的时候结束遍历(一个地方最多到达3次,否则表示机器人是在来回重复)

AC代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<deque>
#include<queue>
#include<list>
const int inf=0x3f3f3f3f;
const int MOD=1e9+7;
#define ll long long
#define ME0(x) memset(x,0,sizeof(x))
#define MEI(x) memset(x,inf,sizeof(x))
#define MEF(x) memset(x,-1,sizeof(x))
using namespace std;
int ans,a,b;
char s[15][15],f;
int vis[15][15];
struct GO
{
    int x,y;
};
char turn(char x)
{
    if(x=='D')
    {
        return 'L';
    }
    else if(x=='L')
    {
        return 'U';
    }
    else if(x=='U')
    {
        return 'R';
    }
    else
    {
        return 'D';
    }
}
GO go(char z)
{
    GO re;
    if(z=='U')
    {
        re.x=-1;
        re.y=0;
    }
    else if(z=='R')
    {
        re.x=0;
        re.y=1;
    }
    else if(z=='D')
    {
        re.x=1;
        re.y=0;
    }
    else
    {
        re.x=0;
        re.y=-1;
    }
    return re;
}
void dfs(int i1,int i2,char i3)
{
    if(vis[i1][i2]==0)
    {
        ans++;
    }
    else if(vis[i1][i2]==4)
    {
        return ;
    }
    vis[i1][i2]++;
    int x1=i1+go(i3).x,y1=i2+go(i3).y;
    if(s[x1][y1]!='*'&&(x1>=1&&x1<=a&&y1>=1&&y1<=b))
    {
        dfs(x1,y1,i3);
    }
    else
    {
        dfs(i1,i2,turn(i3));
    }
}
int main()
{
    cin>>a>>b;
    for(int a1=1;a1<=a;a1++)
    {
        for(int b1=1;b1<=b;b1++)
        {
            cin>>s[a1][b1];
            if(s[a1][b1]>='A'&&s[a1][b1]<='Z')
            {
                int x,y;
                x=a1;
                y=b1;
                f=s[a1][b1];
            }
        }
    }
    ME0(vis);
    ans=0;
    dfs(x,y,f);
    cout<<ans<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值