UVa 784 - Maze Exploration

DFS ~

代码如下:

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;
char a[30+5][80+5];
int vis[30+5][80+5];
void DFS(int x,int y)
{
    if(a[x][y]=='X'||vis[x][y]==1)
        return ;
    if(a[x][y]=='*'||a[x][y]==' ')
        a[x][y]='#';
    vis[x][y]=1;
    DFS(x-1,y);
    DFS(x,y-1);
    DFS(x,y+1);
    DFS(x+1,y);
    DFS(x-1,y-1);
    DFS(x-1,y+1);
    DFS(x+1,y+1);
    DFS(x+1,y-1);
}
int main()
{
#ifdef test
    freopen("sample.txt","r",stdin);
#endif
    int num,len;
    scanf("%d",&num);
    getchar();
    while(num--)
    {
        int cct=0,i,j;
        memset(a,0,sizeof(a));
        memset(vis,0,sizeof(vis));
        while(gets(a[cct])!=NULL)
        {
            cct++;
            if(a[cct-1][0]=='_')break;
        }
        for(i=2; i<cct; i++)
        {
            len=strlen(a[i]);
            for(j=0; j<len; j++)
            if(a[i][j]=='*')
                DFS(i,j);
        }
        for(int i=0;i<cct;i++)
            puts(a[i]);
    }
    return 0;
}


你可能感兴趣的:(UVa 784 - Maze Exploration)