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:为什么总是超时In Reply To:Re:为什么总是超时 Posted by:Ethanh at 2005-03-28 14:44:07 > 偶也超时啊 > 用的是迪克思略的算法 #include <iostream> #include <vector> #define Max 99999999 using namespace std; typedef struct pos{ short x; short y; }Pos; int main() { vector<Pos> s,t; int r,c,end,Min; char **p; char *flag; int *dist; cin>>r>>c; while(r&&c){ Pos point; p=new char*[r]; for(int i=0;i<r;i++) p[i]=new char[c]; flag=new char[r*c]; dist=new int[r*c]; memset(flag,0,r*c); for(int j=0;j<r;j++){ for(int k=0;k<c;k++){ point.x=j; point.y=k; cin>>p[j][k]; if(p[j][k]=='Y'){ t.push_back(point); flag[j*c+k]=1; dist[j*c+k]=0; } else{ s.push_back(point); dist[j*c+k]=Max; } if(p[j][k]=='T') end=j*c+k; } } Min=0; while(Min<Max&&dist[end]==Max){ Min=Max; Pos d; for(int l=t.size()-1;l>=0;l--){ short x=t[l].x; short y=t[l].y; if(x-1>=0&&!flag[(x-1)*c+y]){ if(p[x-1][y]=='B'&&dist[x*c+y]+2<Min){ Min=dist[x*c+y]+2; d.x=x-1; d.y=y; } else if((p[x-1][y]=='E'||p[x-1][y]=='T')&&dist[x*c+y]+1<Min){ Min=dist[x*c+y]+1; d.x=x-1; d.y=y; } } if(y-1>=0&&!flag[x*c+y-1]){ if(p[x][y-1]=='B'&&dist[x*c+y]+2<Min){ Min=dist[x*c+y]+2; d.x=x; d.y=y-1; } else if((p[x][y-1]=='E'||p[x][y-1]=='T')&&dist[x*c+y]+1<Min){ Min=dist[x*c+y]+1; d.x=x; d.y=y-1; } } if(x+1<r&&!flag[(x+1)*c+y]){ if(p[x+1][y]=='B'&&dist[x*c+y]+2<Min){ Min=dist[x*c+y]+2; d.x=x+1; d.y=y; } else if((p[x+1][y]=='E'||p[x+1][y]=='T')&&dist[x*c+y]+1<Min){ Min=dist[x*c+y]+1; d.x=x+1; d.y=y; } } if(y+1<c&&!flag[x*c+y+1]){ if(p[x][y+1]=='B'&&dist[x*c+y]+2<Min){ Min=dist[x*c+y]+2; d.x=x; d.y=y+1; } else if((p[x][y+1]=='E'||p[x][y+1]=='T')&&dist[x*c+y]+1<Min){ Min=dist[x*c+y]+1; d.x=x; d.y=y+1; } } } if(Min<Max){ t.push_back(d); flag[d.x*c+d.y]=1; dist[d.x*c+d.y]=Min; } } if(dist[end]==Max) cout<<"-1"<<endl; else cout<<dist[end]<<endl; for(int z=0;z<r;z++) delete[] p[z]; delete []p; delete []flag; delete []dist; cin>>r>>c; } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator