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 |
代码!#include <stdio.h> #define M 1024 typedef struct queue { int x,y,pre; }q; q p[M]; typedef struct stack { int x,y; }s; s sp[M]; int maze[5][5]; int r,c; int head,tail; int dx[4]={1,0,0,-1}; int dy[4]={0,1,-1,0}; void print() { int i,j; for(i=0;i<5;i++) { for(j=0;j<5;j++) printf("%d",maze[i][j]); printf("\n"); } } void init() { int i,j; for(i=0;i<5;i++) { for(j=0;j<5;j++) scanf("%d",&maze[i][j]); } // print(); } void path() { int t=tail,s=0,i; while(p[t].pre!=-1) { sp[s].x=p[t].x; sp[s].y=p[t].y; t=p[t].pre; s++; } for(i=s;i>=0;i--) printf("(%d, %d)\n",sp[i].x,sp[i].y); } void bfs(int x,int y) { int i,tempx,tempy; head=0; tail=0; p[head].x=x; p[head].y=y; p[head++].pre=-1; maze[x][y]=1; while(tail<=head) { if(p[tail].x==4&&p[tail].y==4) break; else { for(i=0;i<4;i++) { tempx=p[tail].x+dx[i]; tempy=p[tail].y+dy[i]; if(tempx>=0&&tempx<=4&&tempy>=0&&tempy<=4&&!maze[tempx][tempy]) { maze[tempx][tempy]=1; p[head].x=tempx; p[head].y=tempy; p[head++].pre=tail; } } tail++; } } path(); } int main() { init(); bfs(0,0); return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator