poj3083--Children of the Candy Corn
来源:互联网 发布:阿里大数据行业分析 编辑:程序博客网 时间:2024/06/07 17:39
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9162 Accepted: 3973
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.
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.
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
Source
South Central USA 2006
给出图,找出以向左优先的步数,向右优先的步数,最短步数
两次dfs 一次bfs , 判断好当前的方向
#include <stdio.h>#include <string.h>char str[50][50] ;int n , m ;int map_str[50][50] ;int dfs_s[4][2] = { {1,0},{-1,0},{0,1},{0,-1} } ;/* 0 3 1 2*/struct node{ int x, y ; int temp ;} p[1000000];void dfs1(int x,int y,int temp,int dir){ if(str[x][y] == 'E') { printf("%d ", temp); return ; } int i = dir - 1 ; if(i == -1) i = 3 ; while( 1 ) { if(i == 0 && x-1 >= 0 && str[x-1][y] != '#') { dfs1(x-1,y,temp+1,i); break; } else if(i == 1 && y+1 < m && str[x][y+1] != '#') { dfs1(x,y+1,temp+1,i); break; } else if(i == 2 && x+1 < n && str[x+1][y] != '#') { dfs1(x+1,y,temp+1,i); break; } else if(i == 3 && y-1 >= 0 && str[x][y-1] != '#') { dfs1(x,y-1,temp+1,i); break; } i++ ; if(i == 4) i = 0 ; } return ;}void dfs2(int x,int y,int temp,int dir){ if(str[x][y] == 'E') { printf("%d ", temp); return ; } int i = dir + 1 ; if(i == 4) i = 0 ; while( 1 ) { if(i == 0 && x-1 >= 0 && str[x-1][y] != '#') { dfs2(x-1,y,temp+1,i); break; } else if(i == 1 && y+1 < m && str[x][y+1] != '#') { dfs2(x,y+1,temp+1,i); break; } else if(i == 2 && x+1 < n && str[x+1][y] != '#') { dfs2(x+1,y,temp+1,i); break; } else if(i == 3 && y-1 >= 0 && str[x][y-1] != '#') { dfs2(x,y-1,temp+1,i); break; } i-- ; if(i == -1) i = 3 ; } return ;}void bfs(int x,int y,int temp){ memset(map_str,0,sizeof(map_str) ) ; p[0].x = x ; p[0].y = y ; p[0].temp = temp ; map_str[x][y] = 1 ; int low = 0 , top = 1 , i ; while(low < top) { node k = p[low++] ; if(str[k.x][k.y] == 'E') { printf("%d\n", k.temp); break; } for(i = 0 ; i < 4 ; i++) { int u = k.x + dfs_s[i][0] ; int v = k.y + dfs_s[i][1] ; if( u >= 0 && u < n && v >= 0 && v < m && map_str[u][v] == 0 && str[u][v] != '#') { p[top].x = u ; p[top].y = v ; p[top++].temp = k.temp + 1 ; map_str[u][v] = 1 ; } } } return ;}int main(){ int t , i , j , x , y ; scanf("%d", &t); while(t--) { scanf("%d %d", &m, &n) ; for(i = 0 ; i < n ; i++) scanf("%s", str[i]); for(i = 0 ; i < n ; i++) { for(j = 0 ; j < m ; j++) { if(str[i][j] == 'S') { x = i ; y = j ; break; } } if(j != m) break; } if(x == 0) { dfs1(x,y,1,2); dfs2(x,y,1,2); } else if(x == n-1) { dfs1(x,y,1,0); dfs2(x,y,1,0); } else if(y == 0) { dfs1(x,y,1,1); dfs2(x,y,1,1); } else if(y == m-1) { dfs1(x,y,1,3); dfs2(x,y,1,3); } bfs(x,y,1) ; } return 0;}
0 0
- POJ3083--Children of the Candy Corn
- POJ3083 Children of the Candy Corn 搜索
- POJ3083--Children of the Candy Corn
- poj3083 Children of the Candy Corn
- poj3083 Children of the Candy Corn
- poj3083--Children of the Candy Corn
- poj3083 Children of the Candy Corn
- POJ3083--Children of the Candy Corn
- POJ3083-Children of the Candy Corn
- poj3083 Children of the Candy Corn
- POJ3083-Children of the Candy Corn
- poj3083 Children of the Candy Corn
- poj3083——Children of the Candy Corn
- POJ3083《Children of the Candy Corn》方法:DFS+BFS
- POJ3083——Children of the Candy Corn
- POJ3083 Children of the Candy Corn dfs+bfs
- POJ3083 Children of the Candy Corn(BFS+DFS水题)
- POJ3083 Children of the Candy Corn BFS+DFS
- Quartz使用-入门使用(java定时任务实现)
- 新企网络世界杯营销怎么做?
- UIMenuController IOS 弹出菜单
- C语言播放声音最简单的两种方法
- $(document).ready、body.Onload()和 $(window).load的区别
- poj3083--Children of the Candy Corn
- logmnr
- php获取汉字首字母的函数
- listview android:cacheColorHint,android:listSelector属性作用
- IOS开发--Description描述方法书写小技巧
- android-vlc for rtsp build OK
- 【轻松玩蓝牙】序列之4:青云蓝牙开发板驱动安装
- CoffeeScript - CoffeeScript安装使用入门
- linux ehci ehci_urb_enqueue之qh_urb_transaction()分析(一)