2-5-6264: Out of the maze

Source: Internet
Author: User

Describe

When you are standing in a maze, you tend to lose your sense of direction by the intricacies of the road, and if you can get a maze map, things can become very simple.
Assuming you've got a blueprint for a n*m maze, please find the shortest path from the starting point to the exit.

The first line of input is two integers n and m (1<=n,m<=100), representing the number of rows and columns of the maze.
Next n rows, each line of a string of length m, represents the layout of the entire maze. Character '. ' Represents the open space, ' # ' represents the wall, ' S ' represents the starting point, ' T ' represents the exit. The minimum number of steps to take to output output from the starting point to the exit. Sample input

3 3s#t.# ....

Sample output

6
1#include <stdio.h>2#include <string.h>3 intni[4]={0,1,0,-1};4 intnj[4]={1,0,-1,0};5 intqu[100 on][2],dep[100xx];6 Chara[101][101];7 intBook[101][101];8 intm,n,i,j,k,si,sj,xi,xj,qi,qj,zi,zj,bu=0, ans;9 intMain ()Ten { One     inth,t; Ascanf"%d%d",&m,&n); -memset (book,0,sizeof(book)); -      for(i=1; i<=m;i++) thescanf"%s", A[i]); -      for(i=1; i<=m;i++) -          for(j=0; j<=n-1; j + +) -         { +             if(a[i][j]=='S') {qi=i;qj=j;book[i][j]=1;} -             if(a[i][j]=='T') {zi=i;zj=J;} +         } AH=0; t=0; ans=-1; atmemset (DEP,0,sizeof(DEP)); -Memset (Qu,0,sizeof(qu)); -qu[0][0]=qi;qu[0][1]=QJ; -          while(h<=t) -         { -xi=qu[h][0]; inxj=qu[h][1]; -             if(xi==zi&&xj==ZJ) to             { +ans=Dep[h]; -                  Break; the             } *              for(k=0; k<=3; k++) $             {Panax Notoginsengsi=xi+Ni[k]; -sj=xj+Nj[k]; the                 if(si>=1&&si<=m&&sj>=0&&sj<=n-1) +                     if(a[si][sj]!='#'&&book[si][sj]!=1) A                     { thequ[++t][0]=si; +qu[t][1]=SJ; -dep[t]=dep[h]+1; $book[si][sj]=1; $                     } -             } -h++; the         } -printf"%d\n", ans);Wuyi     return 0; the}

2-5-6264: Out of the maze

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.