蓝桥杯-省赛-2017-第4-题-迷宫
目录
蓝桥杯 省赛 2017 第4 题 迷宫
static String res1[] = {
"UDDLUULRUL",
"UURLLLRRRU",
"RRUURLDLRD",
"RUDDDDUUUU",
"URUDLLRRUU",
"DURLRLDLRL",
"ULLURLLRDU",
"RDLULLRDDD",
"UUDDUDUDLL",
"ULRDLUURRR"
};
static int count = 0;
static char res2 [][] = new char[10][10];
static char res3[][] = new char [10][10];
public static void main(String []args) {
for(int i = 0;i<10;i++) {
res2[i] = res1[i].toCharArray();
}
for(int i = 0;i<10;i++) {
for(int j = 0;j<10;j++) {
res3= new char [10][10];//每次都置空一下
cc(i,j);
}
}
System.out.println(count);
}
static int cc(int i,int j) {
if(i<0||j<0||i>9||j>9) {
count++;
return 1;
}
if(res3[i][j]==1) {//又走到上次那个位置了
return 0;
}
res3[i][j]=1;//标记位置,防止一直循环
if(res2[i][j]== 'L') {
cc(i,j-1);
}
else if(res2[i][j]== 'R') {
cc(i,j+1);
}
else if(res2[i][j]== 'U') {
cc(i-1,j);
}
else if(res2[i][j]== 'D') {
cc(i+1,j);
}
return 0;
}
每个人都在不同的房间,100个人在100个不同的房间,把每个人的都带入函数递归,看看能不能走出去,如果能超出范围证明能够走出去,再创建一个二维数组,如果再次走到上次的位置证明走不出去了。