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. hdu 1754:I Hate It(线段树,入门题,RMQ问题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例 分类: JavaScript 2014-09-23 10:41 218人阅读 评论(1) 收藏

    前言: 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Sock ...

  3. AxureRP7.0各类交互效果汇总帖(转)

    了便于大家参考,我把这段时间发布分享的所有关于AxureRP7.0的原型做了整理. 以下资源均有对应的RP源文件可以下载. 当然 ,其中有部分是需要通过完成解密游戏[攻略]才能得到下载地址或者下载密码 ...

  4. form表单验证和事件

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  5. Blog Starting...

    30出头,开始Blog记录学习生活的点滴,待40时再回来一看.

  6. SpringHttpInvoker解析3-客户端实现

    主要的配置文件 <bean id="httpInvokerUserService" class="org.springframework.remoting.http ...

  7. 分享Kali Linux 2016.2第46周虚拟机

    分享Kali Linux 2016.2第46周虚拟机该虚拟机使用Kali Linux 2016.2第46周的64位镜像安装.桌面为Gnome模式.该虚拟机配置如下:(1)支持VMWare 9以上的版本 ...

  8. Js+XML 操作

    xml文件Login.xml如下. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8" ?> <L ...

  9. Floyd_Warshall POJ 1847 Tram

    题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...

  10. Dijkstra+计算几何 POJ 2502 Subway

    题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...