Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

就不能用递归做的???我知道这个递归有问题,但是不知道怎么改这个递归

Posted by zhengmoa at 2011-08-07 13:38:34 on Problem 2251
# include <stdio.h>
# include <string.h>
# define MAXN 31
int dr[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int ei,ej,ek,si,sj,sk,count;
int mark[MAXN][MAXN][MAXN];
char map[MAXN][MAXN][MAXN];
int s,r,c,flag;
void bfs(int z,int x,int y)
{
    int zz,xx,yy,i;
    for (i=0;i<=5;i++)
    {
        zz=z+dr[i][0];
        xx=x+dr[i][1];
        yy=y+dr[i][2];
        if (zz==ei && xx==ej && yy==ek)
        {
            flag=1;
            count++;
            printf ("Escaped in %d minute(s).\n",count);
            return;
        }
        if (zz<0 || zz>s-1 || xx<0 || xx>r-1 || yy<0 || yy>c-1)continue;
        if (map[zz][xx][yy]=='.' && mark[zz][xx][yy]==0)
        {
            count++;
            mark[zz][xx][yy]=1;
            bfs(zz,xx,yy);
        }
    }
}
int main()
{
    int i,j,k;
    while (scanf ("%d%d%d",&s,&r,&c))
    {
        if (r==0 && c==0 && s==0)break;
        memset(mark,0,sizeof(mark));
        count=0;flag=0;
        for (i=0;i<=s-1;i++)
         for (j=0;j<=r-1;j++)
         {
            scanf ("%s",map[i][j]);
         }
         for (i=0;i<=s-1;i++)
          for (j=0;j<=r-1;j++)
           for (k=0;k<=c-1;k++)
           {
                if (map[i][j][k]=='#')
                mark[i][j][k]=1;
                if (map[i][j][k]=='S')
                {
                    si=i;
                    sj=j;
                    sk=k;
                    mark[i][j][k]=1;
                }
                if (map[i][j][k]=='E')
                {
                    ei=i;
                    ej=j;
                    ek=k;
                }
           }
           bfs(si,sj,sk);
           if (flag==0)
           printf ("Trapped!\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator