蓝桥杯--穿越雷区(宽度优先搜索)

标题:穿越雷区

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

解题说明:

宽度优先方法

1.设置一个node类存放当前状态a[][]和记录步数

2.设置一个存放node的队列

当队列不空取出队头,枚举四个方向并加以条件判断,是否可以移动

若可以移动,将原来的位置置为0,这样就不会返回原位置(因为是宽度,不存在回溯),并将新的状态进入队列

若新的位置和B的位置一致,则返回当前状态步数+1,搜索结束

否则继续判断下一个位置。

package 穿越雷区;

import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	static int n;
	public static void main(String[] args) throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		
		Scanner in=new Scanner(System.in);
		n=in.nextInt();
		String at[][]=new String[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				at[i][j]=in.next();
			}
		}
		node fn=new node(at,0,n,"0");
		System.out.println(bfs(fn));
	}
	public static boolean check(int x,int y,node at){
		if(x<0||y>=n||x>=n||y<0) return false;
		if(at.ct=="0") return true;
		if(at.ct.equals("+")&&(at.a[x][y].equals("-")||at.a[x][y].equals("B"))) return true;
		if(at.ct.equals("-")&&(at.a[x][y].equals("+")||at.a[x][y].equals("B"))) return true;
		return false;
	}
	public static int bfs(node fn) throws CloneNotSupportedException{
		List<node>list=new LinkedList<node>();//存放生成的所有状态
		list.add(fn);
		int dir[][]={{0,1},{0,-1},{1,0},{-1,0}};
		int xb = 0,yb=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(fn.a[i][j].equals("B")){
					xb=i;
					yb=j;
					break;
				}
			}
		}
		while(!list.isEmpty()){
			node tn=list.get(0);//取出队头
			list.remove(0);
			int x=0,y=0;
			String ta[][]=new String[n][n];
			for(int i=0;i<n;i++){
				ta[i]=tn.a[i].clone();
			}
			//找到当前状态的A的坐标
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(ta[i][j].equals("A")){
						x=i;
						y=j;
						break;
					}
				}
			}
			for(int i=0;i<4;i++){
				int nx,ny;
				String t;
				nx=x+dir[i][0];
				ny=y+dir[i][1];
				if(check(nx,ny,tn)){
					String st=ta[nx][ny];
					ta[nx][ny]=ta[x][y];
					ta[x][y]="0";
					node nn=new node(ta,tn.ants+1,n,st);
					if(nx==xb&&ny==yb) {
						return tn.ants+1;
					}
					list.add(nn);
					ta[nx][ny]=st;
					ta[x][y]="A";
				}
			}
		}
		return -1;
	}
}
class node implements Cloneable{
	String a[][];
	int ants;
	String ct;
	public node(String a[][],int ants,int n,String ct) throws CloneNotSupportedException{
		this.a=new String[n][n];
		this.ants=ants;
		this.ct=ct;
		for(int i=0;i<n;i++){
			if(a[i].clone().length!=0){
				this.a[i]=a[i].clone();
			}			
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值