# CodeForces - 589J —（DFS）

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.

1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<string>
5 #include<cmath>
6 #include<algorithm>
7 #include<stack>
8 #include<queue>
9 #define eps 1e-7
10 #define ll long long
11 #define inf 0x3f3f3f3f
12 #define pi 3.141592653589793238462643383279
13 using namespace std;
14 char map[15][15];
15 int n,m,visit[15][15],ans,flag;
16 void change(char &dire) //改变方向
17 {
18     if(dire == 'U')
19         dire = 'R';
20     else if(dire == 'R')
21         dire = 'D';
22     else if(dire == 'D')
23         dire = 'L';
24     else if(dire == 'L')
25         dire = 'U';
26 }
27
28 void go(char dire,int x,int y,int &xx,int &yy) //前进一格
29 {
30     if(dire == 'U')
31     {
32         xx = x - 1;
33         yy = y;
34     }
35     else if(dire == 'L')
36     {
37         xx = x;
38         yy = y - 1;
39     }
40     else if(dire == 'D')
41     {
42         xx = x+1;
43         yy = y;
44     }
45     else
46     {
47         xx = x;
48         yy = y + 1;
49     }
50 }
51
52 void DFS(int x,int y,char dire)
53 {
54     if(flag) return;
55     int xx,yy;
56     go(dire,x,y,xx,yy); //前进一格
57     while(map[xx][yy] == '*' || xx>=n || xx<0 || yy>=m || yy<0) //如果那一个不和要求
58     {
59         change(dire); //就改变方向，修改的是未前进前的格子处的方向
60         visit[x][y]++; //标记+1
61         go(dire,x,y,xx,yy); //再次前进
62         if(visit[x][y] >= 10) //若到达指定次数，就结束递归
63         {
64             flag = 1;
65             return;
66         }
67     }
68     if(map[xx][yy] != '*' && xx<n && x>=0 && yy<m && yy>=0) //到达的格子满足要求
69     {
70         if(!visit[xx][yy]) ans++; //未走过走过格子，ans++
71         visit[xx][yy]++;
72         if(visit[xx][yy] >= 10) //到达指定次数
73         {
74             flag = 1;
75             return;
76         }
77         DFS(xx,yy,dire); //递归
78     }
79 }
80
81 int main()
82 {
83     while(cin>>n>>m)
84     {
85         char dire;
86         int x,y;
87         ans = 1;
88         flag = 0;
89         memset(visit,0,sizeof(visit));
90         for(int i=0; i<n; ++i)
91             for(int j=0; j<m; ++j)
92             {
93                 cin>>map[i][j];
94                 if(map[i][j] >='A' && map[i][j] <= 'Z')
95                 {
96                     dire = map[i][j];
97                     visit[i][j] = 1;
98                     x = i;
99                     y = j;
100                 }
101             }
102         DFS(x,y,dire);
103         cout<<ans<<endl;
104     }
105     return 0;
106 }

posted @ 2018-08-03 11:02  特务依昂  阅读(174)  评论(0编辑  收藏  举报