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.

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 ;
}

codeforces589J 简单dfs,队列的更多相关文章

  1. C语言 简单的队列(数组队列)

    //简单的队列 #include<stdio.h> #include<stdlib.h> #define datatype int #define N 10 //定义队列结构体 ...

  2. Red and Black(简单dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 哈,又一款超级简单的队列(MQ)实现方案来了~

    开源的消息队列已经很多了,但大部分很重,实际环境下,很多可能只是使用到了一点功能而已,杀鸡使用牛刀,着实有些浪费了.很多时候,我们只想要一片绿叶,但它们给了我们整个的春天,很难消化.本着DIR精神, ...

  4. simple简单消息队列

    一:介绍 1.优缺点 简单,但是耦合性较高. 这种模式是生产者与消费者一一对应,就是一个产生者,有一个消费者来消费. 如果,多个消费者想消费一个队列中的消息就不适合了.这种情况在后面会接着介绍. 2. ...

  5. Redis简单延时队列

    Redis实现简单延队列, 利用zset有序的数据结构, score设置为延时的时间戳. 实现思路: 1.使用命令 [zrangebyscore keyName socreMin socreMax] ...

  6. 用redis实现简单的队列

    在工作中,时常会有用到队列的场景,比较常见的用rabbitMQ这些专业的组件,官网地址是:http://www.rabbitmq.com,重要的是官方有.net的客户端,但是如果对rabbitMQ不熟 ...

  7. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  8. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  9. POJ1979 Red and Black (简单DFS)

    POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

随机推荐

  1. 【网络资料】如何优雅地使用Sublime Text3

    如何优雅地使用Sublime Text3 Sublime Text:一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制,用她来写代码,绝对是一种享受.相 ...

  2. dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get

    在64位的操作系统安装oracle10g 软件安装完成后,使用dbca建库的时候报下面的错: $ dbcaUnsatisfiedLinkError exception loading native l ...

  3. 攻城狮在路上(叁)Linux(二十四)--- linux设置开机挂载及镜像文件挂载

    虽然可以手动进行文件系统的挂载,但是每次都手动挂载就会很麻烦,开机挂载的目的就是实现文件系统的自动挂载. 一.开机挂载:/etc/fstab及/etc/mtab 主要是通过修改/etc/fstab文件 ...

  4. Liunx-https-java.lang.NoClassDefFoundError: javax/crypto/SunJCE_b

    错误信息: java.lang.NoClassDefFoundError: javax/crypto/SunJCE_b at javax.crypto.KeyGenerator.a(DashoA13* ...

  5. CG&CAD resource

    Computational Geometry The Geometry Center (UIUC) Computational Geometry Pages (UIUC) Geometry in Ac ...

  6. FastDFS简介

    一.FastDFS概述: FastDFS是一个开源的轻量级分布式文件系统,他对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.下载)等,解决了大容量存储和负载均衡的问题,高度追求高性能 ...

  7. Java的静态导入

    静态导入作用是可以适当减少代码量,但实际上减少得很有限,实际应用中也用的不多,但是作为Java的特性,我们应该适当了解: //静态导入方法或者常量 import static java.lang.Sy ...

  8. HDU 3461 Code Lock(并查集)

    很好的一个题,思想特别6 题意:给你小写字母个数n,每个字母可以向上翻动,例如:d->c,a->z.然后给你m对数(L,R)(L<=R),表示[L,R]之间可以同时向上翻动,且翻动后 ...

  9. 对android录制的NV21视频数据进行旋转(90,180,270)与剪切

    android默认的视频采集格式是NV21,(属于YUV420) 在onPreviewFrame中传进来的byte[] data即为NV21格式. 旋转算法 对NV21进行顺时针旋转90度,180度和 ...

  10. MVC LINQ to SQL: Basic Concepts and Features

    http://www.codeproject.com/Articles/215712/LINQ-to-SQL-Basic-Concepts-and-Features