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 ...
随机推荐
- Session保存数据
int nameid=dao.isLegalUser(name, password);/ /方法返回int数据 request.setAttribute("nam ...
- 进程间通信-Queue
进程间通信-Queue Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信. 1. Queue的使用 可以使用multiprocessing模块的Queue实现多进程之间的数据传 ...
- java script 模拟鼠标事件
try { var selector1 = "._3-8y:first-child"; var evt = document.createEvent("MouseEven ...
- mysql增加远程连接用户及查看数据库表结构
一.增加远程连接用户 1.用root权限登录数据库 2.加用户:grant all privileges on *.* to '111'@'192.168.1.%' identified by '2 ...
- tf.get_variable()
1. tf.Variable()W = tf.Variable(<initial-value>, name=<optional-name>)1用于生成一个初始值为initial ...
- Django的cookie学习
为什么要有cookie,因为http是无状态的,每次请求都是独立的,但是我们还需要保持状态,所以就有了cookie cookie就是保存在客户端浏览器上的键值对,别人可以利用他来做登陆 rep = r ...
- runloop与线程的关系
- MD5加密获得文件的MD5码
哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才会匹配 ...
- js-移动端android浏览器中input框被软键盘遮住的问题解决方案
我遇到的问题:在一个页面里有一个弹出层之前我给我的最外层加了固定定位 用了下面的方法也不好使:没有办法我将之改为绝对定位层级变高在加上一个顶部标签通过js计算顶部高度来实现满屏遮挡: <sect ...
- 品味性能之道<二>:性能工程师可以具备的专业素养
性能工程师可以具备的专业素养 程序语言原理,包括:C.C++.java及jvm.ASP,因为建站大部分外围应用和中间件都是JAVA编写,大部分的电商平台采用的ASP编写,底层核心系统是C/ ...