Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
一个条件忘写,WA,过了,贴代码:#include<iostream> #include<queue> #include<cstring> #include<cstdio> using namespace std; const int maxn = 400; char map[maxn][maxn]; int vis[maxn][maxn]; int dir[4][2] = {-1,0,1,0,0,1,0,-1}; //分别代表北,南,东,西 struct point { int x,y; }; void turn(int m,int n,int start) { int cnt,cnt1,i; queue<point> que; point p; p.x = 0; p.y = start-1; que.push(p); cnt = 0; cnt1 = 0; int pos1,pos2; int a[maxn],b[maxn]; int cnt3; cnt3 = 0; while(!que.empty()) { p = que.front(); a[cnt3] = p.x; b[cnt3] = p.y; cnt3++; que.pop(); if(p.x < 0 || p.y < 0 || p.x > m-1 || p.y > n-1) { cout<<cnt<<" step(s) to exit"<<endl; break; } if(map[p.x][p.y] == 'W' && vis[p.x][p.y] == 0) { if(vis[p.x][p.y] == 0) { //cout<<"W:"<<p.x<<" "<<p.y<<endl; vis[p.x][p.y] = 1; p.x = p.x + dir[3][0]; p.y = p.y + dir[3][1]; que.push(p); cnt++; } else { p.x = p.x + dir[3][0]; p.y = p.y + dir[3][1]; } } else if(map[p.x][p.y] == 'S' && vis[p.x][p.y] == 0) { if(vis[p.x][p.y] == 0) { //cout<<"S:"<<p.x<<" "<<p.y<<endl; vis[p.x][p.y] = 1; p.x = p.x + dir[1][0]; p.y = p.y + dir[1][1]; que.push(p); cnt++; } else { p.x = p.x + dir[3][0]; p.y = p.y + dir[3][1]; } } else if(map[p.x][p.y] == 'E' && vis[p.x][p.y] == 0) { if(vis[p.x][p.y] == 0) { //cout<<"E:"<<p.x<<" "<<p.y<<endl; vis[p.x][p.y] = 1; p.x = p.x + dir[2][0]; p.y = p.y + dir[2][1]; que.push(p); cnt++; } else { p.x = p.x + dir[3][0]; p.y = p.y + dir[3][1]; } } else if(map[p.x][p.y] == 'N' && vis[p.x][p.y] == 0) { if(vis[p.x][p.y] == 0) { //cout<<"N:"<<p.x<<" "<<p.y<<endl; vis[p.x][p.y] = 1; p.x = p.x + dir[0][0]; p.y = p.y + dir[0][1]; que.push(p); cnt++; } else { p.x = p.x + dir[3][0]; p.y = p.y + dir[3][1]; } } } //cout<<"cnt:"<<cnt<<endl; //cout<<p.x<<" "<<p.y<<endl; int pos3; for(i=0;i<cnt3;i++) { if(p.x == a[i] && p.y == b[i]) { pos3 = i; break; } } if(cnt-pos3 != 0) cout<<pos3<<" step(s) before a loop of "<<cnt-pos3<<" step(s)"<<endl; } int main() { int m,n,k,i,j; //freopen("111","r",stdin); while(cin>>m>>n>>k && !(m == 0 && n == 0 && k == 0)) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>map[i][j]; } } memset(vis,0,sizeof(vis)); turn(m,n,k); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator