codeforces589J 简单dfs,队列

时间:2022-03-10 09:41:06

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

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.

codeforces589J    简单dfs,队列

1、codeforces589J

2、链接:http://codeforces.com/problemset/problem/589/J

3、总结:清洁机器人,一开始用bfs,但第二种样例不知道怎么跳出

参考了别人的 http://codeforces.com/contest/589/submission/19431126

用的队列,更简便一点

#include<bits/stdc++.h>
using namespace std; int n,m;
char mapn[][];
char dir[]="URDL";
int visit[][];
int dir_r[]={-,,,}; //方向要与"URDL"吻合,mapn[0][0]为左上角
int dir_c[]={,,,-};
queue<int>q; bool change(int i,int j)
{
if((i>=)&&(i<n)&&(j>=)&&(j<m))
return true;
else
return false;
} int getDI(char c)
{
for(int i=;i<;i++){
if(c==dir[i])return i;
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(visit,,sizeof(visit));
q=queue<int>(); bool fin=true;
for(int i=;i<n;i++){
scanf("%s",mapn[i]);
if(fin){
for(int j=;j<m;j++){
if((mapn[i][j]!='*')&&(mapn[i][j]!='.')){
int di=getDI(mapn[i][j]);
q.push(i);q.push(j);q.push(di);
fin=false;
visit[i][j]=;
}
}
}
} int r,c,di1;
int rr,cc,di2,cal=;
while((cal++<)&&(!q.empty())) //用(cal++<100000)控制跳出。。感觉这里不太好
{
r=q.front();q.pop();
c=q.front();q.pop();
di1=q.front();q.pop();
for(int i=;i<;i++)
{
di2=(di1+i)%; //方向控制
rr=r+(dir_r[di2]);
cc=c+(dir_c[di2]);
if(!change(rr,cc))continue;
if(mapn[rr][cc]=='*')continue;
q.push(rr);q.push(cc);q.push(di2);
visit[rr][cc]=;
break;
}
/*
一开始把上面两行放在这,忽略了一些情况,一直RE
q.push(rr);q.push(cc);q.push(di2);
visit[rr][cc]=1;
*/ } int num=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(visit[i][j]){
num++;
}
}
printf("%d\n",num);
}
return ;
}