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> using namespace std; int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; struct node { int x,y; }; int n,m,sx,sy,xx,yy; int value[101][101]; char map[101][101]; int bfs() { queue<node>q; while(!q.empty()) q.pop(); bool flag[101][101]; int i; node temp; memset(value,0,sizeof(value)); memset(flag,true,sizeof(flag)); temp.x=sx;temp.y=sy;value[sx][sy]=0;flag[sx][sy]=false; q.push(temp); while(!q.empty()&&value[xx][yy]==0) { node loop; loop.x=q.front().x;loop.y=q.front().y; for(i=0;i<4;i++) { temp.x=loop.x+dir[i][0]; temp.y=loop.y+dir[i][1]; while(temp.x>=0&&temp.y>=0&&temp.x<=n+1&& temp.y<=m+1&&flag[temp.x][temp.y]==true&&map[temp.x][temp.y]==' ') { flag[temp.x][temp.y]=false; q.push(temp); value[temp.x][temp.y]=value[loop.x][loop.y]+1; temp.x+=dir[i][0];temp.y+=dir[i][1]; } } q.pop(); } return value[xx][yy]; } int main() { int i,j,k=0,k1,ans,m1,m2; while(cin>>m>>n) { getchar(); k1=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%c",&map[i][j]); } getchar(); } for(i=0;i<=n+1;i++) { map[i][0]=' '; map[i][m+1]=' '; } for(j=1;j<=m;j++) { map[0][j]=' '; map[n+1][j]=' '; } k++; printf("Board #%d:\n",k); while(cin>>sy>>sx>>yy>>xx) { if(sx==0&&sy==0&&xx==0&&yy==0) break; m1=0,m2=0; if(map[sx][sy]=='X') { map[sx][sy]=' '; m1=1; } if(map[xx][yy]=='X') { map[xx][yy]=' ';m2=1; } k1++; if(abs(sx-xx)>=2||abs(sy-yy)>=2) { ans=bfs(); if(ans!=0) printf("Pair %d: %d segments.\n",k1,ans); else printf("Pair %d: impossible.\n",k1); } else printf("Pair %d: impossible.\n",k1); if(m1==1) map[sx][sy]='X'; if(m2==1) map[xx][yy]='X'; } cout<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator