UVa 784 - Maze Exploration 搜索专题

FILE784 - Maze Exploration7758
41.30%
2328
84.06%

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=725


题目类型: 搜索


样例输入:

2
XXXXXXXXX
X   X   X
X *     X
X   X   X
XXXXXXXXX
X   X
X   X
X   X
XXXXX
_____
XXXXX
X   X
X * X
X   X
XXXXX
_____

样例输出:

XXXXXXXXX
X###X###X
X#######X
X###X###X
XXXXXXXXX
X   X
X   X
X   X
XXXXX
_____
XXXXX
X###X
X###X
X###X
XXXXX
_____


分析:

又是一道搜索入门的简单题,不解释。 天天刷水题,今天特别多。今天的度假很happy,在这样的大热天,就应该水水降降温。


代码1: DFS

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;

char map[35][100];
int vis[35][100], row;
int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};

inline bool isWall(char ch){
    if(isprint(ch) && ch!=' ' && ch!='*' && ch!='-') return true;
    return false;
}

void dfs(int x,int y){
    for(int i=0; i<4; ++i){
        int dx=x+dir[i][0], dy=y+dir[i][1];
        if(dx>=0 && dx<row && dy>=0 && dy<strlen(map[dx]) && !isWall(map[dx][dy]) && map[dx][dy]!='#' && !vis[dx][dy]){
            vis[dx][dy] = true;
            map[dx][dy] = '#';
            dfs(dx, dy);
        }
    }
}

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif  
    int T, start_x, start_y;
    scanf("%d%*c", &T);
    while(T--){
        row=0;
        bool isFind = false;
        while(gets(map[row])){
            if(map[row][0]=='_') break;
            if(!isFind){
                for(int i=0; i<strlen(map[row]); ++i){
                    if(map[row][i]=='*'){
                        start_x = row, start_y = i;
                        isFind = true;
                        break;
                    }
                }
            }
            ++row;
        }
        memset(vis, 0, sizeof(vis));
        dfs(start_x, start_y);
        for(int i=0; i<=row; ++i)
            puts(map[i]);
    }
    return 0;
}


代码2:BFS

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;

char map[35][100];
int vis[35][100], row;
int dir[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}};
struct Node{int x,y; };
Node que[10000];

inline bool isWall(char ch){
    if(isprint(ch) && ch!=' ' && ch!='*' && ch!='-') return true;
    return false;
}

void bfs(int x,int y){
    int front=0, rear=1;
    que[0].x = x, que[0].y = y;

    while(front<rear){
        Node t = que[front++];
        for(int i=0; i<4; ++i){
            int dx=t.x+dir[i][0], dy=t.y+dir[i][1];
            if(dx>=0 && dx<row && dy>=0 && dy<strlen(map[dx]) && !isWall(map[dx][dy]) && map[dx][dy]!='#' && !vis[dx][dy]){
                vis[dx][dy] = true;
                map[dx][dy] = '#';
                Node q;
                q.x = dx, q.y = dy;
                que[rear++] = q;
            }
        }
    }
}

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif  
    int T, start_x, start_y;
    scanf("%d%*c", &T);
    while(T--){
        row=0;
        bool isFind = false;
        while(gets(map[row])){
            if(map[row][0]=='_') break;
            if(!isFind){
                for(int i=0; i<strlen(map[row]); ++i){
                    if(map[row][i]=='*'){
                        start_x = row, start_y = i;
                        isFind = true;
                        break;
                    }
                }
            }
            ++row;
        }
        memset(vis, 0, sizeof(vis));
        bfs(start_x, start_y);
        for(int i=0; i<=row; ++i)
            puts(map[i]);
    }
    return 0;
}



——      生命的意义,在于赋予它意义。 

                   原创  http://blog.csdn.net/shuangde800  , By   D_Double









  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值