D - 金樽清酒斗十千(搜索dfs)
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:
- 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 Input
2 3
U..
.*.
4
4 4
R...
.**.
.**.
....
12
3 4
***D
..*.
*...
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)的更多相关文章
- 简单搜索dfs, 简单的修剪搜索
选择最合适的语言做一个项目是非常重要的.但,熟练的掌握自己的武器,这也是非常重要的. ========================================================= ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)
需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...
- POJ 2243 简单搜索 (DFS BFS A*)
题目大意:国际象棋给你一个起点和一个终点,按骑士的走法,从起点到终点的最少移动多少次. 求最少明显用bfs,下面给出三种搜索算法程序: // BFS #include<cstdio> #i ...
- 记忆化搜索(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]) + ...
- 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty
题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
- 深度优先搜索 DFS 学习笔记
深度优先搜索 学习笔记 引入 深度优先搜索 DFS 是图论中最基础,最重要的算法之一.DFS 是一种盲目搜寻法,也就是在每个点 \(u\) 上,任选一条边 DFS,直到回溯到 \(u\) 时才选择别的 ...
随机推荐
- BZOJ 2879: [Noi2012]美食节( 费用流 + 动态加边 )
倒着做菜..然后考虑为当前的人做菜对后面的人的影响就可以了..要动态加边 --------------------------------------------------------------- ...
- BZOJ 1951: [Sdoi2010]古代猪文( 数论 )
显然答案是G^∑C(d,N)(d|N).O(N^0.5)枚举N的约数.取模的数999911659是质数, 考虑欧拉定理a^phi(p)=1(mod p)(a与p互质), 那么a^t mod p = a ...
- java——HashCode和equal方法
equals()反映的是对象或变量具体的值,即两个对象里面包含的值--可能是对象的引用,也可能是值类型的值. 而hashCode()是对象或变量通过哈希算法计算出的哈希值. 之所以有hashCode方 ...
- 10_Segue Example
10 // // ViewController.swift // Segues Example // // Created by ZC on 16/1/10. // Copyright © 2016年 ...
- 美版nexus 5 LG D820才支持CDMA,国际版LG D821不支持
我们都知道nexus 5其实是有两个不同的版本的,分别是LG D820和LG D821,它们在几乎所有的配置和外观上都没有任何的区别,主要区别在通讯模块上,一个支持GSM/CDMA/WCDMA/LTE ...
- HDU 5755 Gambler Bo(高斯消元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5755 [题目大意] 一个n*m由0,1,2组成的矩阵,每次操作可以选取一个方格,使得它加上2之后对 ...
- HDU 4416 Good Article Good sentence(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4416 [题目大意] 给出一个字符串,然后,给出一个字符串集合,问在该字符串中出现,且不在字符串集合 ...
- Android模拟器PANIC: Could not open:问题解决方法
最主要的就是环境变量没有配置或者我们使用的是绝对路径配置环境变量.这时我们只需要修改一下Android的环境变量就可以了. 具体解决方法如下: ①在环境变量中创建变量名:ANDROID_SDK_HOM ...
- QT与JavaScript互调 - 虹的日志 - 网易博客
QT与JavaScript互调 - 虹的日志 - 网易博客 QT与JavaScript互调 2012-05-29 21:43:14| 分类: 技术 | 标签:qt javascript w ...
- Android 标签控件
版本号:1.0 日期:2014.7.24 版权:© 2014 kince 转载注明出处 在有的应用中可能须要设置一些标签来方便用去去查询某些信息,比方手机助手或者购物软件之类都会有一些标签. ...