不知道哪里错了,一开始忘记条件说超过10就输出-1,结果老是超时
#include "iostream" #include "algorithm" #define N 10000 using namespace std; int rea[4][2]={{0,1},{-1,0},{1,0},{0,-1}}; struct{ int x,y; }s,e; int w,h,step,map[25][25],MIN; int min(int a,int b){return a>b?b:a;} void dfs(int x,int y){ int i,j,k; for(i=0;i<4;i++){ for(j=1;j<=20;j++){ int newtemx=x+j*rea[i][0]; int newtemy=y+j*rea[i][1]; if(step>MIN||step>9)return; if(map[newtemx][newtemy]==3){MIN=min(MIN,step);/*for(k=1;k<=5;k++)cout<<map[1][k]<<' ';cout<<x<<" **** "<<y<<' '<<step<<endl;*/} if(map[newtemx][newtemy]==1&&j==1)goto l1; if(newtemx>h||newtemy>w||newtemx<0||newtemy<0)goto l1; if(map[newtemx][newtemy]==1){ map[newtemx][newtemy]=0;step++; //for(k=1;k<=5;k++)cout<<map[1][k]<<' ';cout<<endl;cout<<newtemx-rea[i][0]<<" "<<newtemy-rea[i][1]<<endl;system("pause"); dfs(newtemx-rea[i][0],newtemy-rea[i][1]); map[newtemx][newtemy]=1;step--; goto l1; } } l1:; } l2:; } int main(){ int i,j; while(cin>>w>>h){ if(w==0&&h==0)break; MIN=N; step=0; for(i=1;i<=h;i++){ for(j=1;j<=w;j++){ cin>>map[i][j]; if(map[i][j]==2){s.x=i;s.y=j;} if(map[i][j]==3){e.x=i;e.y=j;} } } map[s.x][s.y]=0; dfs(s.x,s.y); if(MIN==N)cout<<-1<<endl; else cout<<MIN+1<<endl; } }