2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J Cleaner Robot

Cleaner RobotCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

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:

  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.

Sample Input

 
Input
2 3
U..
.*.
Output
4
Input
4 4
R...
.**.
.**.
....
Output
12
Input
3 4
***D
..*.
*...
Output
6

Sample Output

 

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.

                                                                                                             

/*
一个机器人遇到障碍物就会顺时针旋转90度,问你机器人活动区域最大是多少
用v[i][j][d]记录用怎么样的状态进入这个点,如果下一次又出现,那么一定出现循环就可以停止了
*/

#include<bits/stdc++.h> 
#include<string.h>
#include<stdio.h>
#define N 12
using namespace std; 
int n,m,cur=1;
int dir[5][2]={-1,0,1,0,0,-1,0,1};//上下左右
char mapn[N][N];
int visit[N][N]={0};//记录这个点来没来过
int visitn[N][N][N]={0};//记录这个点来过多少次
int v(char ch)
{
    if(ch=='U') return 0;
    if(ch=='D') return 1;
    if(ch=='L') return 2;
    if(ch=='R') return 3;
}
bool check(int x1,int y1)
{
    if(x1<0||x1>=n||y1<0||y1>=m||mapn[x1][y1]=='*')
        return true;
    return false;
}
void dfs(int x,int y,int d)
{
    if(visitn[x][y][d])
        return ;
    visitn[x][y][d]=1;
    if(!visit[x][y])
        cur++,visit[x][y]=1;
    if(d==0)//
    {
        int fx=x+dir[0][0];
        int fy=y+dir[0][1];
        if(check(fx,fy))//走不通
            //cout<<"转向 "<<x<<" "<<y<<endl;
            dfs(x,y,3);
        else//能走通
            dfs(fx,fy,0);
    }
    else if(d==1)//
    {
        int fx=x+dir[1][0];
        int fy=y+dir[1][1];
        if(check(fx,fy))//走不通
            //cout<<"转向 "<<x<<" "<<y<<endl;
            dfs(x,y,2);
        else//能走通
            dfs(fx,fy,1);
    }
    else if(d==2)//
    {
        int fx=x+dir[2][0];
        int fy=y+dir[2][1];
        //cout<<x<<" "<<y<<endl;
        //cout<<fx<<" "<<fy<<endl;
        //return ;
        if(check(fx,fy))//走不通
            //cout<<"转向 "<<x<<" "<<y<<endl;
            dfs(x,y,0);
        else//能走通
            dfs(fx,fy,2);
    }
    else if(d==3)//
    {
        int fx=x+dir[3][0];
        int fy=y+dir[3][1];
        if(check(fx,fy))//走不通
            //cout<<"转向 "<<x<<" "<<y<<endl;
            dfs(x,y,1);
        else//能走通
            dfs(fx,fy,3);
    }
}
int main()
{ 
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    //cout<<n<<" "<<m<<endl;
        getchar();
        int x,y;
        cur=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&mapn[i][j]);
                if(mapn[i][j]=='U'||mapn[i][j]=='D'||mapn[i][j]=='L'||mapn[i][j]=='R')
                    x=i,y=j;
            }
            scanf("\n");
        }
        //for(int i=0;i<n;i++)
        //    cout<<mapn[i]<<endl;
        visit[x][y]=1;
        dfs(x,y,v(mapn[x][y]));
        //for(int i=0;i<n;i++)
        //{
        //    for(int j=0;j<m;j++)
        //        cout<<visit[i][j]<<" ";
        //    cout<<endl;
        //}
        printf("%d\n",cur);
    return 0; 
} 

 

 

posted @ 2016-08-20 20:01  勿忘初心0924  阅读(225)  评论(0编辑  收藏  举报