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 |
help!有什么特殊情况不?我一直WA啊,谁帮我看看呀 #include<stdio.h> #include<string.h> #define MAXN 105 long hash[MAXN][MAXN][30],n,m,ans,num,dir[4][2]={0,1,0,-1,1,0,-1,0}; char map[MAXN][MAXN]; struct point { long x,y,time,d; }a[1001000]; void bfs() { long x,y,i,begin,end; begin=0; end=num; while(begin<end) { for(i=0;i<4;i++) { x=a[begin].x+dir[i][0]; if(x<0||x>n-1) continue; y=a[begin].y+dir[i][1]; if(y<0||y>m-1) continue; if(map[x][y]=='*') continue; a[end].x=x; a[end].y=y; if(map[x][y]=='.') { a[end].d=a[begin].d; a[end].time=a[begin].time; if(hash[x][y][a[end].d]==-1||a[end].time<hash[x][y][a[end].d]) { hash[x][y][a[end].d]=a[end].time; end++; } } else { if(map[x][y]>='1'&&map[x][y]<='9') { if(a[begin].d>0) { a[end].d=a[begin].d-1; a[end].time=a[begin].time; if(hash[x][y][a[end].d]==-1||a[end].time<hash[x][y][a[end].d]) { hash[x][y][a[end].d]=a[end].time; end++; } } a[end].x=x; a[end].y=y; a[end].d=a[begin].d; a[end].time=a[begin].time+map[x][y]-'0'; if(hash[x][y][a[end].d]==-1||a[end].time<hash[x][y][a[end].d]) { hash[x][y][a[end].d]=a[end].time; end++; } } else { if(map[x][y]=='$'&&a[begin].time<ans) { ans=a[begin].time; } } } } begin++; } } int main() { long i; while (scanf("%s%*c",&map[0])&&map[0][0]!='-') { num=0; m=strlen(map[0]); for(i=0;i<m;i++) { if(map[n][i]=='#') { a[num].d=0; a[num].time=0; a[num].x=n; a[num].y=i; num++; } else { if(map[n][i]>='A'&&map[n][i]<='Z') { a[num].d=map[n][i]-'A'+1; a[num].time=0; a[num].x=n; a[num].y=i; num++; } } } for(n=1;;n++) { gets(map[n]); if(map[n][0]=='\0') break; for(i=0;i<m;i++) { if(map[n][i]=='#') { a[num].d=0; a[num].time=0; a[num].x=n; a[num].y=i; num++; } else { if(map[n][i]>='A'&&map[n][i]<='Z') { a[num].d=map[n][i]-'A'+1; a[num].time=0; a[num].x=n; a[num].y=i; num++; } } } } memset(hash,-1,sizeof(hash)); ans=999999999; bfs(); if(ans==999999999) printf("IMPOSSIBLE\n"); else printf("%ld\n",ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator