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

最新文章

  1. ajax优点与缺点
  2. Android性能优化典范第一季
  3. 使用getopt函数对windows命令行程序进行参数解析
  4. Linux命令详解之—cat命令
  5. php解压zip文件
  6. [ActionScript] AS3解决html与flash鼠标滚轮冲突的问题
  7. bnuoj 4357 传送阵
  8. 【jQuery插件】用jQuery Masonry快速构建一个pinterest网站布局(转)
  9. Sublime ctags 函数跳转插件安装
  10. [Spring入门学习笔记][创建网站URL]
  11. iOS开发系列--打造自己的“美图秀秀”
  12. J2SE知识点摘记(二十六)
  13. 多个viewpager可能产生的问题
  14. ios 软键盘顶起这个页面
  15. SAP RFC函数远程调试跟踪管理软件
  16. this指针随笔
  17. 《剑指offer(第二版)》面试题64——求1+2+...+n
  18. rem 适配
  19. 使用crypt配置Basic Auth登录认证
  20. HQL count(*)

热门文章

  1. 浅谈SpringMVC(二)
  2. A - A
  3. Nanjing GDG Meetup 10月19日线下活动
  4. xStream完美转换XML、JSON_java
  5. 计算BMI指数的小程序
  6. MySQL show binglog event in &#39;log_name&#39;
  7. Linux 安装g++
  8. uglifyjs note
  9. ElasticSearch D3
  10. poj 2228 Naptime dp