UVa784 Maze Exploration


// 题意:输入一个迷宫,从*开始遍历,把可达点标记为字符#

注意迷宫边界不规则,要用strlen判断。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 100 + 5;
char maze[maxn][maxn];

int dr[]={0, 0, -1, 1};
int dc[]={1, -1, 0, 0};

int R;

void dfs(int r, int c)
{
    if(r<0 || r>=R || c<0 || c >=strlen(maze[r])) return;
    if(maze[r][c] == 'X' || maze[r][c] == '#') return;
    maze[r][c]='#';
    for(int i=0;i<4;i++)
    {
        int nr=r+dr[i];
        int nc=c+dc[i];
        dfs(nr, nc);
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("./uva784.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T); gets(maze[0]);
    while(T--) {
        R=0;
        while(1) {
            gets(maze[R]);
            if(maze[R][0]=='_')
                break;
            R++;
        }
        int j=0;
        while(j<R)
        {
            char *p;
            if((p=strchr(maze[j], '*'))!=0)
            {
                int r, c;
                c=p-maze[j];
                r=j;
                dfs(r, c);
            }
            j++;
        }
        
        j=0;
        while(j<=R)
            printf("%s\n", maze[j++]);

    }

    return 0;
}

你可能感兴趣的:(exp)