当前位置: 首页 > news >正文

走迷宫、八数码

走迷宫

给定一个 n×m 的二维整数数组,用来表示一个迷宫,数组中只包含 0 或 1,其中 0 表示可以走的路,1 表示不可通过的墙壁。

最初,有一个人位于左上角 (1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。

请问,该人从左上角移动至右下角 (n,m) 处,至少需要移动多少次。

数据保证 (1,1) 处和 (n,m) 处的数字为 0,且一定至少存在一条通路。

输入格式

第一行包含两个整数 n 和 m。

接下来 n 行,每行包含 m 个整数(0 或 1),表示完整的二维数组迷宫。

输出格式

输出一个整数,表示从左上角移动至右下角的最少移动次数。

数据范围

1≤n,m≤100

输入样例:
5 5 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0
输出样例:
8

dfs超时

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static int N = 110; static boolean f[][]=new boolean[N][N]; static int a[][]=new int[N][N],res=Integer.MAX_VALUE; static int dx[]={-1,0,1,0},dy[]={0,1,0,-1}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String g[] = br.readLine().split(" "); int n=Integer.parseInt(g[0]),m=Integer.parseInt(g[1]); for (int i = 0; i < n; i++) { g = br.readLine().split(" "); for (int j = 0; j < m; j++) { a[i][j]=Integer.parseInt(g[j]); } } f[1][1]=true; dfs(0,0,n,m,0); System.out.println(res); } static void dfs(int x,int y,int n,int m,int step){ if(x==n-1 && y==m-1){ res=Math.min(res, step); return; } for (int i = 0; i < 4; i++) { int nx=x+dx[i],ny=y+dy[i]; if(nx<0||nx>n||ny<0||ny>m)continue; if(a[nx][ny]==1)continue; if(f[nx][ny]==true)continue; f[nx][ny]=true; dfs(nx, ny, n, m, step+1); f[nx][ny]=false; } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; public class Main { static int N = 110; static boolean f[][]=new boolean[N][N]; static int a[][]=new int[N][N]; static int dx[]={-1,0,1,0},dy[]={0,1,0,-1}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String g[] = br.readLine().split(" "); int n=Integer.parseInt(g[0]),m=Integer.parseInt(g[1]); for (int i = 0; i < n; i++) { g = br.readLine().split(" "); for (int j = 0; j < m; j++) { a[i][j]=Integer.parseInt(g[j]); } } Queue<int[]> queue=new LinkedList<int[]>(); int t[]={0,0,0}; queue.add(t);f[0][0]=true; int res=0; while(!queue.isEmpty()){ int p[]=queue.poll(); if(p[0]==n-1 && p[1]==m-1){ System.out.println(p[2]); break; } for (int i = 0; i < 4; i++) { int nx=p[0]+dx[i],ny=p[1]+dy[i]; if(nx<0 || nx>=n || ny<0 || ny>=m)continue; if(f[nx][ny]==true || a[nx][ny]==1)continue; int son[]={nx,ny,p[2]+1}; queue.add(son);f[nx][ny] = true; } } } }

八数码

在一个 3×3 的网格中,1∼8 这 8 个数字和一个x恰好不重不漏地分布在这 3×3 的网格中。

例如:

1 2 3 x 4 6 7 5 8

在游戏过程中,可以把x与其上、下、左、右四个方向之一的数字交换(如果存在)。

我们的目的是通过交换,使得网格变为如下排列(称为正确排列):

1 2 3 4 5 6 7 8 x

例如,示例中图形就可以通过让x先后与右、下、右三个方向的数字交换成功得到正确排列。

交换过程如下:

1 2 3 1 2 3 1 2 3 1 2 3 x 4 6 4 x 6 4 5 6 4 5 6 7 5 8 7 5 8 7 x 8 7 8 x

现在,给你一个初始网格,请你求出得到正确排列至少需要进行多少次交换。

输入格式

输入占一行,将 3×3 的初始网格描绘出来。

例如,如果初始网格如下所示:

1 2 3 x 4 6 7 5 8

则输入为:1 2 3 x 4 6 7 5 8

输出格式

输出占一行,包含一个整数,表示最少交换次数。

如果不存在解决方案,则输出 −1。

