无信息搜索之迭代加深(iterative deepening)

2023-10-23 21:30

本文主要是介绍无信息搜索之迭代加深(iterative deepening),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

The Rotation Game

描述
The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.
在这里插入图片描述

Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration.

输入
The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single ‘0’ after the last test case that ends the input.

输出
For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from ‘A’ to ‘H’, and there should not be any spaces between the letters in the line. If no moves are needed, output `No moves needed’ instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.

样例输入

1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3 1 1 2 1 1 1 3 2 2 1 1 2 2 2 2 3 3 3 3 2 3 3 1
1 3 3 3 1 1 2 2 1 1 1 2 2 3 3 3 2 2 2 1 1 3 2 3
1 3 1 3 2 2 3 3 1 1 3 2 1 3 3 2 1 2 1 3 1 2 2 2
2 3 1 3 3 2 1 3 3 1 1 2 1 3 2 3 2 1 2 1 3 2 1 2
1 2 2 3 2 1 3 3 1 1 3 1 3 3 2 3 2 1 1 1 2 2 3 2
1 2 2 2 1 3 3 3 1 3 2 1 1 1 3 2 3 2 3 1 2 1 3 2
2 2 3 2 1 3 1 3 1 3 2 3 1 1 3 2 2 2 3 1 3 1 1 2
3 2 1 2 1 3 1 3 3 3 2 2 1 1 3 3 2 2 3 1 1 1 2 2
1 2 2 2 1 3 3 3 3 3 2 1 1 3 1 2 3 3 2 2 1 1 1 2
1 3 1 1 2 3 2 2 2 1 3 3 2 2 3 1 1 3 1 2 2 3 1 3
1 3 1 1 2 1 3 2 2 1 3 2 2 2 3 3 1 3 1 2 1 3 2 3
1 2 2 2 3 2 1 1 3 2 1 1 3 2 3 3 1 3 1 2 2 3 3 1
3 2 1 2 2 1 2 2 1 1 3 3 3 2 3 1 1 3 1 2 3 3 2 1
3 2 2 1 3 2 3 1 2 3 1 1 1 2 2 2 1 3 3 1 3 1 3 2
3 1 2 2 3 2 3 1 2 3 1 1 1 1 2 2 2 2 3 3 3 1 3 1
3 2 3 1 3 2 3 1 2 3 1 2 1 2 2 1 2 3 3 1 2 1 3 1
3 1 3 2 1 2 1 1 3 2 3 2 3 1 2 2 1 2 1 3 2 3 3 1
3 3 3 1 1 2 3 1 1 2 3 1 2 1 2 2 2 3 3 3 1 1 2 2
0

样例输出

AC
2
DDHH
2
No moves needed
1
AHADDE
2
CACGAH
2
AAHEGACA
1
BDFFDHHFD
1
CBGFGBHHFD
1
ABGEECGAGAC
1
BBDFHBBHFG
2
AGAEHEEDHF
2
ABBCAACDE
1
ECGEGAACG
2
DBBFGHBHA
3
BDAEECGACB
3
BFHBDDFFHF
3
GEGHBFDBH
1
DDDBHBBFH
1
DADECCAEG
2
ADHAEECCG
2
DBDFCBFHHF
2
import java.util.Scanner;
import java.io.*;
//import java.util.Random;/***          A     B*          01    02*          03    04* H  05 06 07 08 09 10 11 C*          12    13* G  14 15 16 17 18 19 20 D*          21    22*          23    24*          F     E *  *  input cases:* 1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3* 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3* 0*/public class Main {static class State{public String boardStr;public String lastOp;public State parent;public int depth;public State(State parent, String o, String s){State.this.parent = parent;State.this.lastOp = o;State.this.boardStr = s;}@Overridepublic int hashCode(){return State.this.boardStr.hashCode();}public boolean check(){if(boardStr.charAt(6) == boardStr.charAt(7) &&boardStr.charAt(7) == boardStr.charAt(8) &&boardStr.charAt(8) == boardStr.charAt(11) &&boardStr.charAt(11) == boardStr.charAt(12) &&boardStr.charAt(12) == boardStr.charAt(15) &&boardStr.charAt(15) == boardStr.charAt(16) &&boardStr.charAt(16) == boardStr.charAt(17) )return true;return false;}public State move(String action){char[] board = boardStr.toCharArray();char temp;switch(action){case "A":temp = board[0];board[0] = board[2];board[2] = board[6];board[6] = board[11];board[11] = board[15];board[15] = board[20];board[20] = board[22];board[22] = temp;break;case "B":temp = board[1];board[1] = board[3];board[3] = board[8];board[8] = board[12];board[12] = board[17];board[17] = board[21];board[21] = board[23];board[23] = temp;break;case "C":temp = board[10];board[10] = board[9];board[9] = board[8];board[8] = board[7];board[7] = board[6];board[6] = board[5];board[5] = board[4];board[4] = temp;break;case "D":temp = board[19];board[19] = board[18];board[18] = board[17];board[17] = board[16];board[16] = board[15];board[15] = board[14];board[14] = board[13];board[13] = temp;break;case "E":temp = board[23];board[23] = board[21];board[21] = board[17];board[17] = board[12];board[12] = board[8];board[8] = board[3];board[3] = board[1];board[1] = temp;break;case "F":temp = board[22];board[22] = board[20];board[20] = board[15];board[15] = board[11];board[11] = board[6];board[6] = board[2];board[2] = board[0];board[0] = temp;break;case "G":temp = board[13];board[13] = board[14];board[14] = board[15];board[15] = board[16];board[16] = board[17];board[17] = board[18];board[18] = board[19];board[19] = temp;break;case "H":temp = board[4];board[4] = board[5];board[5] = board[6];board[6] = board[7];board[7] = board[8];board[8] = board[9];board[9] = board[10];board[10] = temp;break;	}State newState = new Main.State(State.this, action, new String(board));return newState;}public boolean solvable(int limit){char[] board = boardStr.toCharArray();char[] center = {board[6],board[7],board[8],board[11],board[12],board[15],board[16],board[17]};int[] count = new int[3];for(char c: center){count[c-'1'] += 1;}int maxCount = Math.max(Math.max(count[0], count[1]),count[2]);if(8 - maxCount > limit){return false;}return true;}}static Scanner input;static String boardString;static String[] actions = {"A","B","C","D","E","F","G","H"};static State targetState;static char targetNum;static public void showBoard(State s){char[] board = s.boardStr.toCharArray();System.out.println("  "+board[0]+" "+board[1]);System.out.println("  "+board[2]+" "+board[3]);System.out.println(""+board[4]+board[5]+board[6]+board[7]+board[8]+board[9]+board[10]);System.out.println("  "+board[11]+" "+board[12]);System.out.println(""+board[13]+board[14]+board[15]+board[16]+board[17]+board[18]+board[19]);System.out.println("  "+board[20]+" "+board[21]);System.out.println("  "+board[22]+" "+board[23]);		}	static public void search(State origin){	for(int i=1; i<1000; ++i){if(DepthLimitedSearch(origin, i)){break;}}String path = "";while(targetState.parent != null){path = targetState.lastOp + path;targetState = targetState.parent;}System.out.println(path);System.out.println(targetNum);}static public boolean DepthLimitedSearch(State curr, int limit){if(limit < 0 || !curr.solvable(limit)){return false;}if(curr.check()){targetState = curr;			targetNum = curr.boardStr.charAt(7);return true;}else{if(limit > 0)for(String act:actions){if(DepthLimitedSearch(curr.move(act), limit-1)){return true;}}}return false;}//	static public void genTest(){
//		Random rand =new Random();
//		State o = new State(null,null,"111111112222222233333333");
//		State tmp = o;
//		for(int i=0; i<20; ++i){
//			for(int j=0; j<7; ++j){
//				tmp = tmp.move(actions[rand.nextInt(8)]);
//			}
//			System.out.println(tmp.boardStr);
//		}
//	}public static void main(String[] args) throws FileNotFoundException{input= new Scanner(new BufferedReader(new FileReader("E:\\workspace\\rotate\\bin\\data.txt")));//input= new Scanner(System.in);while(true){int[] board = new int[24];board[0] = input.nextInt();if (board[0] == 0)break;StringBuilder sb = new StringBuilder();sb.append(board[0]);for(int i=1; i<24;++i){board[i] = input.nextInt();sb.append(board[i]);}boardString = sb.toString();State originState = new Main.State(null, null, boardString);if(originState.check()){System.out.println("No moves needed");System.out.println(board[7]);}else{search(originState);}}}
}

这篇关于无信息搜索之迭代加深(iterative deepening)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/270549

相关文章

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法

《Linux查看系统盘和SSD盘的容量、型号及挂载信息的方法》在Linux系统中,管理磁盘设备和分区是日常运维工作的重要部分,而lsblk命令是一个强大的工具,它用于列出系统中的块设备(blockde... 目录1. 查看所有磁盘的物理信息方法 1:使用 lsblk(推荐)方法 2:使用 fdisk -l(

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

C++迭代器失效的避坑指南

《C++迭代器失效的避坑指南》在C++中,迭代器(iterator)是一种类似指针的对象,用于遍历STL容器(如vector、list、map等),迭代器失效是指在对容器进行某些操作后... 目录1. 什么是迭代器失效?2. 哪些操作会导致迭代器失效?2.1 vector 的插入操作(push_back,

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

springboot实现配置文件关键信息加解密

《springboot实现配置文件关键信息加解密》在项目配置文件中常常会配置如数据库连接信息,redis连接信息等,连接密码明文配置在配置文件中会很不安全,所以本文就来聊聊如何使用springboot... 目录前言方案实践1、第一种方案2、第二种方案前言在项目配置文件中常常会配置如数据库连接信息、Red

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文