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. Big Data, MapReduce, Hadoop, and Spark with Python

    此书不错,很短,且想打通PYTHON和大数据架构的关系. 先看一次,计划把这个文档作个翻译. 先来一个模拟MAPREDUCE的东东... mapper.py class Mapper: def map ...

  2. Could not link against boost_system 解决办法

    Could not link against boost_system 解决办法: 先安装 libboost-all-dev ./configure --with-incompatible-bdb - ...

  3. APP设计尺寸规范大全,APP界面设计新手教程【官方版】(转)

    正值25学堂一周年之际,同时站长和APP设计同仁们在群里(APP界面设计 UI设计交流群,APP界面设计⑥群 APPUI设计③群58946771 APP设计资源⑤群 386032923欢迎大家加入交流 ...

  4. 直接拿来用!最火的Android开源项目(一) (转)

    对于开发者而言,了解当下比较流行的开源项目很是必要.利用这些项目,有时能够让你达到事半功倍的效果.为此,CSDN特整理了GitHub上最受欢迎的Android及iOS开源项目,本文详细介绍了20个An ...

  5. loj 1257 (求树上每一个点到树上另一个点的最长距离)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1257 思路:首先需要用到一个知识点就是树上任一点到树上最长直径的某一个端点的距离最远, ...

  6. 标准MDL方法修改Page、NonPage内存的属性

    typedef struct _REPROTECT_CONTEXT { PMDL   Mdl; PUCHAR LockedVa; } REPROTECT_CONTEXT, * PREPROTECT_C ...

  7. HTML的格式、内容容器、表格标签

    HTML(Hyper Text Markup Language,超文本标记语言)超指的是超链接. <html>    --开始标签 <head> 网页上的控制信息 <ti ...

  8. Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法

    Error -26488: Could not obtain information about submitted file "E:\.jpg": _stat32 rc=-1, ...

  9. ROC曲线绘制

    ROC 曲线绘制 个人的浅显理解:1.ROC曲线必须是针对连续值输入的,通过选定不同的阈值而得到光滑而且连续的ROC曲线,故通常应用于Saliency算法评价中,因为可以选定0~255中任意的值进行阈 ...

  10. Liferay 6.2 改造系列之二:清理不需要的Portlet

    一.特殊Portlet: 以下Portlet数据特殊用途的Portlet,去除后会出现运行错误: 1.站点模版 通过com.liferay.portal.events.AddDefaultLayout ...