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. C++ 0x 使用 shared_ptr 自动释放, 防止内存泄漏

    最近在研究 cocos2d-x 3.0 ,它在创建类的对象时比如 Layer 时, 并不是直接使用 new , 而是使用一个宏方法  CREATE_FUNC(MyLayer);. 这个宏就是自动的创建 ...

  2. JSP复习

    3.2.2 JSP指令元素: JSP指令 (1) page指令:定义整个页面的全局属性 (2)include指令:用于包含一个文件或代码的文件 (3)taglib指令:用来引用自定义的标签或第三方标签 ...

  3. 协同过滤 spark scala

    1 http://www.cnblogs.com/charlesblc/p/6165201.html [转载]协同过滤 & Spark机器学习实战 2 基于Spark构建推荐引擎之一:基于物品 ...

  4. hadoop无法启动常见原因

    1.Could not chdir to home directory /home/USER: Permission denied 启动datanode时会报这个错误,尝试利用ssh登录datanod ...

  5. SmartOS技术常见问题

    什么是默认用户名和密码? 当SmartOS Live Image第一次启动时,系统将提示您设置root密码. 如果在不导入Zpool的情况下启动SmartOS,则需要默认的root密码. 当使用noi ...

  6. \extras\intel\Hardware_Accelerated_Execution_Manager HAXM 未安装导致AndroidStudio新建了模拟器开启不了

    之前安装过 bios模式也是正常的.所以按照下面的步骤操作. https://software.intel.com/en-us/android  在这个界面 选择右侧的, 下载后解压到下面图上的路径: ...

  7. Linux 软件 安装到 /usr,/usr/local/ 还是 /opt 目录

    Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理解为C:/Windows/System32./u ...

  8. 了解大数据的特点、来源与数据呈现方式以及用Python写Mad Libs游戏

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2620. 1.浏览2019春节各种大数据分析报告,例如: 这世间,再 ...

  9. 把System.Drawing.Image对象img,在页面的Image控件上显示出来

    1.保存到本地,然后调用. //定义文件名    string iname = DateTime.Now.ToString("yyMMddhhmmss"); //保存到服务器,b是 ...

  10. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...