-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
-
Describe
-
-
Young Li 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.
-
-
-
What the hell! Cents Island to seek medicine is a new one of the Paladin off! If the monster can second lead, this game how to play!!!
-
-
-
-
Input
-
The
-
input has more than one set 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 one row, which contains the minimum number of squares that Li Xiaoyao can find to pass through (counting the squares that include the initial position). 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
Thought it was an end to the respective BFS problem, did half feel wrong, began to read the question (only began to see the title 233).
The original is a bare bfs, the mood complex ...
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6 using namespacestd;7 intmx[5]={0,1,0,-1,0},8my[5]={0,0,1,0,-1};9 intM,n;Ten Charmp[ -][ -]; One intSx,sy; A intvis[ -][ -]; - //Queue - structque{ the intx, y; - intdis; -}q[200000]; - // + voidBFS () { -memset (Vis,0,sizeofvis); + intHd=0, tl=1; Aq[++hd]= (que) {sx,sy,0}; at while(hd<=TL) { -Que now=Q[HD]; - for(intI=1; i<=4; i++){ - intnx=now.x+Mx[i]; - intny=now.y+My[i]; - if(nx<=0|| Nx>m | | ny<=0|| Ny>n)Continue; in if(Vis[nx][ny])Continue; - if(mp[nx][ny]=='#')Continue; to if(mp[nx][ny]=='*'){ +printf"%d\n", now.dis+1); - return; the } *vis[nx][ny]=1; $q[++tl]= (que) {nx,ny,now.dis+1};Panax Notoginseng } -hd++; the } +printf"-1\n"); A return; the } + intMain () { - while(SCANF ("%d%d", &m,&n) && m &&N) { $ inti,j; $ BOOLflag=0; - for(i=1; i<=m;i++){ -scanf"%s", mp[i]+1); the if(!flag) for(j=1; j<=n;j++){ - if(mp[i][j]=='@'){Wuyisx=i;sy=J; theflag=1; - Break; Wu } - } About } $ BFS (); - } - return 0; -}
Openjudge 2727 cents on the island to seek medicine