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 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.

题意:给出了一个n*m的字符矩阵,矩阵由‘ * ’和‘ . ’组成,‘ . ’表示空白位置,‘ * ’表示被占用的位置,在矩阵中有一个机器人,用字母‘U’,‘R’,‘D’,或’L‘表示,字母代表了机器人的初始方向,它将往那个方向移动一格,若那个方向走不通(那格是’*‘或边界外),机器人将顺时针旋转90度改变方向。问机器人最终总共走了多少个不重复的格子。

思路:用DFS模拟就好了,而DFS的结束条件就是到达某个格子的次数不超过某一个数,我在代码中设置了10次,其实5次就够了(4次wa了)。

代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define eps 1e-7
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
char map[][];
int n,m,visit[][],ans,flag;
void change(char &dire) //改变方向
{
if(dire == 'U')
dire = 'R';
else if(dire == 'R')
dire = 'D';
else if(dire == 'D')
dire = 'L';
else if(dire == 'L')
dire = 'U';
} void go(char dire,int x,int y,int &xx,int &yy) //前进一格
{
if(dire == 'U')
{
xx = x - ;
yy = y;
}
else if(dire == 'L')
{
xx = x;
yy = y - ;
}
else if(dire == 'D')
{
xx = x+;
yy = y;
}
else
{
xx = x;
yy = y + ;
}
} void DFS(int x,int y,char dire)
{
if(flag) return;
int xx,yy;
go(dire,x,y,xx,yy); //前进一格
while(map[xx][yy] == '*' || xx>=n || xx< || yy>=m || yy<) //如果那一个不和要求
{
change(dire); //就改变方向,修改的是未前进前的格子处的方向
visit[x][y]++; //标记+1
go(dire,x,y,xx,yy); //再次前进
if(visit[x][y] >= ) //若到达指定次数,就结束递归
{
flag = ;
return;
}
}
if(map[xx][yy] != '*' && xx<n && x>= && yy<m && yy>=) //到达的格子满足要求
{
if(!visit[xx][yy]) ans++; //未走过走过格子,ans++
visit[xx][yy]++;
if(visit[xx][yy] >= ) //到达指定次数
{
flag = ;
return;
}
DFS(xx,yy,dire); //递归
}
} int main()
{
while(cin>>n>>m)
{
char dire;
int x,y;
ans = ;
flag = ;
memset(visit,,sizeof(visit));
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
{
cin>>map[i][j];
if(map[i][j] >='A' && map[i][j] <= 'Z')
{
dire = map[i][j];
visit[i][j] = ;
x = i;
y = j;
}
}
DFS(x,y,dire);
cout<<ans<<endl;
}
return ;
}

CodeForces - 589J —(DFS)的更多相关文章

  1. CodeForces - 589J(DFS)

    题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...

  2. codeforces 731C(DFS)

    题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...

  3. CodeForces - 95B(DFS)

    题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...

  4. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  5. Cleaner Robot - CodeForces 589J(搜索)

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

  6. Codeforces 761E(DFS)

    E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Codeforces 115A- Party(DFS)

    A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...

  8. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  9. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

随机推荐

  1. windows下多个文件合并成一个文件

    如果你拿到的是一堆文件,那么你想把它合并成一个文件来使用,那么按下面的步骤,轻轻松松就可以搞定. 第一步:把所有要合并的文件放到同一个文件下面 第二步:在CMD里面进入到你的文件目录 第三步:输入如下 ...

  2. jquery 不选择第一个

    参考 https://zhidao.baidu.com/question/174343639.html th:not(':first')

  3. java内存模型:简单理解

    1.Java内存模型(Java Memory Model,JMM) 2.JMM定义了线程和主内存之间的抽象关系: 线程之间的共享变量存储在主内存(main memory)中,每个线程都有一个私有的本地 ...

  4. Python sum() 函数

    Python sum() 函数  Python 内置函数 描述 sum() 方法对系列进行求和计算. 语法 以下是 sum() 方法的语法: sum(iterable[, start]) 参数 ite ...

  5. Jiu Yuan Wants to Eat(树链剖分+线段树延迟标记)

    Jiu Yuan Wants to Eat https://nanti.jisuanke.com/t/31714 You ye Jiu yuan is the daughter of the Grea ...

  6. Princess Principal(思维题)

    Princess Principal https://www.nowcoder.com/acm/contest/201/J 题目描述 阿尔比恩王国(the Albion Kingdom)潜伏着一群代号 ...

  7. 安装tftp服务器进行文件传输

    1. 安装: sudo apt-get install tftp-hpa tftpd-hpa ps: tftpd是服务器,tftp是客户端,客户端能发送和获取,服务器不能动. 2. 配置文件: sud ...

  8. VMware克隆CentOS网络配置

    配置网络 如果是克隆CentOS的: vi /etc/udev/rules.d/70-persistent-net.rules 注释掉网络eth0,把最后一个改为eth0,记录下mac地址. vi / ...

  9. 2-Qt关闭子窗口时执行特定代码

    https://blog.csdn.net/naibozhuan3744/article/details/82689434 本文主要总结在关闭qt的QWidget子窗口瞬间,执行特定代码.由于主窗口关 ...

  10. php 利用http上传协议(表单提交上传图片 )

    主要就是利用php 的 fsocketopen 消息传输. 这里先通过upload.html 文件提交,利用chrome抓包,可以看到几个关键的信息. 首先指定了表单类型为multipart/form ...