CodeForces - 589J —(DFS)
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
2 3
U..
.*.
4
4 4
R...
.**.
.**.
....
12
3 4
***D
..*.
*...
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.
题意:给出了一个n*m的字符矩阵,矩阵由‘ * ’和‘ . ’组成,‘ . ’表示空白位置,‘ * ’表示被占用的位置,在矩阵中有一个机器人,用字母‘U’,‘R’,‘D’,或’L‘表示,字母代表了机器人的初始方向,它将往那个方向移动一格,若那个方向走不通(那格是’*‘或边界外),机器人将顺时针旋转90度改变方向。问机器人最终总共走了多少个不重复的格子。
思路:用DFS模拟就好了,而DFS的结束条件就是到达某个格子的次数不超过某一个数,我在代码中设置了10次,其实5次就够了(4次wa了)。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define eps 1e-7
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
char map[][];
int n,m,visit[][],ans,flag;
void change(char &dire) //改变方向
{
if(dire == 'U')
dire = 'R';
else if(dire == 'R')
dire = 'D';
else if(dire == 'D')
dire = 'L';
else if(dire == 'L')
dire = 'U';
} void go(char dire,int x,int y,int &xx,int &yy) //前进一格
{
if(dire == 'U')
{
xx = x - ;
yy = y;
}
else if(dire == 'L')
{
xx = x;
yy = y - ;
}
else if(dire == 'D')
{
xx = x+;
yy = y;
}
else
{
xx = x;
yy = y + ;
}
} void DFS(int x,int y,char dire)
{
if(flag) return;
int xx,yy;
go(dire,x,y,xx,yy); //前进一格
while(map[xx][yy] == '*' || xx>=n || xx< || yy>=m || yy<) //如果那一个不和要求
{
change(dire); //就改变方向,修改的是未前进前的格子处的方向
visit[x][y]++; //标记+1
go(dire,x,y,xx,yy); //再次前进
if(visit[x][y] >= ) //若到达指定次数,就结束递归
{
flag = ;
return;
}
}
if(map[xx][yy] != '*' && xx<n && x>= && yy<m && yy>=) //到达的格子满足要求
{
if(!visit[xx][yy]) ans++; //未走过走过格子,ans++
visit[xx][yy]++;
if(visit[xx][yy] >= ) //到达指定次数
{
flag = ;
return;
}
DFS(xx,yy,dire); //递归
}
} int main()
{
while(cin>>n>>m)
{
char dire;
int x,y;
ans = ;
flag = ;
memset(visit,,sizeof(visit));
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
{
cin>>map[i][j];
if(map[i][j] >='A' && map[i][j] <= 'Z')
{
dire = map[i][j];
visit[i][j] = ;
x = i;
y = j;
}
}
DFS(x,y,dire);
cout<<ans<<endl;
}
return ;
}
CodeForces - 589J —(DFS)的更多相关文章
- CodeForces - 589J(DFS)
题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- CodeForces - 95B(DFS)
题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...
- codeforces 723D(DFS)
题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...
- Cleaner Robot - CodeForces 589J(搜索)
有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面 ...
- Codeforces 761E(DFS)
E. Dasha and Puzzle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 115A- Party(DFS)
A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
随机推荐
- dubbo+springMVC+Spring+Mybatis
1.新建Maven Project 1.1 1.2 2新建Maven Module ——提供者和消费者都需要引用的共同代码块(如entity和service接口) 2.1 2.2 2.3 2. ...
- HTML的属性和css基础
1.name属性: name属性,用于指定标签元素的名称,<a>标签内必须提供href或name属性:<a name ="value"> 2.id属性: 1 ...
- http头解释
<---响应头---> 长连接: Connection: keep-alive 开启长连接 ---- connection 英 [kəˈnekʃn]连接 ...
- Docker Dockerfile 定制镜像(转)
转自: https://yeasy.gitbooks.io/docker_practice/ 及 https://blog.csdn.net/wo18237095579/article/details ...
- New Game! (最短路+建图)
New Game! https://www.nowcoder.com/acm/contest/201/L 题目描述 Eagle Jump公司正在开发一款新的游戏.Hifumi Takimoto作为其中 ...
- 转)安装svn服务器
以下转载自:http://www.linuxidc.com/Linux/2015-01/111956.htm 安装 安装软件包: sudo apt-get install subversion 配置 ...
- pyhon模块之日志模块
#Auther Bob#--*--coding:utf-8--*-- import logging #python一共有5个级别的日志,debug.info.warning.error.critica ...
- GridView上同时定义了 DataSource 和 DataSourceId
VS平台下ASP.NET网站的建立,我们常常要跟数据库打交道,获取数据库的信息,通过GridView控件进行显示,需要为GridView指定 DataSourceId或者DataSource,切忌不可 ...
- Linux之常用命令
1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切换到 ...
- cannot convert from 'wchar_t *' to 'char *' 问题
MFC中使用unicode 会导致cstring之间的转换变的很复杂 经常遇到这样的错误cannot convert from 'wchar_t *' to 'char *' 强制转换成wchar_t ...