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<cstdlib>//注意w是列,h是行 #include<algorithm> #include<cstdio> #include<iostream> #include<cstring> using namespace std; int dx[4]={0,-1,1,0},dy[4]={-1,0,0,1},w,h,a[22][22],sx,sy,ex,ey,minn; void dfs(int x,int y,int step) { if(step>=10)return; for(int i=0;i<4;i++) { int xx=x+dx[i]; int yy=y+dy[i]; if(a[xx][yy]==1)continue; while(a[xx][yy]==0) { xx+=dx[i]; yy+=dy[i]; } if(xx>=0&&xx<h&&yy>=0&&yy<w) { if(a[xx][yy]) a[xx][yy]=0; dfs(xx-dx[i],yy-dy[i],step+1);//遇障碍物后退 a[xx][yy]=1; } if(xx==ex&&yy==ey) { if(step+1<minn)//step+1 minn=step+1; } } } int main() { while(scanf("%d%d",&w,&h)) { if(w==0&&h==0)return 0; minn=0x3f3f3f3f; memset(a,0,sizeof(a)); for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { scanf("%d",&a[i][j]); if(a[i][j]==2) { sx=i; sy=j; a[sx][sy]=0; } if(a[i][j]==3) { ex=i; ey=j; } } } dfs(sx,sy,0); if(minn<0x3f3f3f3f) printf("%d\n",minn); else printf("-1\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator