ZSC freshman race Marsh leap fish have already seen through everything

Source: Internet
Author: User

Description

The Moor fish opens the password door and discovers that the door is a maze-like room, and the sign on the wall reads: There is a treasure chest somewhere in the room, but the chest is locked and the key is in the corner of the room. The swamp fish is interested in what's in the chest, but it has to get the key before it can open the chest. However, the Marsh leap Fish has already seen through everything, it saw the layout of the room, now give the layout of the room, ask the marsh jump fish to get the key and open the Treasure box at least how many steps to take. The marsh jump fish can only walk one step up, down, left, and right in one direction at a time, but if that position is a wall, it cannot go to that position (obviously, the marsh-jumping fish cannot wear the wall).

Input

The first line of input is an integer t (0<t<20), which represents the next T-group data.


The first row of each group of data has two integers n,m (0<n,m≤10), n represents the width of the room, and m represents the length of the room.

Next n lines, each line has m characters, ' S ' indicates the start of the marsh jump where the fish is located, ' # ' stands for the Wall, ' * ' stands for the Clearing, ' K ' stands for the key, ' B ' represents the chest. The key is only one.

See sample input for details.

Output

For each set of data, the output line contains an integer x,x represents the minimum number of steps required to get the key and open the chest for the marsh jump. If the marsh jump does not get the key and opens the chest (i.e. where the key is not reached or where the chest is located) output-1.

Sample Input6*** #B #s**#*###*** #K #*#*#***#*#Sample Output -HINT

For sample data, the room is 5 units wide and 6 units long.


The minimum number of steps required to start from S to K is 8, while the minimum number of steps required to start from K to B is 9.

So the answer is 8 + 9 = 17.


Please use scanf ("%s", s), or cin>> s; to read the string to avoid the case of missed reads and multiple reads.

Source

"American Cup" second freshman race, author:13 Ling Jiawei

Idea: Using BFS breadth First search .... Call two BFS to calculate t1,t2
#include <iostream>#include<cstdio>#include<queue>using namespacestd;Charstr[ A][ A];intvis[ A][ A],n,m;structnode{intx; inty;};intBFS (node A,node b) {memset (Vis,0,sizeof(VIS));    Node c,temp; Queue<node>Q;    Q.push (a);  while(!Q.empty ()) {C=Q.front ();        Q.pop (); //cout<<c.x<< "" <<c.y<<endl;        if(str[c.x+1][c.y]!='#'&&!vis[c.x+1][c.y]&&c.x+1<N) {vis[c.x+1][c.y]=vis[c.x][c.y]+1; Temp.x=c.x+1, temp.y=c.y;        Q.push (temp); }        if(Str[c.x-1][c.y]!='#'&&!vis[c.x-1][c.y]&&c.x-1>=0) {vis[c.x-1][c.y]=vis[c.x][c.y]+1; Temp.x=c.x-1, temp.y=c.y;        Q.push (temp); }        if(str[c.x][c.y+1]!='#'&&!vis[c.x][c.y+1]&&c.y+1<m) {vis[c.x][c.y+1]=vis[c.x][c.y]+1; Temp.x=c.x,temp.y=c.y+1;        Q.push (temp); }        if(str[c.x][c.y-1]!='#'&&!vis[c.x][c.y-1]&&c.y-1>=0) {vis[c.x][c.y-1]=vis[c.x][c.y]+1; Temp.x=c.x,temp.y=c.y-1;        Q.push (temp); }    }    if(Vis[b.x][b.y]) {returnVIS[B.X][B.Y]; }    return-1;}intMain () {#ifdef cdzsc_offline freopen ("In.txt","R", stdin);#endif    intT,i,j,t1,t2,flag,flag1,flag2;    Node a,b,c; scanf ("%d",&t);  while(t--) {memset (str,0,sizeof(str)); scanf ("%d%d",&n,&m);  for(i=0; i<n; i++) {scanf ("%s", Str[i]); }         for(i=0; i<n; i++)        {             for(j=0; j<m; J + +)            {                if(str[i][j]=='S') {a.x=i,a.y=J; }                if(str[i][j]=='K') {b.x=i,b.y=J; }                if(str[i][j]=='B') {c.x=i,c.y=J; } }} T1=BFS (A, b); T2=BFS (B,C); if(t1==-1|| t2==-1) {printf ("-1\n"); }        Else{printf ("%d\n", t1+T2); }    }    return 0;}

ZSC freshman race Marsh leap fish have already seen through everything

Related Article

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.