输入样例:
2 3 4 1 5 x 7 6 8
输出样例
19
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Set; public class Main { static int N = 110; static boolean f[][]=new boolean[N][N]; static int a[][]=new int[N][N]; static int dx[]={-1,0,1,0},dy[]={0,1,0,-1}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String g = String.join("", br.readLine().split(" ")); char c[]=g.toCharArray(); Set<String> set=new HashSet<>(); Queue<Node> queue=new LinkedList<>(); queue.add(new Node(c,0));set.add(g); while(!queue.isEmpty()){ Node node=queue.poll(); char p[]=node.p; int step=node.step; if("12345678x".equals(new String(p))){ System.out.println(step); return; } int idx=new String(p).indexOf('x'); //System.out.println(idx); int x=idx/3,y=idx%3; for (int i = 0; i < 4; i++) { int nx=x+dx[i],ny=y+dy[i]; if(nx<0 || nx>= 3 || ny<0 ||ny>=3)continue; int exa=nx*3+ny; char pp[]=Arrays.copyOfRange(p,0,9); char t=pp[idx]; pp[idx]=pp[exa]; pp[exa]=t; if(set.contains(new String(pp)))continue; queue.add(new Node(pp,step+1));set.add(new String(pp)); } } System.out.println(-1); } static class Node{ char p[]; int step; public Node() { // TODO Auto-generated constructor stub } public Node(char p[], int step) { this.p = p; this.step = step; } } }
http://www.jsqmd.com/news/951731/

相关文章:

  • Gemini 3.1 Flash TTS:首个支持自然语言导演指令的可控语音引擎
  • ArcGIS+SWAT模型实战:从DEM到HRU分析,手把手搞定石羊河流域水文模拟(附避坑指南)
  • 医院后台管理系统的设计与实现毕设源码
  • 【字节跳动】工业级巨量引擎微服务 完整全套源码
  • UE4SS完整指南:为虚幻引擎游戏添加Lua脚本和模组功能的终极工具
  • 用快马ai五分钟生成vue3待办应用原型,体验组合式api的魅力
  • GLM-Z1-9B-0414应用场景探索:代码生成、数学推理与复杂任务处理终极指南
  • 微信小程序大转盘抽奖源码(带跑马灯旋转+实时中奖高亮)
  • Steam挂刀行情站:24小时实时监控四大平台饰品价格的完整指南
  • 概率拟合AI的哲学溯源、权力困境与确定性哲学重构探析
  • 基于Arduino与PID控制的SPEIC升降压电源设计与实现
  • 别再为Lidar-IMU标定发愁了!手把手教你用lidar_align搞定外参(附避坑指南)
  • 避开特征提取的坑:MATLAB实战中峭度、裕度因子计算的5个常见错误与调试技巧
  • 从 0 开始用 Python 训练YOLOv8检测模型(保姆级·单篇到底)
  • 告别手动填坑!用Matlab一键生成Vivado ROM的.coe文件(附完整脚本)
  • 从DQN到Dueling DQN:用PARL框架复现Atari游戏AI的保姆级代码解读
  • 纯硬件SPWM信号生成:基于运放与比较器的核心原理与工程实践
  • bert-base-uncased-emotion代码深度解析:从数据预处理到推理输出的完整流程
  • 教条主义的自我指涉悖论与西方学术霸权的虚伪批判逻辑
  • Qwen2-1.5B-Instruct安全部署指南:确保AI应用安全运行的10个要点
  • 老旧音箱智能化改造:蓝牙WiFi模块与Class-D功放实战指南
  • 钓鱼链接致储户资金损失下银行责任边界与技术防控路径研究
  • 从LAS到PLY:手把手教你用PDAL和LAStools搞定激光雷达点云数据的格式转换与预处理
  • 从百G到T级吞吐:高性能网关、防火墙、IPS、WAF背后的架构设计与性能优化实践
  • 异步任务提交 + Redis 状态轮询模式实战指南
  • CANN/cannbot-skills SIMT线程排布模式
  • 树莓派便携服务器DIY:从硬件组装到软件部署全攻略
  • 从零到部署:基于快马ai在ubuntu上快速构建可运行的个人博客系统实战
  • 解锁WanVideo_comfy高级功能:LoRAs模型安装与应用技巧终极指南
  • 终极指南:如何在消费级GPU上快速部署Wan2.2-T2V-A14B视频模型