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 |
Re:那位大牛看看我的为什么WA?In Reply To:那位大牛看看我的为什么WA? Posted by:acmzhy at 2012-05-08 15:54:10 #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <stack> using namespace std; int step[25][25]; bool flag[25][25]; int dir[4][2]={1,0,-1,0,0,1,0,-1}; struct ln{ int x; int y; }; stack<ln>stk; queue<ln>q; int n,m,l,k; void UFset(){ for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ step[i][j]=-1; flag[i][j]=1; } } void bfs(){ ln a; int headx,heady,nowx,nowy,nexx,nexy; while(!q.empty()){ headx=q.front().x; heady=q.front().y; q.pop(); bool find=0; for(int i=0;i<4;i++){ nexx=headx+dir[i][0]; nexy=heady+dir[i][1]; if(flag[nexx][nexy]==0 || step[nexx][nexy]!=-1 || nexx<0 || nexx>n || nexy<1 || nexy>m) continue; else{ find=1; a.x=nexx; a.y=nexy; step[nexx][nexy]=step[headx][heady]+1; q.push(a); } } if(find&&!stk.empty()){ nowx=stk.top().x; nowy=stk.top().y; flag[nowx][nowy]=1; stk.pop(); } if(step[1][1]!=-1) return ; } return ; } int main(){ int tet=1; ln b; int x,y; while(cin>>n>>m>>l){ if(n==0&&m==0&&l==0) break; UFset(); cin>>x>>y; b.x=x; b.y=y; q.push(b); step[x][y]=0; flag[x][y]=0; for(int i=1;i<l;i++){ cin>>x>>y; b.x=x; b.y=y; flag[x][y]=0; stk.push(b); } cin>>k; while(k--){ cin>>x>>y; step[x][y]=-2; flag[x][y]=0; } bfs(); printf("Case %d: %d\n",tet++,step[1][1]); while(!stk.empty()){ stk.pop(); } while(!q.empty()){ q.pop(); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator