D - 金樽清酒斗十千(搜索dfs)

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:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<stack>
 7 #include<vector>
 8 using namespace std;
 9 const int INF=0x3f3f3f3f;
10 char map[15][15];
11 int vis[15][15];
12 int w,h,ans;
13 void dfs(int x,int y,int dx,int dy,int step){
14     int nx,ny;
15 //    printf("%d %d %d %d %d %d\n",x,y,dx,dy,vis[x][y],step);
16     nx=x+dx;ny=y+dy;ans=max(ans,step);
17     if(vis[x][y]>4)return;
18     if(nx<0||ny<0||nx>=w||ny>=h||map[nx][ny]=='*'){//前面把vis放入里面是不行的 
19     //因为走过的点也要走。。。 
20         if(dx==0&&dy==1)dx=1,dy=0;//方向定义错误过。。。 
21         else if(dx==1&&dy==0)dx=0,dy=-1;
22         else if(dx==0&&dy==-1)dx=-1,dy=0;
23         else if(dx==-1&&dy==0)dx=0,dy=1;
24         vis[x][y]++;//这也要++; 
25         dfs(x,y,dx,dy,step);
26     }
27     else{
28         if(vis[nx][ny]){//这步也重要,访问过不能能再step+1了但可以走 
29             vis[nx][ny]++;
30             dfs(nx,ny,dx,dy,step);
31             return;
32         }
33         vis[nx][ny]++;
34         dfs(nx,ny,dx,dy,step+1);
35     }
36 }
37 int main(){
38     while(~scanf("%d%d",&w,&h)){
39         int x,y,dx,dy;
40         for(int i=0;i<w;i++){
41             scanf("%s",map[i]);
42             for(int j=0;j<h;j++){
43                 if(map[i][j]=='U')dx=-1,dy=0,x=i,y=j;
44                 else if(map[i][j]=='R')dx=0,dy=1,x=i,y=j;
45                 else if(map[i][j]=='D')dx=1,dy=0,x=i,y=j;
46                 else if(map[i][j]=='L')dx=0,dy=-1,x=i,y=j;
47             }
48         }ans=0;
49         memset(vis,0,sizeof(vis));
50         //printf("%d %d %d %d\n",x,y,dx,dy);
51         vis[x][y]=1;
52         
53         dfs(x,y,dx,dy,1);
54         printf("%d\n",ans);
55     }
56     return 0;
57 }

 



posted @ 2015-10-23 13:57  handsomecui  阅读(473)  评论(0编辑  收藏  举报