UVA 784 - Maze Exploration(简单DFS)

题目链接

网速真不好啊,题目挺简单的,变量名打错WA了一次。

 1 #include <stdio.h>

 2 #include <string.h>

 3 char p[101][101];

 4 int a[5] = {0,0,1,-1};

 5 int b[5] = {1,-1,0,0};

 6 int num[101],len;

 7 void dfs(int x,int y)

 8 {

 9     int i;

10     if(p[x][y] == 'X'||p[x][y] == '#')

11     return ;

12     else

13     p[x][y] = '#';

14     for(i = 0;i <= 3;i ++)

15     {

16         if(x+a[i] >= 0&&x+a[i] <= len&& y+b[i]>=0&&y+b[i]<= num[i]-1)

17         {

18             dfs(x+a[i],y+b[i]);

19         }

20     }

21     return ;

22 }

23 int main()

24 {

25    int i,j,k,t,z;

26    scanf("%d%*c",&t);

27    while(t--)

28    {

29        memset(num,0,sizeof(num));

30        z = 1;

31        for(i = 0;;i ++)

32        {

33            gets(p[i]);

34            num[i] = strlen(p[i]);

35            if(strcmp("_____",p[i])==0)

36            break;

37        }

38        len = i;

39        for(i = 0;i <= len&&z;i ++)

40        for(j = 0;j <= num[i]-1;j ++)

41        {

42            if(p[i][j] == '*')

43           {

44               z = 0;

45               break;

46           }

47        }

48        dfs(i,j);

49        for(i = 0;i <= len;i ++)

50        printf("%s\n",p[i]);

51    }

52    return 0;

53 }

 

你可能感兴趣的:(exp)