CodeForces #589 J Cleaner Robot 搜索
来源:互联网 发布:苹果手机短信备份软件 编辑:程序博客网 时间:2024/06/08 03:54
题目描述:
Description
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.
Sample Input
Input
2 3U...*.
Output
4
Input
4 4R....**..**.....
Output
12
Input
3 4***D..*.*...
Output
6
Hint
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’表示机器人所在位置以及它将要前进的方向。如果机器人不能在这个方向继续前进,它会顺时针改变方向,也就是由’U’变为’R’,’R’变为’D’,等等。
其中’*’不能前进,’.’可以前进,求机器人最多的路程。
暴力搜即可。
代码如下:
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>using namespace std;int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};char mp[15][15];bool vis[15][15][4];int n,m;int ans;void dfs(int x,int y,int op){ if (mp[x][y]=='.') {ans++;mp[x][y]='#';} for(int i=0; i<4; i++) { int dx=x+dir[(i+op)%4][0]; int dy=y+dir[(i+op)%4][1]; if (dx<n && dx>=0 && dy<m && dy>=0 && mp[dx][dy]!='*') { if (vis[dx][dy][(i+op)%4]==true) break; vis[dx][dy][(i+op)%4]=true; dfs(dx,dy,(i+op)%4); break; } }}int main(){ while(scanf("%d%d",&n,&m)!=-1) { int x,y,op; memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) { scanf("%s",mp[i]); for(int j=0; j<m; j++) { if (mp[i][j]=='U') { x=i;y=j;op=0; } if (mp[i][j]=='R') { x=i;y=j;op=1; } if (mp[i][j]=='D') { x=i;y=j;op=2; } if (mp[i][j]=='L') { x=i;y=j;op=3; } } } ans=1; dfs(x,y,op); printf("%d\n",ans); } return 0;}
- CodeForces #589 J Cleaner Robot 搜索
- Codeforces 589J. Cleaner Robot
- CodeForces 589J Cleaner Robot
- CodeForces 589 J Cleaner Robot
- CodeForces 589 J Cleaner Robot (DFS)
- Codeforces 589J Cleaner Robot(DFS)
- CodeForces 589J -- J. Cleaner Robot (BFS)
- Cleaner Robot(CF 589J)
- CF-589J Cleaner Robot
- DFS J. Cleaner Robot Codeforce 589J(实例与分析)
- CF Cleaner Robot (BFS)
- 【2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J】【暴力模拟】Cleaner Robot 机器人行走 决策固定
- dfs搜索练习题 http://codeforces.com/problemset/problem/589/J
- codeforces 589J
- codeforces 589J
- CodeForces 44J - Triminoes -搜索转构造
- 搜索 J
- 搜索器robot技术
- Javaweb项目虚拟路径
- C语言文件输入输出总结
- Activity的生命周期和四种开启模式之我见
- BZOJ 4563: [Haoi2016]放棋子
- FAILED: InvalidConfigurationException hive.server2.authentication can't be none in non-testing mode
- CodeForces #589 J Cleaner Robot 搜索
- hdu 1151 Air Raid【最小路径覆盖】
- demangle函数实现
- 简单文章发布系统(1)
- Java注解-自定义注解
- ExtJS创建DataGrid
- HTML知识复习(1)
- Java基于jxl与模板导出excel并弹出下载
- Maven类包冲突终极解决小技若干