Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J题(BFS)
2015-10-25 10:25
399 查看
J. Cleaner Robot
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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 test(s)
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.
题意:给出机器人的走路方式,每次朝着能走的方向一直走,无法走的时候顺时针旋转90 度,继续走,继续旋转,,,问最后能记过几个不同的格子;
解法:bfs
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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 test(s)
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.
题意:给出机器人的走路方式,每次朝着能走的方向一直走,无法走的时候顺时针旋转90 度,继续走,继续旋转,,,问最后能记过几个不同的格子;
解法:bfs
#include<bits/stdc++.h> using namespace std; const int maxn=113; const int inf=1<<27; #define LL long long #define P pair<int,int> #define X first #define Y second #define pb push_back #define cl(a,b) memset(a,b,sizeof(a)); int n,m; int si,sj; char a[maxn][maxn]; int vis[maxn][maxn]; bool pan(int x,int y){ if(x<0||x>=n||y<0||y>=m||a[x][y]=='*'||vis[x][y]>4)return false; return true; } struct node{ int x,y; char dir; node(int a,int b,char c){ x=a;y=b;dir=c; } node(){} }; void bfs(){ queue<node> q; cl(vis,0); q.push(node(si,sj,a[si][sj])); vis[si][sj]=1; while(!q.empty()){ node t=q.front();q.pop(); int x=t.x,y=t.y; char dir=t.dir; if(vis[x][y]>4)break; //printf("===%d\n",vis[x][y]); //printf("x = %d,y = %d dir = %c\n",x,y,dir); //system("pause"); if(dir=='U'){ if(pan(x-1,y)){ q.push(node(x-1,y,'U')); vis[x-1][y]++; } else { q.push(node(x,y,'R')); vis[x][y]++; } } if(dir=='D'){ if(pan(x+1,y)){ q.push(node(x+1,y,'D')); vis[x+1][y]++; } else { q.push(node(x,y,'L')); vis[x][y]++; } } if(dir=='L'){ if(pan(x,y-1)){ q.push(node(x,y-1,'L')); vis[x][y-1]++; } else { q.push(node(x,y,'U')); vis[x][y]++; } } if(dir=='R'){ if(pan(x,y+1)){ q.push(node(x,y+1,'R')); vis[x][y+1]++; } else { q.push(node(x,y,'D')); vis[x][y]++; } } } } int main(){ while(~scanf("%d%d",&n,&m)){ for(int i=0;i<n;i++){ scanf("%s",a[i]); for(int j=0;j<m;j++){ if(isalpha(a[i][j])){ si=i;sj=j; } } } bfs(); int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++)if(vis[i][j])ans++; } printf("%d\n",ans); } return 0; }
相关文章推荐
- 数学之美读书笔记3
- bzoj1726: [Usaco2006 Nov]Roadblocks第二短路
- Vim中不得不知的中级技巧(一)
- Codevs P1155 金明的预算方案
- LeetCodeOJ_206_Reverse Linked List
- [cb] C# 事件编程
- android悬浮窗口的实现
- iOS开发中常见的解析XML的类库以及简要安装方法
- ubuntu 安装sublime
- Android之浮动小窗口
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest I题(模拟)
- PHP微信开发---简单的文本自动回复
- 栈的基本操作(链表)
- java多进程中waitFor()的作用
- (算法)等概率选出m个整数
- WINDOWS 10 LTSB安装截图
- 二叉树中序遍历的三种方法
- A == B ?(杭州电2054)
- EntityFramework 使用经验
- 栈的基本操作(顺序表)