用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
*/