D - 金樽清酒斗十千(搜索dfs)
2015-10-23 13:57
288 查看
D - 金樽清酒斗十千
Time Limit:2000MS Memory Limit:524288KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 589JDescriptionMasha 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?InputThe 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).OutputIn 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 InputInput
Time Limit:2000MS Memory Limit:524288KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 589JDescriptionMasha 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?InputThe 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).OutputIn 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 InputInput
2 3 U.. .*.Output
4Input
4 4 R... .**. .**. ....Output
12Input
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[15][15]; int vis[15][15]; 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]>4)return; if(nx<0||ny<0||nx>=w||ny>=h||map[nx][ny]=='*'){//前面把vis放入里面是不行的 //因为走过的点也要走。。。 if(dx==0&&dy==1)dx=1,dy=0;//方向定义错误过。。。 else if(dx==1&&dy==0)dx=0,dy=-1; else if(dx==0&&dy==-1)dx=-1,dy=0; else if(dx==-1&&dy==0)dx=0,dy=1; 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+1); } } int main(){ while(~scanf("%d%d",&w,&h)){ int x,y,dx,dy; for(int i=0;i<w;i++){ scanf("%s",map[i]); for(int j=0;j<h;j++){ if(map[i][j]=='U')dx=-1,dy=0,x=i,y=j; else if(map[i][j]=='R')dx=0,dy=1,x=i,y=j; else if(map[i][j]=='D')dx=1,dy=0,x=i,y=j; else if(map[i][j]=='L')dx=0,dy=-1,x=i,y=j; } }ans=0; memset(vis,0,sizeof(vis)); //printf("%d %d %d %d\n",x,y,dx,dy); vis[x][y]=1; dfs(x,y,dx,dy,1); printf("%d\n",ans); } return 0; }
相关文章推荐
- Android对接微信支付体验
- android使用asynchttp网络请求服务器返回json数据并显示在手机
- 混合开发交互中遇到的一些问题
- java中RandomAccessFile类
- 那些支持着JobDeer的开源项目和云服务
- 存储过程之小实例
- CSS 伪类与伪元素
- 使用strace命令定位和诊断故障的实例分享
- Java泛型详解
- 树状数组求第K小元素
- 一位入行20年的老程序员对新人的建议
- 默然日记20151023
- 如何区别Android手机卡三大运营商
- Jenkins知识地图
- js中的true,false盲点
- SharedPreferences
- Android仿ios录音动画
- php通过会话控制实现身份验证
- <ul>与<ol>的区别
- windows server 2008 R2 配置mediawiki