Analysis: This problem is actually not difficult, is in the conventional BFS more than a BFS, that is, two BFS only! Use a For loop to find out! Have done a lot of BFS, this problem is similar to those, that is, in the data need to do some of their own work, the string of the map to change to an integer-type map!
Idea: 1. First enter the map, then set up two int type start and end arrays to save the starting and ending points;
At the same time in the string map, if you can walk here, in the corresponding integer map labeled 1, can not go to the place marked as 0;
2. As a result of the title: If the key is not found or found the key but can not give the key to DBZ, the same as unsuccessful, that is, the output "DBZ cheat us";
3. Based on the 2nd, you can set up a tag, in the written for loop, as long as there is a ans[i]==0, then it indicates failure!
4. This depends on your thoughts, you can use the array write queue, you can also use the C + + Standard Template Library write queue, of course I think the latter is more simple!
5. The rest is the details, this much to think about should be no problem!
The code is as follows:
#include <cstdio> #include <iostream> #include <cstring> #include <queue>using namespace std; Char map[520][520];int map[520][520];int n,m,sx[2],sy[2],dx[2],dy[2],ans[2];int xx[4]= {1,0,-1,0};int yy[4]= {0,1,0,- 1};bool vis[520][520];struct node{int x,y,ti;}; BOOL BFs () {for (int ss=0; ss<2; ss++) {memset (vis,false,sizeof (VIS)); Vis[sx[ss]][sy[ss]]=true; queue<node>q; Node now; NOW.X=SX[SS]; NOW.Y=SY[SS]; NOW.TI=ANS[SS]; Q.push (now); while (! Q.empty ()) {node Te=q.front (); Q.pop (); if (Te.x==dx[ss]&&te.y==dy[ss]) {ans[ss]=te.ti; Break cout<< "eee--->" <<ans[ss]<<endl; } for (int i=0; i<4; i++) {int x=xx[i]+te.x; int y=yy[i]+te.y; if (map[x][y]&&!vis[x][y]&&x>=0&&x<n&&y>=0&&y<m) {vis[x][y]=true; Node num; Num.x=x; Num.y=y; num.ti=te.ti+1; Q.push (num); }}} if (!ans[ss]) return false; } return true; int main () {while (cin>>n>>m) {ans[0]=ans[1]=0; GetChar (); memset (map,1,sizeof (map)); for (int i=0, i<n; i++) {for (int j=0; j<m; J + +) {cin>>map[i][j]; if (map[i][j]== '. ') Map[i][j]=1; if (map[i][j]== ' * ') map[i][j]=0; if (map[i][j]== ' S ') {sx[0]=i; Sy[0]=j; } if (map[i][j]== ' X ') {dx[0]=i; Dy[0]=j; Sx[1]=i; Sy[1]=j; } if (map[i][j]== ' E ') {dx[1]=i; Dy[1]=j; }} getchar (); } if (BFS ())) cout<<ans[0]+ans[1]<<endl; else cout<< "DBZ Cheat Us" <<endl; } return 0;}
Description
Ancient extremely wealthy million, this is rich can enemy ball DBZ. Say DBZ has a super Lincoln long, its front in Dujiangyan, and the car tail is in Sichuan, after a few months she finally arrived at our Chengdu Neusoft College, attracted countless people onlookers. can be careless DBZ but do not know the car's key lost in the car where, so rich can enemy ball of his reward 500 trillion (of course this for him is small case ^_^), public reward for key. As the ordinary you of neusoft, I certainly do not want to miss the chance to become Gaofu and mating. If you want to get a bonus, you need to find the key in the quickest way and give it to DBZ.
Input
The test data contains multiple groups.
The first row of each set of test data contains 2 numbers n (1 < = n < =), m (1 < = m < = N), representing the length and width of the vehicle.
Next contains n rows, each m element (where '. ' represents the path that can be passed, ' X ' stands for the location of the key, ' * ' stands for obstructions, ' E ' stands for DBZ lock on the ground, ' S ' stands for your location. Each time you can move up or down.
Output
The output contains only one line, that is, the minimum time to give the key to DBZ, if the key cannot be successfully found or the key cannot be successfully handed to DBZ, the output "DBZ Cheat Us" (no quotation marks).
Sample Input4 4s.* ... X.e ..... 4 4s.*. *x. E ...Sample Output6DBZ Cheat Us
Problem-b DBZ's key.