穿越雷区--bfs宽度优先搜索

标题:穿越雷区

X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。
某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?

已知的地图是一个方阵,上面用字母标出了A,B区,其它区都标了正号或负号分别表示正负能量辐射区。
例如:
A + – + –
– + – – +
– + + + –
+ – + – +
B + – + –

坦克车只能水平或垂直方向上移动到相邻的区。

数据格式要求:
输入第一行是一个整数n,表示方阵的大小, 4<=n<100
接下来是n行,每行有n个数据,可能是A,B,+,-中的某一个,中间用空格分开。
A,B都只出现一次。

要求输出一个整数,表示坦克从A区到B区的最少移动步数。
如果没有方案,则输出-1

例如:
用户输入:
5
A + – + –
– + – – +
– + + + –
+ – + – +
B + – + –

则程序应该输出:
10

资源约定:
峯值内存消耗(含虚拟机) < 512M

CPU消耗  < 2000ms


package com.lulu.main3;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;

public class Main {

static int n;
static String [][]map;
static int [][]minpathcount;//存储走的最小步数
static boolean [][]visit;//判断是否已经走过了
static int x1 = 0,x2=0,y1=0,y2=0;
static node [][]pre;//记录他的前驱节点,方便输出方向使用
static String [][]dir;//存储走的方向

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
map = new String[n][n];
minpathcount = new int[n][n];
visit = new boolean[n][n];
pre = new node[n][n];
dir = new String[n][n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
minpathcount[i][j] = Integer.MAX_VALUE;
}
}
in.nextLine();
for(int i = 0; i < n; i++)
{
String s = in.nextLine();
map[i] = s.split(” “);
for(int j = 0; j < n; j++)
{
if(map[i][j].equals(“A”))
{
x1 = i;
y1 = j;
}
if(map[i][j].equals(“B”))
{
x2 = i;
y2 = j;
}
}
}
if(bfs(x1, y1))
{
System.out.println(minpathcount[x2][y2]);
Print();
}else
{
System.out.println(“-1”);
}
}

private static void Print() {
node nownode = new node();
Stack<node> stack = new Stack<node>();
nownode.x = x2;
nownode.y = y2;
while(nownode.x != x1 || nownode.y != y1)
{
stack.push(nownode);
nownode = pre[nownode.x][nownode.y];
}
while(!stack.isEmpty())
{
nownode = stack.peek();
System.out.println(nownode.x+” “+nownode.y+” “+dir[nownode.x][nownode.y]);
stack.pop();
}
}

private static boolean bfs(int x1, int y1) {
Queue<node> queue = new LinkedList<node>();
queue.offer(new node(x1, y1));
minpathcount[x1][y1] = 0;
while(!queue.isEmpty())
{
node ha = queue.poll();
int x = ha.x;
int y = ha.y;
visit[x][y] = true;

if(x == x2 && y == y2)
{
return true;
}

if(x-1>= 0 && !map[x][y].equals(map[x-1][y]) && !visit[x-1][y])
{
minpathcount[x-1][y] = minpathcount[x][y] + 1 > minpathcount[x-1][y] ? minpathcount[x-1][y] :minpathcount[x][y] + 1;
queue.offer(new node(x-1,y));
pre[x-1][y] = new node(x,y);
dir[x-1][y] = “U”;
}
if(x+1<n &&!map[x][y].equals(map[x+1][y]) && !visit[x+1][y])
{
minpathcount[x+1][y] = minpathcount[x][y] + 1 > minpathcount[x+1][y] ? minpathcount[x+1][y] : minpathcount[x][y] + 1;
queue.offer(new node(x+1,y));
pre[x+1][y] = new node(x,y);
dir[x+1][y] = “D”;
}
if(y+1<n&&!map[x][y].equals(map[x][y+1]) && !visit[x][y+1])
{
minpathcount[x][y+1] = minpathcount[x][y] + 1 > minpathcount[x][y+1] ? minpathcount[x][y+1] : minpathcount[x][y] + 1;
queue.offer(new node(x,y+1));
pre[x][y+1] = new node(x,y);
dir[x][y+1] = “R”;
}
if(y-1>=0 &&!map[x][y].equals(map[x][y-1]) && !visit[x][y-1])
{
minpathcount[x][y-1] = minpathcount[x][y] + 1 > minpathcount[x][y-1] ? minpathcount[x][y-1] : minpathcount[x][y] + 1;
queue.offer(new node(x,y-1));
pre[x][y-1] = new node(x,y);
dir[x][y-1] = “L”;
}
}
return false;
}
}

class node{
int x;
int y;
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 node(int x, int y) {
super();
this.x = x;
this.y = y;
}
public node() {
super();
}
}

点赞