博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3083 -- Children of the Candy Corn
阅读量:6933 次
发布时间:2019-06-27

本文共 4815 字,大约阅读时间需要 16 分钟。

Children of the Candy Corn
 
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9544   Accepted: 4136

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.
One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)
As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.
Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#').
You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9

思路:依次DFS出右转左转的步数,BFS出最小步数。。
1 /*======================================================================  2  *           Author :   kevin  3  *         Filename :   ChildrwenOftheCandyCorn.cpp  4  *       Creat time :   2014-06-08 15:37  5  *      Description :  6 ========================================================================*/  7 #include 
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 50 15 using namespace std; 16 char grap[M][M]; 17 int w,h,sx,sy,ex,ey; 18 int vis[M][M],cntl,cntr; 19 int minsteps[M][M]; 20 int dir0[4][2]={ { 0,-1},{-1,0},{ 0,1},{ 1,0}};//dfs needed 21 int dir[4][4][2] = { //bfs needed 22 { { 0,-1},{-1,0},{ 0,1},{ 1,0}}, 23 { {-1,0},{ 0,1},{ 1,0},{ 0,-1}}, 24 { { 0,1},{ 1,0},{ 0,-1},{-1,0}}, 25 { { 1,0},{ 0,-1},{-1,0},{ 0,1}} 26 }; 27 struct Node 28 { 29 int x,y; 30 }; 31 void BFS() 32 { 33 queue
que; 34 Node node; 35 node.x = sx; node.y = sy; 36 que.push(node); 37 vis[sx][sy] = 1; 38 minsteps[sx][sy] = 1; 39 int flag = 0; 40 while(que.empty() != true){ 41 Node T = que.front(); 42 que.pop(); 43 if(T.x == ex && T.y == ey) return ; 44 for(int i = 0; i < 4; i++){ 45 int x = T.x + dir[1][i][0]; 46 int y = T.y + dir[1][i][1]; 47 if(grap[x][y] == '.' && !vis[x][y]){ 48 node.x = x; node.y = y; 49 vis[x][y] = 1; 50 que.push(node); 51 minsteps[x][y] = minsteps[T.x][T.y]+1; 52 } 53 else if(grap[x][y] == 'E'){ 54 minsteps[x][y] = minsteps[T.x][T.y]+1; 55 flag = 1; break; 56 } 57 } 58 if(flag) break; 59 } 60 } 61 int lDFS(int i,int j,int s,int now) 62 { 63 if(grap[i][j]=='E')return s; 64 for(int d=now;d
now;--d) 78 { 79 int di=i+dir0[d%4][0]; 80 int dj=j+dir0[d%4][1]; 81 if(grap[di][dj]!='#')return rDFS(di,dj,s+1,(d+1)%4); 82 } 83 return -1; 84 } 85 int main(int argc,char *argv[]) 86 { 87 int cas; 88 scanf("%d",&cas); 89 while(cas--){ 90 scanf("%d%d",&w,&h); 91 clr(vis,0); 92 memset(grap,'#',sizeof(grap)); 93 clr(minsteps,0); 94 for(int i = 1; i <= h; i++){ 95 getchar(); 96 for(int j = 1; j <= w; j++){ 97 scanf("%c",&grap[i][j]); 98 if(grap[i][j] == 'S'){ 99 sx = i; sy = j;100 }101 if(grap[i][j] == 'E'){102 ex = i; ey = j;103 }104 }105 }106 printf("%d %d ",lDFS(sx,sy,1,0),rDFS(sx,sy,1,0));107 BFS();108 printf("%d\n",minsteps[ex][ey]);109 }110 return 0;111 }
View Code

 

转载于:https://www.cnblogs.com/ubuntu-kevin/p/3885258.html

你可能感兴趣的文章
逐行阅读redux源码(二)combineReducers
查看>>
javascript之实现bind
查看>>
JS学习系列08 - 内存分配
查看>>
入门|机器学习中常用的损失函数你知多少?
查看>>
JVM -- 运行时栈帧结构简介
查看>>
TiDB 源码阅读系列文章(六)Select 语句概览
查看>>
手把手Fiddler掌握
查看>>
Android Paint应用之自定义View实现进度条控件
查看>>
深入浅出Websocket(二)分布式Websocket集群
查看>>
DOM节点删除方法小结
查看>>
LeetCode 简要日记 455 & 104
查看>>
(十三) 构建dubbo分布式平台-dubbo管控台安装
查看>>
详解动态规划最长公共子序列--JavaScript实现
查看>>
使用索引绘图(转)
查看>>
Kafka简单使用
查看>>
常用的布局?
查看>>
Java并发编程实战笔记2:对象的组合
查看>>
GreenDao使用注意事项
查看>>
HTTP协议
查看>>
Filter 设计模式编码实践
查看>>