无信息搜索之迭代加深(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

相关文章

在C#中获取端口号与系统信息的高效实践

《在C#中获取端口号与系统信息的高效实践》在现代软件开发中,尤其是系统管理、运维、监控和性能优化等场景中,了解计算机硬件和网络的状态至关重要,C#作为一种广泛应用的编程语言,提供了丰富的API来帮助开... 目录引言1. 获取端口号信息1.1 获取活动的 TCP 和 UDP 连接说明:应用场景:2. 获取硬

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭

C#实现获取电脑中的端口号和硬件信息

《C#实现获取电脑中的端口号和硬件信息》这篇文章主要为大家详细介绍了C#实现获取电脑中的端口号和硬件信息的相关方法,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 我们经常在使用一个串口软件的时候,发现软件中的端口号并不是普通的COM1,而是带有硬件信息的。那么如果我们使用C#编写软件时候,如

通过C#获取PDF中指定文本或所有文本的字体信息

《通过C#获取PDF中指定文本或所有文本的字体信息》在设计和出版行业中,字体的选择和使用对最终作品的质量有着重要影响,然而,有时我们可能会遇到包含未知字体的PDF文件,这使得我们无法准确地复制或修改文... 目录引言C# 获取PDF中指定文本的字体信息C# 获取PDF文档中用到的所有字体信息引言在设计和出

C#读取本地网络配置信息全攻略分享

《C#读取本地网络配置信息全攻略分享》在当今数字化时代,网络已深度融入我们生活与工作的方方面面,对于软件开发而言,掌握本地计算机的网络配置信息显得尤为关键,而在C#编程的世界里,我们又该如何巧妙地读取... 目录一、引言二、C# 读取本地网络配置信息的基础准备2.1 引入关键命名空间2.2 理解核心类与方法

使用Python检查CPU型号并弹出警告信息

《使用Python检查CPU型号并弹出警告信息》本教程将指导你如何编写一个Python程序,该程序能够在启动时检查计算机的CPU型号,如果检测到CPU型号包含“I3”,则会弹出一个警告窗口,感兴趣的小... 目录教程目标方法一所需库步骤一:安装所需库步骤二:编写python程序步骤三:运行程序注意事项方法二

C# ComboBox下拉框实现搜索方式

《C#ComboBox下拉框实现搜索方式》文章介绍了如何在加载窗口时实现一个功能,并在ComboBox下拉框中添加键盘事件以实现搜索功能,由于数据不方便公开,作者表示理解并希望得到大家的指教... 目录C# ComboBox下拉框实现搜索步骤一步骤二步骤三总结C# ComboBox下拉框实现搜索步骤一这

PostgreSQL如何查询表结构和索引信息

《PostgreSQL如何查询表结构和索引信息》文章介绍了在PostgreSQL中查询表结构和索引信息的几种方法,包括使用`d`元命令、系统数据字典查询以及使用可视化工具DBeaver... 目录前言使用\d元命令查看表字段信息和索引信息通过系统数据字典查询表结构通过系统数据字典查询索引信息查询所有的表名可

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。