package com.cn.hw.test;
/**
* @author 刘利娟 liulijuan132@gmail.com
* @version 创建时间:2014年8月6日 上午8:48:50 类说明:
* 森林里的苯苯熊要乔迁新喜,上次他已经将物品打包完成,并约了朋友来帮忙。接下来他要选定一个搬家的时间
* ,想了很久,就决定在国庆节进行,因为国庆放假朋友们都有时间啦
* 。但是在森林里,从他现在房子到新豪宅,所经之地有山有水,路途曲折,甚至有些道路是不通的。
* 请你和他一起查看指定的地图,看看从笨笨熊现在的房子到新宅之间,道路是否是畅通的呢?
* 地图是R行、C列的矩阵,矩阵的每一个格子刚好是一天的行程。 矩阵由“B”、“-”、“#”、“H”四种字符成员组成,其中: B:
* 代表苯苯熊现在的房子; H: 代表笨笨熊新的豪宅; -: 代表可以通行的道路; #: 代表无法通过的障碍(高山、大河等);
* 此外,森林里也有交通规则地:在任务位置,只能向“上、下、左、右”四个方向中的其中一个方向行走。
*/
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Square {
private int x;
private int y;
private char ch;
private Set<Square> adjacencies = null;
public Square(int x, int y, char ch) {
super();
this.x = x;
this.y = y;
this.ch = ch;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public char getCh() {
return ch;
}
public void setCh(char ch) {
this.ch = ch;
}
public Set<Square> getAdjacencies() {
return adjacencies;
}
public void setAdjacencies(Set<Square> adjacencies) {
this.adjacencies = adjacencies;
}
public String toString() {
return "square[" + this.x + "][" + this.y + "]=" + this.ch;
}
}
public class Main2 {
static Square[][] graph;
public static Square start;
static int R;
static int C;
static boolean flag = false;
static int[][] visited;
public static boolean findPath(Square start) {
Set<Square> adjacents = start.getAdjacencies();
visited[start.getX()][start.getY()] = 1;
if (adjacents != null) {
for (Square square : adjacents) {
System.out.println(visited[square.getX()][square.getY()]);
if (visited[square.getX()][square.getY()] == 0) {
visited[square.getX()][square.getY()] = 1;
if (square.getCh() == 'H') {
flag = true;
return flag;
}
findPath(square);
}
}
}
return flag;
}
public static void initial(int r, int c) {
visited = new int[R][C];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
visited[i][j] = 0;
if (graph[i][j].getCh() == 'B' || graph[i][j].getCh() == '-') {
Set<Square> set = new HashSet<Square>();
if (i - 1 > 0
&& (graph[i - 1][j].getCh() == '-' || graph[i - 1][j]
.getCh() == 'H')) {
set.add(graph[i - 1][j]);
}
if (j - 1 > 0
&& (graph[i][j - 1].getCh() == '-' || graph[i][j - 1]
.getCh() == 'H')) {
set.add(graph[i][j - 1]);
}
if (i + 1 < r
&& (graph[i + 1][j].getCh() == '-' || graph[i + 1][j]
.getCh() == 'H')) {
set.add(graph[i + 1][j]);
}
if (j + 1 < c
&& (graph[i][j + 1].getCh() == '-' || graph[i][j + 1]
.getCh() == 'H')) {
set.add(graph[i][j + 1]);
}
/*
* System.out.println("square"+i+","+j+"的邻接点"); for(Square
* square: set){ System.out.println(square); }
*/
graph[i][j].setAdjacencies(set);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
R = scan.nextInt();
C = scan.nextInt();
graph = new Square[R][C];
for (int i = 0; i < R; i++) {
String string = scan.next();
for (int j = 0; j < C; j++) {
graph[i][j] = new Square(i, j, string.charAt(j));
if (string.charAt(j) == 'B') {
start = graph[i][j];
}
}
}
initial(R, C);
if (findPath(start)) {
System.out.println("Y");
} else {
System.out.println("N");
}
}
}
= =自己测试着可以通过 但是就是通不过华为的系统 为喵(下午就发现原因了,一定要按要求来啊,多写了两句输出语句= =) 虽然代码渣渣。
样例输入: | 1 5 -B-H# |
样例输出: | Y |