CF Cleaner Robot (BFS)

本文介绍了一种算法,用于计算机器人在无限时间内清扫房间的最大面积。房间被描述为矩形网格,其中家具用符号标记,机器人初始位置和方向也给出。通过遵循特定的清扫策略,算法能够计算出机器人能清扫的总面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



清洁机器人的路径规划是指为了覆盖一个区域使用遗传算法来确定最优路径的过程。遗传算法是一种受到自然进化启发的计算方法,适用于求解优化题。 首先,清洁机器人需要了解整个覆盖区域的地图信息,包括墙壁、障碍物、家具等。然后,将该区域进行离散化,将其划分成一系列小格子。每个小格子表示机器人可以移动的一个位置。 接下来,遗传算法开始运行。首先,随机生成一组初始路径作为第一代个体。每个个体(路径)都是由小格子的序列组成的。然后,通过评估每个个体的适应度,确定需要保留下来的个体。适应度可以用来衡量清洁机器人完成覆盖区域任务的效果,例如覆盖的格子数量、覆盖时间等。适应度评估常常结合启发式算法,例如计算路径长度、避开障碍物等。 在选择适应度较高的个体之后,进行遗传操作如交叉和变异,生成下一代的个体。交叉是指将两个个体的一部分路径进行交换,产生新的个体。变异是指在个体的路径中随机选择一个位置,并将其替换为另一个位置。这样可以在搜索过程中引入新的变化,有助于避免陷入局部最优解。 通过不断重复选择、交叉和变异操作,直到达到预定的停止条件(例如达到最大迭代次数或找到了适应度满足要求的解),遗传算法最终会找到覆盖区域题的最优解。 总之,清洁机器人的路径规划使用遗传算法,通过不断选择和变异生成新的路径,最终找到最优解,实现对覆盖区域的有效清洁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值