CodeForces 589J Cleaner Robot

有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断。求机器人最多能打扫的面积是多少

一开始认为是走到之前清扫过的就停止搜索 后来知道是走到一个四个方向都走过的点才停止搜索

#include<stdio.h>
#include<string.h>

const int MAXN = 17;

 ///‘U‘, ‘R‘, ‘D‘ ‘L‘
int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1} }; //后面是前面90度
int M, N;
char G[MAXN][MAXN];
bool v[MAXN][MAXN][4];

void DFS(int k, int x, int y, int &ans) //k纪录当前方向
{
    if(G[x][y] == ‘.‘)
    {
        ans += 1;
        G[x][y] = ‘#‘;
    }

    for(int i=0; i<4; i++)
    {
        int nx = x+dir[(i+k)%4][0];
        int ny = y+dir[(i+k)%4][1];

        if(nx>=0&&nx<M && ny>=0&&ny<N && G[nx][ny]!=‘*‘)
        {
            if(v[nx][ny][(i+k)%4] == true)
                break;
            v[nx][ny][(i+k)%4] = true;
            DFS((i+k)%4, nx, ny, ans);
            break;
        }
    }
}

int main()
{
    while(scanf("%d%d", &M, &N) != EOF)
    {
        memset(v, 0, sizeof(v));

        int x, y, op;

        for(int i=0; i<M; i++)
        {
            scanf("%s", G[i]);
            for(int j=0; j<N; j++)
            {
                if(G[i][j] == ‘U‘)
                    op = 0, x=i, y=j;
                if(G[i][j] == ‘R‘)
                    op = 1, x=i, y=j;
                if(G[i][j] == ‘D‘)
                    op = 2, x=i, y=j;
                if(G[i][j] == ‘L‘)
                    op = 3, x=i, y=j;
            }
        }

        int ans = 1;
        DFS(op, x, y, ans);

        printf("%d\n", ans);
    }

    return 0;
}
时间: 2024-08-09 02:01:30

CodeForces 589J Cleaner Robot的相关文章

Codeforces 589J Cleaner Robot(DFS)

Cleaner Robot Time Limit: 2000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Submit Status Description Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schematically Masha's room is a rectang

CodeForces 589J Cleaner Robot (DFS,或BFS)

题意:给定n*m的矩阵,一个机器人从一个位置,开始走,如果碰到*或者边界,就顺时针旋转,接着走,问你最后机器人最多能走过多少格子. 析:这个题主要是题意读的不大好,WA了好几次,首先是在*或者边界才能转向,其次就是走过的地方也能走,注意这两点,就可以AC了,可以用DFS,也可以用BFS, 我用的DFS. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #inclu

CodeForce 589J Cleaner Robot

Cleaner Robot 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

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Cleaner Robot - CodeForces 589J(搜索)

有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面积是多少. 分析:直接搜就行了....... 代码如下: ---------------------------------------------------------------------------------------------------------------- #include

【Codeforces 922D】Robot Vacuum Cleaner

[链接] 我是链接,点我呀:) [题意] 让你把n个字符串重新排序,然后按顺序连接在一起 使得这个组成的字符串的"sh"子序列最多 [题解] /* * 假设A的情况好于B * 也就对应了A排在B的前面 * 那么 * As*Bh>Ah*Bs ① * 现在假设B又比C来得好 * 那么有 * Bs*Ch>Bh*Cs ② * 由①可得 * As/Ah>Bs/Bh * 由②可得 * Bs/Bh>Cs/Ch * 那么有 * As/Ah>As/Ch * 即As*Ch&g

codeforces 655D D. Robot Rapping Results Report(拓扑排序+拓扑序记录)

题目链接: D. Robot Rapping Results Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alt

Codeforces 1154D - Walking Robot - [贪心]

题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可充电电池能够充一格电,就用普通电池跑(让可充电池充电),否则就用可充电电池走. AC代码: #include<bits/stdc++.h> using namespace std; const int maxn=2e5+10; int n,a,b; bool s[maxn]; int main()

矩阵 总结

前言 矩阵是一种较为基础的数学工具,OI里面好像不常考?,反正学完数学一本通里的矩阵,做一些矩阵的应用感觉就应该可以了.行列式也是很有趣的东西,我花时间钻研了一下.下面归纳总结一下我做过的一些矩阵的题型 如果定义这种不会请百度吧 -> 百度矩阵 矩阵乘法 矩阵里面最主要的一种.下面是矩阵乘法的一些题. 矩阵快速幂 P3390模板题 基础. 矩阵加速递推数列 P1939模板题 加速加法递推直接设状态转移矩阵就好了. 石头游戏 其实想清楚了石头要从一个格子转移到另一个格子,就可联想到用矩阵来表示格子