Little Q was trapped in the maze by the evil Devil, Love8909 decided to save her.
There are some traps in the maze, and once you get to the trap, you will be trapped and killed: (Maze
There are some ancient transmission arrays that, once they reach the transmission array, are forced to be transmitted to
The other end of the transmission array.
Now ask you to help love8909 calculate how many steps he needs to take at least to solve
Save the Little Q?
Input
The first behavior is an integer t, which indicates the number of test data groups.
The first behavior of each group of test data is two integers n,m, (1 <= N, M <= 50) indicate
The length and width of the maze.
Next there are n lines, each with M characters, which is a concrete description of the maze.
‘.‘ Represents a safe location, ' # ' represents a trap,
' Q ' indicates the position of the small Q, ' L ' indicates the location of the love8909,
Data guarantee love8909 Only one, the data also guarantee small q only one.
The lowercase ' a '-' Z ' indicates a different transmission array, and the data guarantees that the transmission array
22 pairing.
Output
Each set of data output one row, the minimum number of steps required to rescue small Q, if any
Unable to save small Q, Output-1.
Sample Input
2
5 5
.... L
.###.
B#b#a
##.##
... Qa
5 5
.... L
.###.
.#.#.
##.##
... Q.
Sample Output
3
-1
Note that only the first ' a ' is marked when the current ' a ' is transferred to another ' a '.
/************************************************author:guanjuncreated TIME:2016/7/15 16:11:53File Name: 1.cpp*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 10010#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespaceStd;priority_queue<int,vector<int>,greater<int> >PQ;structnode{intx, y;};structcmp{BOOL operator() (Node A,node b) {if(a.x==b.x)returnA.y>b.y; returnA.x>b.x; }};structnode{intx, y; intdis;};BOOLcmpintAintb) { returnA>b;}intn,m;Charmp[ -][ -];intSx,sy,ex,ey;intvis[ -][ -];intdir[4][2]={1,0,0,1,0,-1,-1,0};map<pair<int,int>,pair<int,int> >MPP;intBFS () {CLE (VIS); Node U={sx,sy,0}; Vis[sx][sy]=1; Queue<node>Q; Q.push (U); while(!Q.empty ()) {u=Q.front (), Q.pop (); if(U.x==ex&&u.y==ey)returnU.dis; Node V; for(intI=0;i<4; i++){ intnx=u.x+dir[i][0]; intny=u.y+dir[i][1]; //cout<<v.x<< "" <<v.y<<endl; if(!vis[nx][ny]&&mp[nx][ny]!='#'&&nx<=n&&nx>=1&&ny<=m&&ny>=1){ CharC=Mp[nx][ny]; if(c<='Z'&&c>='a') {v.x=Mpp[{nx,ny}].first; V.y=Mpp[{nx,ny}].second; V.dis=u.dis+1; Q.push (v); } Else{v.x=nx;v.y=NY; V.dis=u.dis+1; Q.push (v); } Vis[nx][ny]=1; } } } return-1;} Vector<pair<int,int> >v[ -];intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif //freopen ("OUT.txt", "w", stdout); intT; CharC; CIN>>T; while(t--) {mpp.clear (); for(intI=0; i<= -; i++) v[i].clear (); CIN>>n>>m; for(intI=1; i<=n;i++){ for(intj=1; j<=m;j++) {cin>>B; if(c=='L') sx=i,sy=J; if(c=='Q') ex=i,ey=J; if(c<='Z'&&c>='a') {v[int(C-'a')].push_back ({i,j}); } Mp[i][j]=C; } } for(intI=0;i< -; i++){ if(v[i].size () = =2) {mpp[{v[i][0].first,v[i][0].second}]={v[i][1].first,v[i][1].second}; mpp[{v[i][1].first,v[i][1].second}]={v[i][0].first,v[i][0].second}; }} cout<<BFS () <<Endl; } return 0;}
UESTC149 Rescue Small Q