python 九宫重排_九宫重排 (C++代码)

#include

#include

using namespace std;

typedef int state[9];

const int maxn = 10000000;

state st[maxn],goal;

int dist[maxn];

int fact[9];

int vis[maxn];

void init(){

fact[0]=1;

for(int i=1;i<9;i++) fact[i]=i*fact[i-1];

}

int try_to_insert(int s){

int code = 0 ;

for(int i=0;i<9;i++){

int cnt =0;

for(int j=i+1;j<9;j++) if(st[s][j]

code += fact[8-i] * cnt ;

}

if(vis[code]) return 0;

vis[code]=1;

return 1;

}

const int dx[]={0,0,1,-1};

const int dy[]={1,-1,0,0};

int dfs(){

init();

int front = 1 , rear = 2;

while( front 

state &s = st[front];

if(memcmp(s,goal,sizeof(goal))==0) return front ;

int z ;

for(z=0;z<9;z++) if(!s[z]) break;

int x=z/3;

int y=z%3;

for(int d=0;d<4;d++){

int newx=x+dx[d];

int newy=y+dy[d];

int newz=newx*3+newy;

if(newx>=0 && newx<3 &&newy>=0 && newy <3){

state &t=st[rear];

memcpy(&t,&s,sizeof(s));

t[newz]=s[z];

t[z]=s[newz];

dist[rear]=dist[front]+1;

if(try_to_insert(rear)) rear++;

}

}

front++;

}

}

int main(void){

char s1[10],s2[10];

scanf("%s%s",s1,s2);

for(int i=0;i<9;i++){

if(s1[i]!='.') st[1][i]=s1[i]-'0';  else st[1][i]=0;

if(s2[i]!='.') goal[i]=s2[i]-'0'; else goal[i]=0;

}

int ans=dfs();

if(ans) printf("%d\n",dist[ans]);

else printf("%d\n",-1);

return 0;

}

解题思路:

注意事项:

参考代码:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用博弈树搜索算法解决九宫重排问题Python代码实现: ```python import copy # 定义目标状态 target = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] # 定义状态类 class State: def __init__(self, board, depth, path): self.board = board # 当前状态的棋盘 self.depth = depth # 当前状态的深度 self.path = path # 当前状态的路径 # 获取当前状态的所有子状态 def get_children(self): children = [] row, col = self.find_blank() # 获取空格的位置 moves = [(0, 1), (0, -1), (1, 0), (-1, 0)] # 定义四个方向的移动 for move in moves: new_row, new_col = row + move[0], col + move[1] if 0 <= new_row < 3 and 0 <= new_col < 3: new_board = copy.deepcopy(self.board) new_board[row][col], new_board[new_row][new_col] = new_board[new_row][new_col], new_board[row][col] # 交换空格和相邻数字的位置 new_state = State(new_board, self.depth + 1, self.path + [(new_row, new_col)]) # 创建新状态 children.append(new_state) return children # 查找空格的位置 def find_blank(self): for i in range(3): for j in range(3): if self.board[i][j] == 0: return i, j # 判断当前状态是否为目标状态 def is_target(self): return self.board == target # 定义博弈树搜索函数 def minmax(state, depth, max_player): if depth == 0 or state.is_target(): # 达到最大深度或找到目标状态 return state, state.depth if max_player: # 最大玩家 best_state = None best_depth = float('-inf') for child in state.get_children(): _, child_depth = minmax(child, depth - 1, False) if child_depth > best_depth: best_state, best_depth = child, child_depth return best_state, best_depth else: # 最小玩家 best_state = None best_depth = float('inf') for child in state.get_children(): _, child_depth = minmax(child, depth - 1, True) if child_depth < best_depth: best_state, best_depth = child, child_depth return best_state, best_depth # 定义主函数 def main(): # 定义初始状态 board = [[2, 8, 3], [1, 0, 4], [7, 6, 5]] state = State(board, 0, []) # 搜索最优解 best_state, _ = minmax(state, 10, True) # 输出路径 print("最优解路径:") for step in best_state.path: print(step) if __name__ == '__main__': main() ``` 该代码中,`State`类表示一个状态(即一个棋盘),包含当前状态的棋盘、深度和路径等属性,以及获取当前状态的所有子状态、查找空格位置和判断当前状态是否为目标状态等方法。`minmax`函数表示博弈树搜索算法,根据当前玩家(最大玩家或最小玩家)递归地搜索最优解,返回最优解的状态和深度。`main`函数表示主函数,定义初始状态并搜索最优解,输出路径。 需要注意的是,由于九宫重排问题的搜索空间很大,该代码中的搜索深度为10,可能需要根据具体情况进行调整。另外,由于博弈树搜索算法的复杂度较高,可能存在性能问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值