Description
A dip in the milk market have forced the cows to move to the city. The only employment available are in the venerable field of Taxi-driving. Help the cows learn their to around the city.
Given a city map with E (1 <= e <=) east/west Street Locations and N (1 <= N <=) North/south Street Loca tions, create instructions for a taxi driver to navigate from the start of his route (marked ' S ') to the end (marked ' E '). Each instruction is a direction (one of the ' N ', ' E ', ' S ', or ' W ') followed by a space followed by an integer that tells how Many blocks to drive in that direction. If multiple routes is available, your program should output the shortest route. The shortest route is guaranteed to exist and are unique.
The map is depicted as a grid of ' + ' s that represent intersections and a set of roads depicted as '-' and ' | '. Buildings and other obstacles is shown as '. ' S. Here is a typical map: cows must travel to the city due to the demand of the milk market, but the only available means of transport is taxis. Teach cows how to fight in cities. Give a map of the city, the east-West Block E (1≤E≤40), the north-South Block N (1≤n≤30). Make a driving guide to the taxi driver and tell him how to get from the starting point (denoted by s) to the end point (denoted by e). Each entry is divided into two parts, the first part is the direction (one of N,e,s,w), and the second is an integer that indicates a few intersections along this direction. If there are more than one route, you should give the shortest. Data guarantees that the shortest path exists and is unique. The "+" in the map represents the intersection, and the road is denoted by "I" and "one". Construction and other facilities. " Said Here is a map:
+-+-+.+-+-+
|...| .....|
+-+.+-+-+-+
..| .......|
s-+-+-+. e-+
The taxi should go east, north, west, north, east, and blocks. See the output format and sample solution below for it complete route. Taxis can be run along the east, north, west, north, east two intersections, and so on. The example is given by the sample
Input
* Line 1:two space-separated integers, N and E.
* Lines 2..2*n:these Lines each contain 2*e-1 characters and encode the map of the street. Every other input line gives the data for the east/west streets; The remaining lines show the North/south streets. The format should is clear from the example.
Line 1th: two integers n and e separated by a space.
2nd to 2nd N rows: Each line has 2e-i characters representing the map.
Output
* Lines 1..: Each line contains a direction letters and a number of blocks to tour in that direction. Each row has a letter representing the direction and a Numbers at several intersections indicate.
Sample Input
3 6+-+-+.+-+-+|...| .....| +-+.+-+-+-+..| .......| s-+-+-+. e-+
Sample Output
E 1N 1W 1N 1E 2S 1E 3S 1W 1
Direct BFS, then record the path on the line ...
#include <queue>#include<cstdio>#include<iostream>using namespacestd;structna{intx, y;};Const intfx[4]={0,1,0,-1},fy[4]={1,0,-1,0};intn,m;Charmap[Bayi][Bayi];intcm[Bayi][Bayi];CharC;queue<na>Q;voidpriintXinty) { intk,p; for(;;) {k=cm[x][y];p =0; if(k==0) printf ("E");Else if(k==1) printf ("S");Else if(k==2) printf ("W");Elseprintf ("N"); while(cm[x][y]==k) {x+=Fx[k]; Y+=Fy[k]; if(map[x][y]=='+') p++; } if(map[x][y]=='E') {printf ("%d", p+1); return; } printf ("%d\n", p); }}intMain () {scanf ("%d%d",&n,&m); N=n*2-1; m=m*2-1; for(intI=0; i<n;i++) for(intj=0; j<m;j++) {C=GetChar (); while(c=='\ n') c=GetChar (); MAP[I][J]=B; if(c=='E') {na tmp; Tmp.x=i;tmp.y=J; Q.push (TMP); } Cm[i][j]=-1; } while(!Q.empty ()) {na k=Q.front (); Q.pop (); for(intI=0;i<4; i++) {na now=K; now.x+=fx[i];now.y+=Fy[i]; if(now.x<0|| now.y<0|| now.x>=n| | NOW.Y>=M)Continue; if(map[now.x][now.y]=='.')Continue; if(cm[now.x][now.y]==-1) {CM[NOW.X][NOW.Y]=i>1? I-2: i+2; if(map[now.x][now.y]=='S') {pri (NOW.X,NOW.Y); return 0; } q.push (now); } } }}
Bzoj:1687;poj 2434:[usaco2005 open]navigating the city transportation