Description of juvenile Lee carefree Aunt sick, Wang Xiaohu introduced him to a trip to fairy Spirit Island, to fairies sister want Elixir to save aunt. Rebellious but filial Li Carefree broke into the Fairy Island, overcome the thousand risks never agree came to the center of the island, found that Xian medicine placed in the depths of the maze. The maze consists of a square of MXN, some of which can be instantaneous seconds Li Carefree monsters, and some squares are safe. Now Lee Carefree want to find the medicine as soon as possible, obviously he should avoid the monster box, and through the fewest squares, and there will be mysterious characters waiting for him. Now ask you to help him achieve this goal.
Shows a sample of the maze and the route of Lee Carefree to find the Xian medicine.
The input input has multiple sets of test data. Each set of test data starts with two nonzero integers M and N, and neither is greater than 20. M represents the number of rows in the maze, and N represents the number of columns. Next there are M lines, each containing n characters, and different characters representing different meanings:
1) ' @ ': the location of young Li's escape;
2) '. ' : squares that can be safely used;
3) ' # ': There are monsters in the squares;
4) ' * ': the location of the Xian medicine.
When two zeros are read in a row, the input ends.
Output for each set of test data, output a row, the line contains the number of pieces that Li Carefree found to pass through the least squares (counting including the initial position of the square). If he can't find the elixir, then output-1. Sample input
8 8[email protected]##...##....#.##.#.##....#.###.#.#...#...###.#....#.*...#...## #6 5.*.#. #.....##.......#... [Email protected]9 6.#. #. .#.*.# .####. .. #... .. #... .. #... .. # ... #[email protected]##. #. #. 0 0
Sample output
108-1
1#include <stdio.h>2#include <string.h>3 intni[4]={0,1,0,-1};4 intnj[4]={1,0,-1,0};5 intqu[401][2],dep[ -];6 Chara[ +][ +];7 intbook[ +][ +];8 intm,n,i,j,k,si,sj,xi,xj,qi,qj,zi,zj,bu=0, ans;9 intMain ()Ten { One /*FILE *fin,*fout; A Fin=fopen ("map.in", "R"); - Fout=fopen ("Answer.out", "w"); - */ intstar,s; the //fscanf (Fin, "%d%d", &m,&n); -scanf"%d%d",&m,&n); - while(m!=0&&n!=0) - { +memset (book,0,sizeof(book)); - for(i=1; i<=m;i++) + //fscanf (FIN, "%s", A[i]); Ascanf"%s", A[i]); at for(i=1; i<=m;i++) - for(j=0;j<=n-1;j++) - { - if(a[i][j]=='@') {qi=i;qj=j;book[i][j]=1;} - if(a[i][j]=='*') {zi=i;zj=J;} - } inStar=0; s=0; ans=-1; -memset (DEP,0,sizeof(DEP)); toMemset (Qu,0,sizeof(qu)); +qu[0][0]=qi;qu[0][1]=QJ; - while(star<=s) the { *xi=qu[star][0]; $xj=qu[star][1];Panax Notoginseng if(xi==zi&&xj==ZJ) - { theans=Dep[star]; + Break; A } the for(k=0; k<=3; k++) + { -si=xi+Ni[k]; $sj=xj+Nj[k]; $ if(si>=1&&si<=m&&sj>=0&&sj<=n-1) - if(a[si][sj]!='#'&&book[si][sj]!=1) - { thequ[++s][0]=si; -qu[s][1]=SJ;Wuyidep[s]=dep[star]+1; thebook[si][sj]=1; - } Wu } -star++; About } $ //fprintf (Fout, "%d\n", ans); -printf"%d\n", ans); - //fscanf (Fin, "%d%d", &m,&n); -scanf"%d%d",&m,&n); A } + /*fclose (Fin); the fclose (fout); - */ return 0; $}
2.5-2727: Cents Island for medicine