CodeForces 589J ——2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest

J - Cleaner Robot
Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u

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

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.

    

题意:有一个机器人,在一个矩形房间里打扫卫生,*表示家具,  . 表示空地,机器人起始有四个方向,URDL,分别为上下左右,机器人碰到墙壁或者家具就顺时针旋转,如果是空地,就直走,问机器人最多可以打扫到多少块地

深度广搜都行了(liao)

神搜代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
char s[20][20];
int n, m, cnt;
int dx[5] = {0, -1, 0, 1, 0};
int dy[5] = {0, 0, 1, 0, -1};
bool use[20][20];
int dirr[20][20][5];
void dfs(int x, int y, int dir)
{
    if(use[x][y]==false)
    {
        cnt++;///计数,有几块是可以打扫的
        use[x][y]=true;
    }
    dirr[x][y][dir]=1;///这个 (x,y) 这块地 ,dir 这个方向z走过了
    int nx,ny,dir1;
    for(int i=0; i<4; i++)
    {
        dir1=(dir+i)%4;///方向 1 - 4
        if(dir1 ==0)dir1=4;
        nx=x+dx[dir1],ny=y+dy[dir1];
        if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]=='.')
        {
            if(dirr[nx][ny][dir1]) break;///可以走但是这个方向走过了,break!!!
            dfs(nx,ny,dir1);
            break;///及时break,机器人只能选择一个可行方向走
        }
    }
}
int main()
{
    int x, y;
    while(cin>>n>>m)
    {
        int dir;
        cnt = 0;
        for(int i = 0; i < n; i++)
        {
            cin>>s[i];
            for(int j=0; j< m; j++)
            {
                if(s[i][j] == 'U') x=i,y=j, dir=1, s[i][j]= '.';
                else if(s[i][j] == 'R') x=i, y=j, dir=2, s[i][j]='.';
                else if(s[i][j] == 'D') x=i, y=j, dir=3, s[i][j]='.';
                else if(s[i][j] == 'L') x=i, y=j, dir=4, s[i][j]='.';
            }
        }
        memset(use, false, sizeof use);
        memset(dirr,0,sizeof(dirr));
        dfs(x, y, dir);
        cout<<cnt<<endl;
    }
    return 0;
}

广搜代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
char s[20][20];
int n, m;
int dx[5] = {0, -1, 0, 1, 0};
int dy[5] = {0, 0, 1, 0, -1};
bool use[20][20];
int dirr[20][20][5];
struct node
{
    int x,y,dir;
    node(){;}
    node (int a,int b,int d)
    {
        x=a,y=b,dir=d;
    }
};
int dfs(int x, int y, int dir)
{
    int ans=0;
    memset(use, false, sizeof use);
    memset(dirr,0,sizeof(dirr));
    queue<node>que;
    que.push(node(x,y,dir));
    int nx,ny,dir1;
    while(!que.empty())
    {

        node temp=que.front();
        que.pop();
        dirr[temp.x][temp.y][temp.dir]=1;
        if(!use[temp.x][temp.y])
        {
            ans++;
           use[temp.x][temp.y]=1;
        }
        for(int i=0;i<4;i++)
        {
            dir1=temp.dir+i;
            dir1%=4;
            if(dir1==0)
                dir1=4;
            nx=temp.x+dx[dir1];
            ny=temp.y+dy[dir1];
            if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]=='.')
            {
                if(dirr[nx][ny][dir1])
                    break;
                que.push(node(nx,ny,dir1));
                break;
            }
        }

    }
    return ans;
}
int main()
{
    int x, y;
    while(cin>>n>>m)
    {
        int dir;
        cnt = 0;
        for(int i = 0; i < n; i++)
        {
            cin>>s[i];
            for(int j=0; j< m; j++)
            {
                if(s[i][j] == 'U') x=i,y=j, dir=1, s[i][j]= '.';
                else if(s[i][j] == 'R') x=i, y=j, dir=2, s[i][j]='.';
                else if(s[i][j] == 'D') x=i, y=j, dir=3, s[i][j]='.';
                else if(s[i][j] == 'L') x=i, y=j, dir=4, s[i][j]='.';
            }
        }
        
        cout<<dfs(x, y, dir)<<endl;
    }
    return 0;
}


































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值