当前位置: 首页 > 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.rkmt.cn/news/1463348.html

相关文章:

  • Real-ESRGAN深度解析:如何用AI算法让模糊图像重获新生
  • 【字节跳动】工业级巨量引擎微服务 完整全套源码
  • 用快马ai五分钟生成vue3待办应用原型,体验组合式api的魅力
  • bert-base-uncased-emotion代码深度解析:从数据预处理到推理输出的完整流程
  • 告别手动填坑!用Matlab一键生成Vivado ROM的.coe文件(附完整脚本)
  • 教条主义的自我指涉悖论与西方学术霸权的虚伪批判逻辑
  • 老旧音箱智能化改造:蓝牙WiFi模块与Class-D功放实战指南
  • 钓鱼链接致储户资金损失下银行责任边界与技术防控路径研究
  • 从百G到T级吞吐:高性能网关、防火墙、IPS、WAF背后的架构设计与性能优化实践
  • 从零到部署:基于快马ai在ubuntu上快速构建可运行的个人博客系统实战
  • 基于Arduino与433MHz无线通信的多LED灯带同步控制系统设计与实现
  • Spring Boot + Jasypt 实战指南:配置文件敏感信息加密完全手册
  • 铁路信号工必看:64D半自动闭塞13个继电器功能详解与日常维护要点
  • 避坑指南:在Win10+VS2013环境下配置BundleFusion跑通D435i离线数据(解决CUDA 8.0等环境问题)
  • “这是好事啊“:“经历过才能从容“是成长的唯一路径?
  • K2.5长文本模型工程化落地:128K稳定推理与生产部署指南
  • 旧音箱改造:从交流供电到直流电池供电的便携化DIY指南
  • 暗黑破坏神2终极优化指南:d2dx宽屏补丁让经典游戏焕发新生
  • question-vs-statement-classifier1在NPU设备上的加速指南:提升推理速度的3个方法
  • 深圳弱电箱生产厂家怎么选?采购前建议了解这几点
  • 广州:从流量争夺到AI认知权争夺,广州企业GEO布局正当时 - GEO优化
  • Vortex模组管理器:游戏模组管理的终极解决方案
  • 告别EV2400:用一块STM32F407开发板搞定BQ40Z50电池数据监控(含电压、电量读取)
  • xcms:构建现代代谢组学分析的技术架构与实现路径
  • TinyLlama微调实战:如何使用DPOTrainer进行模型对齐训练完整指南
  • 178软文网软文营销平台完善多层风控体系护航企业稳健安全传播
  • 雀魂牌谱分析工具:专业麻将数据统计与可视化解决方案
  • 如何快速部署typo-detector-distilbert-en:5分钟实现英文拼写错误检测
  • 计算机毕业设计之基于Spark的网剧推荐系统设计与实现
  • 深度解析:基于YOLOv5的AI自动瞄准系统3种实战部署方案