CodeForces 589J -- J. Cleaner Robot (BFS)

23 篇文章 0 订阅

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:

 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.

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.


大体题意:

给你个机器人,要打扫房间,他有一个初始的方向,你只能冲这个方向走,如果不能走了,你可以更改方向,但是只能顺时针改变方向,如 上右下左,问最多能走几个房间,其中.(dot)代表房间,*代表家具!


思路:

直接bfs,结构体里存 坐标和方向,要注意,最终机器人一定是一个死循环的,所以你可以规定一个房间最多走几次,我规定了走20次后就终止了!
要注意一点,更改方向要算一个状态的!wa了两次!!


详细见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
const int maxn = 20 + 10;
const int inf = 0x3f3f3f3f;

char s[maxn][maxn];
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
int vis[maxn][maxn];
int sx,sy;
int n,m;
int dir;
bool init(int x,int y){
    return x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '*' && vis[x][y] < 20;
}
int cnt= 0 ;
struct Node{
    int x,y,dir;
    Node(int x = 0,int y = 0,int dir = 0):x(x),y(y),dir(dir){}

};
queue<Node>q;
int bfs(){
    while(!q.empty())q.pop();
    vis[sx][sy]++;
    q.push(Node(sx,sy,dir));
    int ans = 0;
    while(!q.empty()){
        Node u = q.front();
        q.pop();
        if (vis[u.x][u.y] == 0)++ans;
        if (vis[u.x][u.y] >= 20)break;
        int xx = u.x + dx[u.dir%4];
        int yy = u.y + dy[u.dir%4];
        if (init(xx,yy)){
            q.push(Node(xx,yy,u.dir%4));
            vis[xx][yy]++;
        }else {
                q.push(Node(u.x,u.y,(u.dir+1)%4));
                vis[u.x][u.y]++;
        }
    }
    return ans;
}
int main(){
    scanf("%d %d",&n,&m);
    for (int i = 0; i < n; ++i)scanf("%s",s[i]);
    for (int i = 0; i < n; ++i){
        for (int j = 0; j < m; ++j){
            if (isalpha(s[i][j])){
                    sx = i,sy = j;
                char ch = s[i][j];
                if (ch == 'U') dir = 0;
                else if (ch == 'R') dir = 1;
                else if (ch == 'D') dir = 2;
                else if (ch == 'L') dir = 3;
            }
        }
    }

        bfs();
        int ans = 0;
        for (int i = 0; i < n; ++i)
            for (int j = 0; j  < m; ++j)
                if (vis[i][j])++ans;
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值