Description
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:
- clean the current cell which a cleaner robot is in;
- 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;
- 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 Input
2 3
U..
.*.
4
4 4
R...
.**.
.**.
....
12
3 4
***D
..*.
*...
6
Hint
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 /************************************************************************* 2 > File Name: code/hust/20151025/JJ.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年10月25日 星期日 14时52分17秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 22 #define yn hez111qqz 23 #define j1 cute111qqz 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 using namespace std; 26 const int dx4[4]={0,1,0,-1}; 27 const int dy4[4]={1,0,-1,0}; 28 typedef long long LL; 29 typedef double DB; 30 const int inf = 0x3f3f3f3f; 31 int w,h; 32 bool vis[15][15][5]; 33 char maze[15][15]; 34 int sx,sy; 35 int dir; 36 bool ok( int x,int y) 37 { 38 if (x>=0&&y>=0&&x<w&&y<h&&maze[x][y]!='*') 39 return true; 40 return false; 41 } 42 int main() 43 { 44 #ifndef ONLINE_JUDGE 45 freopen("in.txt","r",stdin); 46 #endif 47 48 cin>>w>>h; 49 for ( int i = 0 ; i < w ; i++) scanf("%s",maze[i]); 50 51 for ( int i = 0 ; i < w ; i++) 52 for ( int j = 0 ; j < h ; j++) 53 { 54 if (maze[i][j]=='R') 55 { 56 dir = 0; 57 sx = i; 58 sy = j; 59 } 60 if (maze[i][j] =='D') 61 { 62 dir = 1; 63 sx = i ; 64 sy = j; 65 } 66 if (maze[i][j]=='L') 67 { 68 dir = 2; 69 sx = i; 70 sy = j; 71 } 72 if (maze[i][j]=='U') 73 { 74 dir = 3; 75 sx = i; 76 sy = j; 77 } 78 } 79 80 int step = 0; 81 82 ms(vis,false); 83 vis[sx][sy][dir] = true; 84 while (step<=w*h*10) 85 { 86 step++; 87 bool tmp = ok(sx+dx4[dir],sy+dy4[dir]); 88 if (tmp) 89 { 90 sx = sx + dx4[dir]; 91 sy = sy + dy4[dir]; 92 // cout<<"sx:"<<sx<<" sy:"<<sy<<endl; 93 if (vis[sx][sy][dir]) break; 94 vis[sx][sy][dir] = true; 95 } 96 else 97 { 98 dir = dir+1; 99 dir = dir % 4; 100 if (vis[sx][sy][dir]) break; 101 vis[sx][sy][dir] = true; 102 } 103 104 } 105 106 int ans = 0; 107 for ( int i = 0 ; i < w ; i++) 108 for ( int j = 0 ; j < h ; j++) 109 for ( int d = 0 ; d < 4 ; d++) 110 if (vis[i][j][d]) 111 { 112 ans++; 113 // cout<<"i:"<<i<<" j:"<<j<<endl; 114 break; 115 } 116 cout<<ans<<endl; 117 118 119 #ifndef ONLINE_JUDGE 120 fclose(stdin); 121 #endif 122 return 0; 123 }