Children of the Candy Corn

xiaoxiao2021-02-28  149

Children of the Candy Corn

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 77   Accepted Submission(s) : 30
Problem 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'. <br> <br>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 ('#'). <br> <br>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 2 8 8 ######## #......# #.####.# #.####.# #.####.# #.####.# #...#..# #S#E#### 9 5 ######### #.#.#.#.# S.......E #.#.#.#.# #########   Sample Output 37 5 5 17 17 9 题目大意:给出地图,问你沿着向左优先和向右优先,以及最短路径,出去进入或者说出去的1体力也算上 思路:最短路径一看就明白 这种题绝壁bfs 对于左右优先一直没什么思路,左右优先其实就是首先向左看,可行则走,反之则向前看,向右,向后,右优先同理 对于向左向右优先没必要标记走没走过了,样例1 2 自己走一遍就会很明白 wa了n次,对左右优先的我也是看了其他人的代码,最主要的就是方向的判断 Source Code#include<stdio.h> #include<string.h> #include<iostream> #include<queue> using namespace std; struct node{ int x; int y; int step; }now,next; char map[100][100]; int vis[100][100]; int dir[8][2]={-1,0,0,1,1,0,0,-1}; int ex,ey,sx,sy,h,w; int dx[]={0,-1,0,1}; int dy[]={-1,0,1,0}; int bfs() { int i,j; queue<node>q; now.x=sx; now.y=sy; now.step=0; vis[sx][sy]=1; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.x==ex&&now.y==ey) { return now.step; } for(i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(next.x==ex&&next.y==ey) return now.step+1; if(next.x>=0&&next.x<w&&next.y>=0&&next.y<h&&!vis[next.x][next.y]&&map[next.x][next.y]!='#') { vis[next.x][next.y]=1; next.step=now.step+1; q.push(next); } } } return -1; } int turn1(int x,int y) { int ans=1; int flag=0; while(!(x==ex&&y==ey)) { if(flag==0) flag=3; else flag--; if(flag<0)flag=1; int xx=x+dir[flag][0]; int yy=y+dir[flag][1]; if(map[xx][yy]=='#'||xx<0||xx>=w||yy<0||yy>=h) { while(1) { flag=(flag+1)%4; xx=x+dir[flag][0]; yy=y+dir[flag][1]; if(map[xx][yy]!='#'&&!(xx<0||xx>=w||yy<0||yy>=h)) break; } } ans++; x=xx;y=yy; } return ans; } int turn2(int x,int y) { int ans=1; int flag=0; while(!(x==ex&&y==ey)) { flag=(flag+1)%4; int xx=x+dir[flag][0]; int yy=y+dir[flag][1]; if(map[xx][yy]=='#'||xx<0||xx>=w||yy<0||yy>=h) { while(1) { if(flag==0) flag=3; else flag--; xx=x+dir[flag][0]; yy=y+dir[flag][1]; if(map[xx][yy]!='#'&&!(xx<0||xx>=w||yy<0||yy>=h)) break; } } ans++; x=xx;y=yy; } return ans; } int main() { int i,j; int t; cin>>t; while(t--) { memset(vis,0,sizeof(vis)); cin>>h>>w; for(i=0;i<w;i++) { scanf("%s",map[i]); for(j=0;j<h;j++) { if(map[i][j]=='S') { sx=i; sy=j; } if(map[i][j]=='E') { ex=i; ey=j; } } } cout<<turn1(sx,sy)<<" "<<turn2(sx,sy)<<" "<<bfs()+1<<endl; } return 0; }  
转载请注明原文地址: https://www.6miu.com/read-36143.html

最新回复(0)