森林里的苯苯熊要乔迁新喜,上次他已经将物品打包完成,并约了朋友来帮忙。接下来他要选定一个搬家的时间,想了很久,就决定在国庆节进行,因为国庆放假朋友们都有时间啦。但是在森林里,从他现在房子到新豪宅,所经之地有山有水,路途曲折,甚至有些道路是不通的。
请你和他一起查看指定的地图,看看从笨笨熊现在的房子到新宅之间,道路是否是畅通的呢?
地图是R行、C列的矩阵,矩阵的每一个格子刚好是一天的行程。
矩阵由“B”、“-”、“#”、“H”四种字符成员组成,其中:
B: 代表苯苯熊现在的房子;
H: 代表笨笨熊新的豪宅;
-: 代表可以通行的道路;
#: 代表无法通过的障碍(高山、大河等);
4 // C的数值,下面是地图。
--##---
B-----H
#---#--
-------输出:
Y //代表道路可达
或
请你和他一起查看指定的地图,看看从笨笨熊现在的房子到新宅之间,道路是否是畅通的呢?
地图是R行、C列的矩阵,矩阵的每一个格子刚好是一天的行程。
矩阵由“B”、“-”、“#”、“H”四种字符成员组成,其中:
B: 代表苯苯熊现在的房子;
H: 代表笨笨熊新的豪宅;
-: 代表可以通行的道路;
#: 代表无法通过的障碍(高山、大河等);
此外,森林里也有交通规则地:在任务位置,只能向“上、下、左、右”四个方向中的其中一个方向行走。
运行时间限制:无限制
内存限制:无限制输入:
4 // R的数值4 // C的数值,下面是地图。
--##---
B-----H
#---#--
-------输出:
Y //代表道路可达
或
N //代表道路不通
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Test1 {
static int[][] DIR={{0,1},{0,-1},{-1,0},{1,0}};
public static class Position{
int x,y;
Position(int ax,int ay){
this.x=ax;
this.y=ay;
}
}
public static class Step{
int count;
Position p;
public Step(Position pp,int ac){
p=pp;
this.count=ac;
}
public Position getNextPosition() {
int c=this.count;
if(c>=0){
this.count--;
return new Position(p.x+DIR[c][0], p.y+DIR[c][1]);
}
else
{
return null;
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int i,j;
Position start =new Position(0, 0);
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String msg = bf.readLine();
int line=Integer.valueOf(msg);
msg = bf.readLine();
int row=Integer.valueOf(msg);
char[][] map=new char[line][row];
for(i=0;i<line;i++){
msg = bf.readLine();
for(j=0;j<row;j++){
map[i][j]=msg.charAt(j);
}
}
//获取后 进行搜索 初始,终止坐标,遍历搜索。
for(i=0;i<line;i++){
for(j=0;j<row;j++){
if(map[i][j] == 'B'){
start.x=i;start.y=j;
break;
}
}
}
Stack<Step> stack_step=new Stack<Step>();
stack_step.push(new Step(start,3));
Step sp=stack_step.peek();
while(!stack_step.isEmpty()){
Position temp_p=sp.getNextPosition();
if(temp_p!=null){
if(temp_p.x>=0 && temp_p.x<line && temp_p.y>=0 && temp_p.y<row){
switch (map[temp_p.x][temp_p.y]) {
case '-':
//走一步
stack_step.push(new Step(sp.p,sp.count));//存档
sp.p=temp_p;
map[temp_p.x][temp_p.y]='#';
sp.count=3;
break;
case 'H':
System.out.print("Y");
return;
default:
break;
}
}
}
else{
sp=stack_step.pop();
}
}
System.out.print("N");
}
}