Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
A dip in the milk market has forced the cows to move to the city. The only employment available is in the venerable field of taxi-driving. Help the cows learn their way around the city.
Given a city map with E (1 <= E <= 40) east/west street locations and N (1 <= N <= 30) north/south street locations, 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 '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 are available, your program should output the shortest route. The shortest route is guaranteed to exist and be 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 are shown as '.'s. Here is a typical map:
The taxi should go east, north, west, north, east two blocks, and so on. See the output format and sample solution below for its complete route.
Given a city map with E (1 <= E <= 40) east/west street locations and N (1 <= N <= 30) north/south street locations, 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 '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 are available, your program should output the shortest route. The shortest route is guaranteed to exist and be 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 are shown as '.'s. Here is a typical map:
+-+-+.+-+-+ |...|.....| +-+.+-+-+-+ ..|.......| S-+-+-+.E-+
The taxi should go east, north, west, north, east two blocks, and so on. See the output format and sample solution below for its complete route.
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 be clear from the example.
* 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 be clear from the example.
Output
* Lines 1..?: Each line contains a direction letter and a number of blocks to travel in that direction.
Sample Input
3 6 +-+-+.+-+-+ |...|.....| +-+.+-+-+-+ ..|.......| S-+-+-+.E-+
Sample Output
E 1 N 1 W 1 N 1 E 2 S 1 E 3 S 1 W 1
一开始把题意理解复杂了,貌似是一个+就跟着一个-才对,我以为随便多少个都行呢,而且一开始数组开小了,光哇,呵呵了。。。
代码:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<vector> #include<algorithm> #include<set> #include<map> #include<cctype> #include<queue> #define INF 9999999 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef unsigned long long llu; typedef long long ll; const int maxd=200+10; char maze[maxd][maxd]; int dx[]= {0,0,1,-1}; int dy[]= {1,-1,0,0}; char dic[]= {'E','W','S','N'}; int n,e,stx,sty,enx,eny; typedef struct info { int x,y,d; }; info pre[maxd][maxd]; int vis[maxd][maxd]; void print(info tmp) { vector<char> ans; ans.clear(); info tmp2=pre[tmp.x][tmp.y]; ans.push_back(dic[tmp.d]); while(tmp2.d!=-1) { ans.push_back(dic[tmp2.d]); tmp2=pre[tmp2.x][tmp2.y]; } for(int i=ans.size()-1; i>=0;) { int cnt=1; while(i && ans[i]==ans[i-1]) ++cnt,--i; cout<<ans[i]<<' '<<cnt<<endl; --i; } } void bfs(int x,int y) { queue<info> q; q.push( {x,y,-1}); vis[x][y]=1; while(!q.empty()) { info now=q.front(); q.pop(); if(now.x==enx && now.y==eny) { print( {now.x,now.y,now.d}); return; } for(int i=0; i<4; ++i) { int xx=now.x+dx[i]; int yy=now.y+dy[i]; int nx=now.x+dx[i]*2; int ny=now.y+dy[i]*2; if((nx>=0) && (ny>=0) && (nx<2*n-1) && (ny<2*e-1) &&(maze[nx][ny]=='+' || maze[nx][ny]=='E') && (vis[nx][ny]==0) && (maze[xx][yy]=='|' || maze[xx][yy]=='-')) { vis[nx][ny]=1; pre[nx][ny]=now; q.push( {nx,ny,i}); } } } } int main() { freopen("1.txt","r",stdin); while(cin>>n>>e) { mem(vis,0); for(int i=0; i<2*n-1; ++i) { for(int j=0; j<2*e-1; ++j) { cin>>maze[i][j]; if(maze[i][j]=='S') stx=i,sty=j; else if(maze[i][j]=='E') enx=i,eny=j; } } bfs(stx,sty); } return 0; }