D - 金樽清酒斗十千

Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u

Description

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:

  1. clean the current cell which a cleaner robot is in;
  2. 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;
  3. 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 Input

Input
2 3
U..
.*.
Output
4
Input
4 4
R...
.**.
.**.
....
Output
12
Input
3 4
***D
..*.
*...
Output
6
题解:真心错到无耐,刚开始,是上下左右方向定义错误,然后就是访问错误,我弄成访问过的不再访问,
显然是错的,接下来是当前点如果上下左右都不能访问的时候,还要转弯里面++;使其不会陷入死循环。。。最后vis是应该大于4。。。又是错了半天,
总结下来,自己烤虑问题还是不全面啊,各种bug。。。
代码dfs:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
char map[][];
int vis[][];
int w,h,ans;
void dfs(int x,int y,int dx,int dy,int step){
int nx,ny;
// printf("%d %d %d %d %d %d\n",x,y,dx,dy,vis[x][y],step);
nx=x+dx;ny=y+dy;ans=max(ans,step);
if(vis[x][y]>)return;
if(nx<||ny<||nx>=w||ny>=h||map[nx][ny]=='*'){//前面把vis放入里面是不行的
//因为走过的点也要走。。。
if(dx==&&dy==)dx=,dy=;//方向定义错误过。。。
else if(dx==&&dy==)dx=,dy=-;
else if(dx==&&dy==-)dx=-,dy=;
else if(dx==-&&dy==)dx=,dy=;
vis[x][y]++;//这也要++;
dfs(x,y,dx,dy,step);
}
else{
if(vis[nx][ny]){//这步也重要,访问过不能能再step+1了但可以走
vis[nx][ny]++;
dfs(nx,ny,dx,dy,step);
return;
}
vis[nx][ny]++;
dfs(nx,ny,dx,dy,step+);
}
}
int main(){
while(~scanf("%d%d",&w,&h)){
int x,y,dx,dy;
for(int i=;i<w;i++){
scanf("%s",map[i]);
for(int j=;j<h;j++){
if(map[i][j]=='U')dx=-,dy=,x=i,y=j;
else if(map[i][j]=='R')dx=,dy=,x=i,y=j;
else if(map[i][j]=='D')dx=,dy=,x=i,y=j;
else if(map[i][j]=='L')dx=,dy=-,x=i,y=j;
}
}ans=;
memset(vis,,sizeof(vis));
//printf("%d %d %d %d\n",x,y,dx,dy);
vis[x][y]=; dfs(x,y,dx,dy,);
printf("%d\n",ans);
}
return ;
}
												

D - 金樽清酒斗十千(搜索dfs)的更多相关文章

  1. 简单搜索dfs, 简单的修剪搜索

    选择最合适的语言做一个项目是非常重要的.但,熟练的掌握自己的武器,这也是非常重要的. ========================================================= ...

  2. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  3. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  4. POJ 2243 简单搜索 (DFS BFS A*)

    题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...

  5. 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof

    题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...

  6. 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

    题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...

  7. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

  8. 【算法入门】深度优先搜索(DFS)

    深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...

  9. 深度优先搜索 DFS 学习笔记

    深度优先搜索 学习笔记 引入 深度优先搜索 DFS 是图论中最基础,最重要的算法之一.DFS 是一种盲目搜寻法,也就是在每个点 \(u\) 上,任选一条边 DFS,直到回溯到 \(u\) 时才选择别的 ...

随机推荐

  1. jsf小例子

    有人问我用过jsf没?   当时没有用过,就看了一下: 写了一个小例子  JSF和struts2 差不多的,都有一些配置和跳转 struts2的action配置和JSF的faces-config.xm ...

  2. mssql字符串分割后的值,把表中不存在的插入表中

    字符串分割后的值,把表中不存在的插入表中 --供大家参考 使用场景,自行思考…… --创建表tb1 Create table tb1 ( cola int, colb ) ) --插入数据 inser ...

  3. 我的Fedora22美化日记

    首先我说一下,我命令是乱打的[不要打我],用之前先google 配置RPMFusion仓库 $ sudo dnf install --nogpgcheck http://download1.rpmfu ...

  4. smarty函数-转载

    Smarty常用函数 2009-08-13 14:05:55|  分类: Php |举报 |字号 订阅   1 .include_once语句: 引用文件路径,路径必需正确.   eg:include ...

  5. jQuery数据缓存data(name, value)详解及实现

    一. jQuery数据缓存的作用 jQuery数据缓存的作用在中文API中是这样描述的:“用于在一个元素上存取数据而避免了循环引用的风险”.如何理解这句话呢,看看我下面的举例,不知道合不合适,如果你有 ...

  6. symfony2 关于是否需要建立多bundle

    http://blog.danielribeiro.org/yes-you-can-have-low-coupling-in-a-symfony-standard-edition-applicatio ...

  7. MySQL mysqlimport 从txt文件中导入数据到mysql数据库

    mysqlimport: 我说这个我们还是先从世界观方法论的高度来理解一下便有更加准确的把握.数据导入不外呼有两个部分 第一部分:目标对象--我们要把数据导给谁(mysqlimport 的目标对象自然 ...

  8. Linux发展历史大事编年表(截止2013年)

    这篇文章主要介绍了Linux发展历史大事编年表(截止2013年),Linux现在已经无处不在,是一个伟大的开原项目,让我一起来看看23年来它的发展历程吧   我们周围到处都有Linux的身影,在家中. ...

  9. 解决Flex4 发布后访问 初始化极其缓慢的问题

    原文http://blog.163.com/vituk93@126/blog/static/170958034201282222046364/ 昨天找了个免费.net空间,想测试一下做的一个简单Fle ...

  10. cocos2dx 字体

    有些时候需要在界面上显示些文字,自然涉及到字体的问题 显示文字使用CCLabelTTF即可,创建方法是 CCLabelTTF(const char* text, const char* font, i ...