The marsh-jumping fish has already seen everything. Time limit:1 Sec Memory limit:128 MB
submit:593 solved:229
[Submit] [Status] [Web Board] 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.
BFS Breadth First Search
CODE:
#include <iostream>#include<cstring>#include<cstdio>using namespacestd;intb[ the][ the];voidBFsintNintMintnum) { for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) { if(b[i][j]==num) { if(b[i][j+1]==0) {b[i][j+1]=num+1; } if(b[i][j-1]==0) {b[i][j-1]=num+1; } if(b[i-1][j]==0) {B[i-1][j]=num+1; } if(b[i+1][j]==0) {B[i+1][j]=num+1; } } } }}intMain () {intN; CIN>>N; while(n--) { Chara[ the][ the]; intn,m; CIN>>n>>m; memset (b,-1,sizeof(b)); intx0,y0,x1,y1; for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {cin>>A[i][j]; if(a[i][j]!='#') {B[i][j]=0; } if(a[i][j]=='S') {B[i][j]=1; } if(a[i][j]=='K') {x0=i; Y0=J; } } } intnum=1; while(b[x0][y0]==0&&num< the) {BFS (n,m,num); Num++; /*for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {C out<<b[i][j]<< ' \ t '; } cout<<endl; } cout<<endl;*/ } if(num> About) {cout<<"-1"<<Endl; Continue; } for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) { if(a[i][j]!='#') {B[i][j]=0; } if(a[i][j]=='K') {B[i][j]=1; } if(a[i][j]=='B') {x0=i; Y0=J; } } } /*for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) { cout<<b[i][j]<< ' \ t '; } cout<<endl; }*/ intnum0=1; while(b[x0][y0]==0&&num0< the) {BFS (N,M,NUM0); NUM0++; ///*for (int i=1;i<=n;i++) //{ //for (int j=1;j<=m;j++)// { //cout<<b[i][j]<< ' \ t '; // } //cout<<endl; //}*/ //cout<<endl; } if(num0> About) {cout<<"-1"<<Endl; Continue; } cout<<num+num0-2<<Endl; }}
The marsh fish has already seen through all the C + +