标签:
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
+-+-+.+-+-+ |...|.....| +-+.+-+-+-+ ..|.......| S-+-+-+.E-+
Input
Output
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; }
标签:
原文地址:http://blog.csdn.net/whoisvip/article/details/45585037