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:32ms 1a 代码In Reply To:47ms 1a 代码 Posted by:whus2010301500230 at 2012-04-01 17:44:01 //本来考虑用%[^\n]读带空格的字符串,但百思不得其解地实践不了,VS2008太破了…… #include <iostream> using namespace std; const int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; int main() { int m,n,i,j,sx,sy,k,xx,yy,a,b,mi,x,y,move; char maze[22][22]; int step[450][5]; while(1) { cin>>m>>n; memset(maze,-1,sizeof(maze)); sx=m; if (m || n) { for (i=1;i<=n;i++) { //cin>>ws>>maze[i]+1; //scanf("%[^\n]",maze[i]+1); /*if (gx==m) for (j=1;j<=m;j++) if(maze[i][j]=='3') { gx=i; gy=j; break; }*/ for (j=1;j<=m;j++) { cin>>maze[i]+j; if (maze[i][j]=='2') { sx=i; sy=j; maze[i][j]='0'; } } } k=0; step[k][0]=sx; step[k][1]=sy; step[k][2]=4; step[k][3]=step[k][4]=-1; i=0; mi=10; x=sx; y=sy; while(k>=0) { for (;i<4;i++) { a=dir[i][0]; b=dir[i][1]; xx=x; yy=y; move=-1; while(maze[xx][yy]=='0') { xx+=a; yy+=b; move++; } if (maze[xx][yy]=='3') mi=k; else if (maze[xx][yy]=='1' && move) if (k+1<mi) { k++; step[k][2]=i; step[k][1]=y; step[k][0]=x; step[k][4]=yy; step[k][3]=xx; maze[xx][yy]='0'; y=yy-b; x=xx-a; i=0; break; } } if (i==4) { x=step[k][0]; y=step[k][1]; i=step[k][2]+1; maze[step[k][3]][step[k][4]]='1'; k--; } } if (mi==10) cout<<"-1"<<endl; else cout<<mi+1<<endl; } else break; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator