蓝桥杯 第七届决赛 路径之谜

本文介绍了一款名为“路径之谜”的游戏,玩家需要根据城堡墙壁上箭靶的数量推断出骑士的行走路径。文章提供了游戏规则说明及一个Java实现的示例程序,通过深度优先搜索算法来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



路径之谜


小明冒充X星球的骑士,进入了一个奇怪的城堡。
城堡里边什么都没有,只有方形石头铺成的地面。


假设城堡地面是 n x n 个方格。【如图1.png】所示。


按习俗,骑士要从西北角走到东南角。
可以横向或纵向移动,但不能斜着走,也不能跳跃。
每走到一个新方格,就要向正北方和正西方各射一箭。
(城堡的西墙和北墙内各有 n 个靶子)




同一个方格只允许经过一次。但不必做完所有的方格。


如果只给出靶子上箭的数目,你能推断出骑士的行走路线吗?


有时是可以的,比如图1.png中的例子。


本题的要求就是已知箭靶数字,求骑士的行走路径(测试数据保证路径唯一)


输入:
第一行一个整数N(0<N<20),表示地面有 N x N 个方格
第二行N个整数,空格分开,表示北边的箭靶上的数字(自西向东)
第三行N个整数,空格分开,表示西边的箭靶上的数字(自北向南)


输出:
一行若干个整数,表示骑士路径。


为了方便表示,我们约定每个小格子用一个数字代表,从西北角开始编号: 0,1,2,3....
比如,图1.png中的方块编号为:


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




示例:
用户输入:
4
2 4 3 4
4 3 3 3


程序应该输出:
0 4 5 1 2 3 7 11 10 9 13 14 15






资源约定:
峰值内存消耗 < 256M
CPU消耗  < 1000ms




请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。


所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。

注意:主类的名字必须是:Main,否则按无效代码处理。




用深度搜索 dfs,不满足条件的return;
应该不是很难,不过粗心花了点,,写的有点久。
因为没有oj去判定,,我也不知道答案对不对,,感觉思路应该没错。。仅供参考。。。有错欢迎纠正。。
package 第七届试题;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;

public class 路径之谜 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[][] map = new int[n][n];
		boolean[][] m = new boolean[n][n];
		int[] north = new int[n];
		int[] west = new int[n];
		for (int i = 0; i < n; i++)
			north[i] = scanner.nextInt();
		for (int i = 0; i < n; i++)
			west[i] = scanner.nextInt();
		scanner.close();
		Stack<Integer> stack = new Stack<>();
		m[0][0] = true;
		stack.push(0);
		north[0]--;
		west[0]--;
		dfs(map, north, west, 0, m, stack);
	}

	public static void dfs(int[][] map, int[] north, int[] west, int po, boolean[][] m, Stack<Integer> stack) {
		Iterator<Integer> it = stack.iterator();
		// while (it.hasNext()) {
		// Integer integer = (Integer) it.next();
		// System.out.print(integer + " ");
		// }
		// System.out.println();
		// System.out.println("po=" + po + " x=" + po / 4 + " y=" + po % 4);
		if (po == map.length * map.length - 1) {
			boolean flag = true;
			for (int i = 0; i < map.length; i++)
				if (north[i] != 0 || west[i] != 0) {
					flag = false;
					break;
				}
			if (flag) {
				Iterator<Integer> iterator = stack.iterator();
				while (iterator.hasNext()) {
					Integer integer = (Integer) iterator.next();
					System.out.print(integer + " ");
				}
				System.out.println();
			}
			return;
		}

		int x = po / map.length;
		int y = po % map.length;

		if (x > 0 && m[x - 1][y] == false && west[x - 1] > 0 && north[y] > 0) {// 能向上走
			west[x - 1]--;
			north[y]--;
			m[x - 1][y] = true;
			// System.out.println("向上走 add" + (po - map.length));
			stack.push(po - map.length);
			dfs(map, north, west, po - map.length, m, stack);
			west[x - 1]++;
			north[y]++;
			m[x - 1][y] = false;
			stack.pop();
		}
		if (x < map.length - 1 && m[x + 1][y] == false && west[x + 1] > 0 && north[y] > 0) {// 能向下走
			west[x + 1]--;
			north[y]--;
			m[x + 1][y] = true;
			// System.out.println("向下走 add" + (po + map.length));
			stack.add(po + map.length);
			dfs(map, north, west, po + map.length, m, stack);
			west[x + 1]++;
			north[y]++;
			m[x + 1][y] = false;
			stack.pop();
		}
		if (y > 0 && m[x][y - 1] == false && west[x] > 0 && north[y - 1] > 0) {// 能向左走
			west[x]--;
			north[y - 1]--;
			m[x][y - 1] = true;
			// System.out.println("向左走 add" + (po - 1));
			stack.add(po - 1);
			dfs(map, north, west, po - 1, m, stack);
			west[x]++;
			north[y - 1]++;
			m[x][y - 1] = false;
			stack.pop();
		}
		if (y < map.length - 1 && m[x][y + 1] == false && west[x] > 0 && north[y + 1] > 0) {
			west[x]--;
			north[y + 1]--;
			m[x][y + 1] = true;
			// System.out.println("向右走 add" + (po + 1));
			stack.add(po + 1);
			dfs(map, north, west, po + 1, m, stack);
			west[x]++;
			north[y + 1]++;
			m[x][y + 1] = false;
			stack.pop();
		}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值