博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForce 589J Cleaner Robot
阅读量:4691 次
发布时间:2019-06-09

本文共 5321 字,大约阅读时间需要 17 分钟。

Cleaner Robot
 

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.

 

题意:

  给你一个地图, ‘ . ’空地板,‘*’表示家具,字母表示机器人的位置,‘R''L'‘D’‘U’表示方向,

  当机器人不能前进时,就顺时针旋转九十度,继续前进,注意相同的路,可以被重复访问

思路:

  模拟题,dfs模拟每个状态,当经过某地的次数很大时退出

AC代码:

1 # include 
2 # include
3 # include
4 using namespace std; 5 6 const int MAX = 12; 7 8 char map[MAX][MAX]; 9 int vis[MAX][MAX]; 10 int n, m; 11 12 bool ok(int x, int y) 13 { 14 if(x >= 1 && x <= n && y >= 1 && y <= m) 15 return true; 16 return false; 17 } 18 19 void dfs(int x, int y, char ch) 20 { 21 if(vis[x][y] > 10) 22 return; 23 vis[x][y]++; 24 25 if(ch == 'U') 26 { 27 int newx = x - 1; 28 int newy = y; 29 if(!ok(newx, newy) || map[newx][newy] == '*') 30 dfs(x, y, 'R'); 31 else 32 dfs(newx, newy, 'U'); 33 } 34 else if(ch == 'R') 35 { 36 int newx = x; 37 int newy = y + 1; 38 if(!ok(newx, newy) || map[newx][newy] == '*') 39 dfs(x, y, 'D'); 40 else 41 dfs(newx, newy, 'R'); 42 } 43 else if(ch == 'D') 44 { 45 int newx = x + 1; 46 int newy = y; 47 if(!ok(newx, newy) || map[newx][newy] == '*') 48 dfs(x, y, 'L'); 49 else 50 dfs(newx, newy, 'D'); 51 } 52 else if(ch == 'L') 53 { 54 int newx = x; 55 int newy = y - 1; 56 if(!ok(newx, newy) || map[newx][newy] == '*') 57 dfs(x, y, 'U'); 58 else 59 dfs(newx, newy, 'L'); 60 } 61 } 62 63 64 65 int main() 66 { 67 68 while(cin >> n >> m) 69 { 70 int h, l; 71 char ch; 72 73 memset(vis, 0, sizeof(vis)); 74 for(int i = 0; i <= MAX; i++) 75 for(int j = 0; j <= MAX; j++) 76 map[i][j] = '*'; 77 78 for(int i = 1; i <= n; i++) 79 for(int j = 1; j <= m; j++) 80 { 81 cin >> map[i][j]; 82 if(map[i][j] == 'U' || map[i][j] == 'R' || map[i][j] == 'D' || map[i][j] == 'L') 83 { 84 h = i; 85 l = j; 86 ch = map[i][j]; 87 } 88 } 89 90 dfs(h, l, ch); 91 92 int jishu = 0; 93 for(int i = 1; i <= n; i++) 94 for(int j = 1; j <= m; j++) 95 { 96 if(vis[i][j]) 97 jishu++; 98 } 99 cout << jishu << endl;100 101 }102 return 0;103 }
View Code

 

 

 

转载于:https://www.cnblogs.com/lyf-acm/p/5791538.html

你可能感兴趣的文章
getTickCount()函数 VS GetTickCount()函数
查看>>
嵌入式jetty
查看>>
2017~回顾分享
查看>>
使用svn——项目的目录布局
查看>>
RSA System.Security.Cryptography.CryptographicException
查看>>
webservice整合spring cxf
查看>>
[解题报告] 100 - The 3n + 1 problem
查看>>
Entity Framework 学习高级篇1—改善EF代码的方法(上)
查看>>
Mybatis逆向工程配置文件详细介绍(转)
查看>>
String类的深入学习与理解
查看>>
不把DB放进容器的理由
查看>>
OnePage收集
查看>>
Java parseInt()方法
查看>>
yahoo的30条优化规则
查看>>
[CCF2015.09]题解
查看>>
[NYIST15]括号匹配(二)(区间dp)
查看>>
json_value.cpp : fatal error C1083: 无法打开编译器生成的文件:No such file or directory
查看>>
洛谷 P1101 单词方阵
查看>>
Swift DispatchQueue
查看>>
C#和JAVA 访问修饰符
查看>>