题意:给定n*m的矩阵,一个机器人从一个位置,开始走,如果碰到*或者边界,就顺时针旋转,接着走,问你最后机器人最多能走过多少格子。

析:这个题主要是题意读的不大好,WA了好几次,首先是在*或者边界才能转向,其次就是走过的地方也能走,注意这两点,就可以AC了,可以用DFS,也可以用BFS,

我用的DFS。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std ; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
char s[15][15];
int vis[15][15];
int cnt; void dfs(int r, int c, int ok){
if(s[r][c] == '*' || cnt > 5000) return ;
++cnt;
for(int i = 0; i < 4; ++i){
int x = r + dr[(i+ok)%4];
int y = c + dc[(i+ok)%4];
if(is_in(x, y) && s[x][y] != '*'){
vis[x][y] = 1;
dfs(x, y, (ok+i)%4);
return ;
}
}
return ;
} int main(){
while(scanf("%d %d", &n, &m) == 2){
cnt = 0;
memset(s, 0, sizeof(s));
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++i)
scanf("%s", s[i]);
int sx, sy;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(s[i][j] == 'U' || s[i][j] == 'D' || s[i][j] == 'L' || s[i][j] == 'R') sx = i, sy = j; vis[sx][sy] = 1;
if(s[sx][sy] == 'U') dfs(sx, sy, 0);
else if(s[sx][sy] == 'R') dfs(sx, sy, 1);
else if(s[sx][sy] == 'D') dfs(sx, sy, 2);
else if(s[sx][sy] == 'L') dfs(sx, sy, 3);
int ans = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
ans += vis[i][j];
printf("%d\n", ans);
}
return 0;
}

CodeForces 589J Cleaner Robot (DFS,或BFS)的更多相关文章

  1. CodeForces 589J Cleaner Robot

    题目链接 题意:一个机器人打扫卫生,URDL代表初始时机器人面对的方向上右下左. ' . ' 代表可以打扫的, ' * ' 代表家具,如果机器人遇到家具就顺时针转90度,问机器人能打扫多少面积. 题解 ...

  2. CodeForces - 589J —(DFS)

    Masha has recently bought a cleaner robot, it can clean a floor without anybody's assistance. Schema ...

  3. 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest J Cleaner Robot

    Cleaner RobotCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:524288KB     ...

  4. Clone Graph leetcode java(DFS and BFS 基础)

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  5. 数据结构(12) -- 图的邻接矩阵的DFS和BFS

    //////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...

  6. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  7. 在DFS和BFS中一般情况可以不用vis[][]数组标记

    开始学dfs 与bfs 时一直喜欢用vis[][]来标记有没有访问过, 现在我觉得没有必要用vis[][]标记了 看代码 用'#'表示墙,'.'表示道路 if(所有情况都满足){ map[i][j]= ...

  8. 图论中DFS与BFS的区别、用法、详解…

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

  9. 图论中DFS与BFS的区别、用法、详解?

    DFS与BFS的区别.用法.详解? 写在最前的三点: 1.所谓图的遍历就是按照某种次序访问图的每一顶点一次仅且一次. 2.实现bfs和dfs都需要解决的一个问题就是如何存储图.一般有两种方法:邻接矩阵 ...

随机推荐

  1. Fedora20 优化体验

    玩了些许天的fedora系统,到底是加深了对于linux系统的了解 为了便于大家对于fedora系统支持,我将这些天对于fedora的一些不适之处及改进的策略进行了一下小总结.便于新手对于fedora ...

  2. 宏buf_pool_t

    typedef struct buf_pool_struct buf_pool_t; struct buf_pool_struct{ /** @name General fields */ /* @{ ...

  3. 函数flst_remove

    移除 node, node->prev直接指向node->next /*********************************************************** ...

  4. UVa 1401 (Tire树) Remember the Word

    d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...

  5. Android service binder aidl 关系

    /********************************************************************************** * Android servic ...

  6. Java [Leetcode 107]Binary Tree Level Order Traversal II

    题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  7. poj 1236 Network of Schools

    题目描述:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B在学校A的列表中,则A不一定在B的列表中.任务A:计算为 ...

  8. Java 7爆最新漏洞,10年前的攻击手法仍有效

    英文原文:New Reflection API affected by a known 10+ years old attack 据 SECLISTS 透露,他们发现新的 Reflection API ...

  9. centos软件环境

    1,保持能链接外网和yum的可用性. 注意:yum配置项中最好:keepcache=1 2,yum install gcc, gcc-c++, make, cmake, 3, ntfs-3g wget ...

  10. JUC之Atomic系列12大类实例讲解和原理分解

    在java6以后我们不但接触到了Lock相关的锁,也接触到了很多更加乐观的原子修改操作,也就是在修改时我们只需要保证它的那个瞬间是安全的即可,经过相应的包装后可以再处理对象的并发修改,以及并发中的AB ...