华为编程大赛,笨笨熊搬家,OJ平台网络问题提交不上代码。先将代码到这。

来源:互联网 发布:什么电脑壁纸软件好 编辑:程序博客网 时间:2024/05/19 16:28

 

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main

{
 int H = 0;
 int W = 0;
 class son
 {
  int hangX;
  int lieY;
  son father;
  public son(int x, int y, son z)
  {
   this.hangX = x;
   this.lieY = y;
   this.father = z;
  }
 }
 String[][] map ={};
 public String reDeal(int aa,int bb,int cc,int dd )
 {
  String result = "N";
  H = map.length;
  W = map[0].length;
  Set fromPoints = new HashSet();
  fromPoints.add(new son(cc, dd, null));
  while(true)
  {
   Set toPoints = new HashSet();
   son s = goStep(fromPoints, toPoints,aa,bb);
   if (s != null)
   {
    result = "Y";
    break;
   }
   if (toPoints.isEmpty())
   {
    break;
   }
   fromPoints = toPoints;
  }
  return result;
 }
 
 public son goStep(Set from, Set to,int xx,int yy)
 {
  Iterator it = from.iterator();
  son s0;
  son[] s;
  while (it.hasNext())
  {
   s0 = (son) it.next();
   s = new son[4];
   s[0] = new son(s0.hangX - 1, s0.lieY, s0);
   s[1] = new son(s0.hangX, s0.lieY - 1, s0);
   s[2] = new son(s0.hangX + 1, s0.lieY, s0);
   s[3] = new son(s0.hangX, s0.lieY + 1, s0);
   for (int i = 0; i < 4; i++)
   {
    if (s[i].hangX < 0 || s[i].hangX >= map.length)
     continue;
    if (s[i].lieY < 0 || s[i].lieY >= map[0].length)
     continue;
    String x = map[s[i].hangX][s[i].lieY];
    if (s[i].hangX == xx && s[i].lieY == yy) {
     return new son(s[i].hangX, s[i].lieY, s0);
    }
    if (!"#".equals(x))
    {
     map[s[i].hangX][s[i].lieY] = "?";
     to.add(s[i]);
    }
   }
  }
  return null;
 }

 public static void main(String[] args) throws NumberFormatException, IOException
 {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  int aa = 0;
  int bb = 0;
  int cc = 0;
  int dd = 0;
  int X = Integer.parseInt(br.readLine());
  int Y = Integer.parseInt(br.readLine());
  String[][] temp = new String [X][Y];
  String tmp = "";
  for(int x = 0 ; x < X ; x ++){
   tmp = br.readLine();
   for(int y = 0; y < Y ; y++){
    if(tmp.charAt(y)=='B'){
     aa = x;
     bb = y;
    }else if(tmp.charAt(y)=='H'){
     cc = x;
     dd = y;
    }
    temp[x][y] = String.valueOf(tmp.charAt(y));
   }
  }
  Main m = new Main();
  m.map = temp;
  System.out.println(m.reDeal(aa,bb,cc,dd));
 }
}

0 0
原创粉丝点击