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 ...
随机推荐
- Implementing the On Item Checked Event for the TListView Control
The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer d ...
- go 格式化时间戳
func main() { //获取时间戳 timestamp := time.Now().Unix() fmt.Println(timestamp) //格式化为字符串,tm为Time类型 tm : ...
- Zabbix 监控端口状态并邮件报警
Zabbix监控端口 前提 zabbix安装 zabbix邮件报警 添加监控项 添加触发器 添加动作 设置完成后,在配置过报警媒介后也就是 邮件报警 后就完成了.
- Python+Webdriver,中文前加u是unicode格式编码的意思
Python+Webdriver写脚本时,对一些输入框赋值会涉及到输入中文,这时需要在中文前加u 举个例子,在用百度搜索时,要在搜索输入框内输入值, 我用的编码格式是utf-8,向输入框内输入值是:d ...
- win7卸载打印机驱动
无法删除的话停止Print Spooler服务 删除PRINTERS文件夹下面的文件 C:\Windows\System32\spool\PRINTERS目录下所有的文件,重新启动服务:print s ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- IBM MQ + WebSphere + Spring JMS配置方法
IBM MQ + WebSphere + Spring JMS配置方法 首先要在WAS里面配置IBM MQ作为JMS消息的提供者,在WAS管理控制台: Resources->JMS Provi ...
- VINS-mono详细解读
VINS-mono详细解读 极品巧克力 前言 Vins-mono是香港科技大学开源的一个VIO算法,https://github.com/HKUST-Aerial-Robotics/VINS-Mono ...
- php 账号不能同时登陆,当其它地方登陆时,当前账号失效
解决的思路是每当用户登陆时我们必需记录当前的用户id和session_id,如果有人在其它地方用此账号登陆时,我们把此用户id对应的session_id的session文件删除,并重新记录当前的ses ...
- JVM家族史考【转】
说起Java虚拟机,许多Java程序员都会潜意识地把它与Sun(虽然太阳已然西落,但永远值得被记忆) HotSpot虚拟机等同看待,也许还有一些程序员会注意到BEA JRockit和IBM J9,但大 ...