codeforcres 589 J - Cleaner Robot(暴力)
2015-10-26 23:14
357 查看
J - Cleaner Robot
Time Limit:2000MS Memory Limit:524288KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 589J
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
Output
Input
Output
Input
Output
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.
View Code
Time Limit:2000MS Memory Limit:524288KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 589J
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 3 U.. .*.
Output
4
Input
4 4 R... .**. .**. ....
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.
/************************************************************************* > File Name: code/hust/20151025/JJ.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月25日 星期日 14时52分17秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; const int dx4[4]={0,1,0,-1}; const int dy4[4]={1,0,-1,0}; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; int w,h; bool vis[15][15][5]; char maze[15][15]; int sx,sy; int dir; bool ok( int x,int y) { if (x>=0&&y>=0&&x<w&&y<h&&maze[x][y]!='*') return true; return false; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif cin>>w>>h; for ( int i = 0 ; i < w ; i++) scanf("%s",maze[i]); for ( int i = 0 ; i < w ; i++) for ( int j = 0 ; j < h ; j++) { if (maze[i][j]=='R') { dir = 0; sx = i; sy = j; } if (maze[i][j] =='D') { dir = 1; sx = i ; sy = j; } if (maze[i][j]=='L') { dir = 2; sx = i; sy = j; } if (maze[i][j]=='U') { dir = 3; sx = i; sy = j; } } int step = 0; ms(vis,false); vis[sx][sy][dir] = true; while (step<=w*h*10) { step++; bool tmp = ok(sx+dx4[dir],sy+dy4[dir]); if (tmp) { sx = sx + dx4[dir]; sy = sy + dy4[dir]; // cout<<"sx:"<<sx<<" sy:"<<sy<<endl; if (vis[sx][sy][dir]) break; vis[sx][sy][dir] = true; } else { dir = dir+1; dir = dir % 4; if (vis[sx][sy][dir]) break; vis[sx][sy][dir] = true; } } int ans = 0; for ( int i = 0 ; i < w ; i++) for ( int j = 0 ; j < h ; j++) for ( int d = 0 ; d < 4 ; d++) if (vis[i][j][d]) { ans++; // cout<<"i:"<<i<<" j:"<<j<<endl; break; } cout<<ans<<endl; #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- automaticallyAdjustsScrollViewInsets 使用
- Action+Service +Dao三层的功能划分
- 系统系统项目管理工程师20151020作业
- spring整合hibernate删除和更新方法不管用
- 黑马程序员——Java基础(十一)之GUI
- HttpClient发送请求后得到的响应内容出现部分乱码的问题
- 初学swift笔记 方法(九)
- 操作系统3-内核
- HDU - 3374 String Problem(最小表示法和最大表示法)
- SuperSlidev2.1滑动门
- Linux修改系统主机名
- 系统系统项目管理工程师20151015作业
- 组播
- HTTP状态码总结
- 关于工作技巧的一些心得
- Maven搭建SpringMVC+Hibernate项目详解
- ios开发系列之应用的后台处理
- 初学swift笔记 结构体(八)
- [BZOJ1637][Usaco2007 Mar]Balanced Lineup
- 直方图和核密度估计图r语言代码