您的位置:首页 > 其它

CF Cleaner Robot (BFS)

2015-10-20 15:45 204 查看
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

题意:机器人开始有个朝向,然后会一直按着这个方向走,知道不能走,就顺时针转动90 度,然后继续上述操作;问机器人最多能走几步;

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: