lanqiao OJ 641 迷宫

1.迷宫 - 蓝桥云课 (lanqiao.cn)

用dfs进行挨个搜索;每一个点都搜索一下看看能不能走出去

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std ;
const int N = 20 ;
string g[N] ;
bool v[N][N] ;
int ans = 0 ;
int d[4][2] = {{-1,0},{0,1},{1,0},{0,-1}} ;
bool dfs(int x, int y){
    if(x >= 10 || y >= 10 || x < 0 || y < 0){//走出了迷宫,停止
        return  true;
    }
    
    if(v[x][y]) return false ;//走过这个点了,循环了  出不去
    v[x][y] = true ;//标记这个点现在走过了
    if(g[x][y] == 'U') {//往上
        int tx = x +d[0][0] , ty = y + d[0][1] ;
        return dfs(tx,ty);
    }
    if(g[x][y] == 'R'){//往右
        int tx = x +d[1][0] , ty = y + d[1][1] ;
        return dfs(tx,ty);
    } 
    if(g[x][y] == 'D'){//往下
        int tx = x +d[2][0] , ty = y + d[2][1] ;
        return dfs(tx,ty);
    } 
    if(g[x][y] == 'L'){//往左
        int tx = x +d[3][0] , ty = y + d[3][1] ;
        return dfs(tx,ty);
    } 
}


int main(){
    
    for(int i = 0 ; i < 10 ; i ++) cin >> g[i] ;
    for(int i = 0 ; i < 10 ; i ++){
        for(int j = 0 ; j < 10 ; j ++){
            memset(v,0,sizeof v) ;//进行好几个位置的遍历  需要清空数组
            if(dfs(i,j)) ans ++;
        }
    }
    //cout << ans << endl ;
    cout << 31 ; 
}
/*
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值