题目
X星球的一处迷宫游乐场建在某个小山坡上。
它是由10x10相互连通的小房间组成的。
房间的地板上写着一个很大的字母。
我们假设玩家是面朝上坡的方向站立,则:
L表示走到左边的房间,
R表示走到右边的房间,
U表示走到上坡方向的房间,
D表示走到下坡方向的房间。
X星球的居民有点懒,不愿意费力思考。
他们更喜欢玩运气类的游戏。这个游戏也是如此!
开始的时候,直升机把100名玩家放入一个个小房间内。
玩家一定要按照地上的字母移动。
迷宫地图如下:
UDDLUULRUL
UURLLLRRRU
RRUURLDLRD
RUDDDDUUUU
URUDLLRRUU
DURLRLDLRL
ULLURLLRDU
RDLULLRDDD
UUDDUDUDLL
ULRDLUURRR
请你计算一下,最后,有多少玩家会走出迷宫?
而不是在里边兜圈子。
请提交该整数,表示走出迷宫的玩家数目,不要填写任何多余的内容。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char n[10][10];
int res = 0;
bool is[10][10];
void search(int x, int y, bool step[10][10]){
if(x < 0 || x > 9 || y < 0 || y > 9) {
res += 1;
return ;
}
if(n[x][y] == 'U'){
if(step[x-1][y] == 1) return;
step[x-1][y] = 1;
search(x-1, y, step);
}
else if(n[x][y] == 'D'){
if(step[x+1][y] == 1) return;
step[x+1][y] = 1;
search(x+1, y, step);
}
else if(n[x][y] == 'R'){
if(step[x][y+1] == 1) return;
step[x][y+1] = 1;
search(x, y+1, step);
}
else{
if(step[x][y-1] == 1) return;
step[x][y-1] = 1;
search(x, y-1, step);
}
}
void dfs(int x, int y){
if(x == 9 && y == 9){
bool step[10][10];
memset(step, 0, sizeof(step));
step[9][9] = 1;
search(x, y, step);
return;
}
if(y == 9){
bool step[10][10];
memset(step, 0, sizeof(step));
step[x][y] = 1;
search(x, y, step);
dfs(x+1, 0);
}
else{
bool step[10][10];
memset(step, 0, sizeof(step));
step[x][y] = 1;
search(x, y, step);
dfs(x, y+1);
}
}
int main(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10 ;j++){
cin >> n[i][j];
}
}
dfs(0,0);
cout << res;
return 0;
}
答案:31