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. .NET Nancy 详解(四) Self Host

    Self Host 使得Nancy 能够在任意application 中启动,无论是console 还是windows service.这期我们使用的版本是Nancy v0.4.0. Demo 首先看 ...

  2. js判断手机端Android手机还是iPhone手机

    /*判断当前设备是平板.安卓.苹果设备*/ <script type="text/javascript"> function fBrowserRedirect(){ v ...

  3. HTML5头部标签备忘

    DOCTYPE DOCTYPE(Document Type),该声明位于文档中最前面的位置,处于html 标签之前,此标签告知浏览器文档使用哪种HTML 或者XHTML 规范. 推荐使用HTML5 推 ...

  4. 小甲鱼PE详解之IMAGE_NT_HEADERS结构定义即各个属性的作用(PE详解02)

    PE Header 是PE相关结构NT映像头(IMAGE_NT_HEADER)的简称,里边包含着许多PE装载器用到的重要字段.下边小甲鱼将为大家详细讲解哈~ (视频教程:http://fishc.co ...

  5. Arduino101学习笔记(九)—— 中断函数

    1.设置中断函数 //***************************************************************************************** ...

  6. 《数据结构与算法分析》学习笔记(三)——链表ADT

    今天简单学习了下链表,待后续,会附上一些简单经典的题目的解析作为学习的巩固 首先要了解链表,链表其实就是由一个个结点构成的,然后每一个结点含有一个数据域和一个指针域,数据域用来存放数据,而指针域则用来 ...

  7. Linux学习笔记(9)Linux常用命令之关机重启命令

    (1)shutdown shutdown命令用于关机重启,其语法格式为: shutdown [选项] 时间 其中,-c选项表示取消前一个设置的shutdown命令,-h命令表示关机,-r命令表示重启 ...

  8. java Clone使用方法详解

    java"指针"       Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文会试图澄清这一概念.并且由于Java不能 通过 ...

  9. linux查看和修改系统时间

    设置日期:date -s 20091112 设置时间:date -s 18:30:50 日期和时间一起设置: date 111218302009 (月日时分年) date -s "20091 ...

  10. 创建一个Portlet工程

    使用Liferay的SDK创建一个简单的Portlet,此Portlet不包括业务逻辑.不包括数据库,只有简单的页面展现,用以说明Portlet的开发过程. 一.创建Portlet工程 1.打开Lif ...