蓝桥杯题目-移动距离

X星球居民小区的楼房全是一样的,并且按矩阵样式排列。其楼房的编号为1,2,3...
当排满一行时,从下一行相邻的楼往反方向排号。
比如:当小区排号宽度为6时,开始情形如下:

1  2  3  4  5  6
12 11 10 9  8  7
13 14 15 .....

我们的问题是:已知了两个楼号m和n,需要求出它们之间的最短移动距离(不能斜线方向移动)

输入为3个整数w m n,空格分开,都在1到10000范围内
要求输出一个整数,表示m n 两楼间最短移动距离。

例如:
用户输入:
6 8 2
则,程序应该输出:
4

再例如:
用户输入:
4 7 20
则,程序应该输出:
5


解题思路:使用bfs广度搜索来实现求两地的最短路径

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

//使用bfs来实现
public class 最短路径算法_求两城市的最短路径 {

	public static void main(String[] args) {
		int n, p, q;
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		// 起点p 终点q
		p = scanner.nextInt();
		q = scanner.nextInt();
		int sum = 0;

		if (p < q) {
			int i, j, k;
			int c = 1;
			for (j = 1;; j++) {
				// 计算数组的行数
				if (j >= q && j % n == 0) {
					sum = j;
					break;
				}

			}

			// 数组
			int a[][] = new int[sum / n][n];
			// 标记
			int book[][] = new int[sum / n][n];
			// 初始化数组
			for (i = 0; i < sum / n; i++)
				for (j = 0; j < n; j++)
					a[i][j] = c++;

			int temp;
			for (i = 0; i < sum / n; i++)
				for (j = 0, k = n - 1; j < k; j++, k--) {
					// 偶数行为倒序
					if ((i + 1) % 2 == 0) {
						temp = a[i][j];
						a[i][j] = a[i][k];
						a[i][k] = temp;
					}
				}

			// 使用链表来模拟队列
			LinkedList<City> list = new LinkedList<City>();
            //记录终点的下标
			int qIndexX = 0, qIndexY = 0;

			System.out.println("\n");
			// 打印数组
			for (i = 0; i < sum / n; i++) {
				for (j = 0; j < n; j++)
					System.out.print(a[i][j] + " ");
				System.out.println();
			}

			System.out.println("\n");

			for (i = 0; i < sum / n; i++) {
				for (j = 0; j < n; j++) {
					// 寻找起点城市的坐标
					if (a[i][j] == p) {
						City city = new City(i, j, 0);
						// 将起点城市添加进队列中
						list.add(city);
						book[i][j] = 1;// 标记该城市已经走过了
					}

					// 寻找终点的坐标
					if (a[i][j] == q) {
						qIndexX = i;
						qIndexY = j;
						break;
					}
				}
			}

			City per, city;

			// 人物移动方向。右下左上
			int next[][] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };

			int flag = 0;// 标记
			while (list.size() > 0) {
				// 获取队列的队头
				per = list.get(0);

				// 通过队列的对头的城市坐标点来向四周拓展其他城市
				for (i = 0; i < 4; i++) {
					int tx = per.getX() + next[i][0];
					int ty = per.getY() + next[i][1];
                    //如果地图越界就跳过
					if (tx < 0 || ty < 0 || tx >= sum / n || ty >= n)
						continue;

					// 判断该城市是否已经被走过
					if (book[tx][ty] == 0) {
						// 标记该城市已经走过了
						book[tx][ty] = 1;
						
						city = new City(tx, ty, per.getStep() + 1);

						// 将城市添加进队列
						list.add(city);

						// 到达目的地
						if (tx == qIndexX && ty == qIndexY) {

							flag = 1;
							break;
						}

					}

				}

				if (flag == 1)
					break;

				// 将队头城市出队
				list.remove(0);

			}
			city = list.get(list.size() - 1);
			System.out.println(p + "号到" + q + "号之间的步数" + city.getStep());

		}

	}

}

创建一个楼房对象。每个city对象就代表每号楼

// 楼房对象
class City {
	private int x; // x坐标
	private int y; // y坐标
	private int step; // 步数

	public City(int x, int y, int step) {
		super();
		this.x = x;
		this.y = y;
		this.step = step;
	}

	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 int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值