深度搜索B - 棋盘问题

B - 棋盘问题

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

input

输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
#.
.#
4 4
…#
…#.
.#…
#…
-1 -1

Sample Output

2
1

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>

using namespace std;

char map[9]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
八数码问题是一种经典的人工智能问题,可以使用深度优先搜索、广度优先搜索、A*算法等多种算法来解决。以下是一个基于C语言的八数码问题求解程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_QUEUE_SIZE 1000 // 队列最大长度 #define MAX_STEP 100 // 最大步数 // 八数码状态结构体 typedef struct { int board[3][3]; // 棋盘状态 int zero_row; // 空格所在行 int zero_col; // 空格所在列 char path[MAX_STEP + 1]; // 路径 } State; // 队列结构体 typedef struct { State data[MAX_QUEUE_SIZE]; // 数据 int front; // 队首指针 int rear; // 队尾指针 } Queue; // 初始化队列 void init_queue(Queue *queue) { queue->front = 0; queue->rear = 0; } // 判断队列是否为空 int is_queue_empty(Queue *queue) { return queue->front == queue->rear; } // 判断队列是否已满 int is_queue_full(Queue *queue) { return (queue->rear + 1) % MAX_QUEUE_SIZE == queue->front; } // 入队 void enqueue(Queue *queue, State state) { if (is_queue_full(queue)) { fprintf(stderr, "Error: queue is full\n"); exit(1); } queue->data[queue->rear] = state; queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE; } // 出队 State dequeue(Queue *queue) { if (is_queue_empty(queue)) { fprintf(stderr, "Error: queue is empty\n"); exit(1); } State state = queue->data[queue->front]; queue->front = (queue->front + 1) % MAX_QUEUE_SIZE; return state; } // 初始化状态 void init_state(State *state) { int i, j, k; for (i = 0, k = 1; i < 3; i++) { for (j = 0; j < 3; j++, k++) { state->board[i][j] = k % 9; if (k % 9 == 0) { state->board[i][j] = 0; state->zero_row = i; state->zero_col = j; } } } state->path[0] = '\0'; } // 复制状态 void copy_state(State *dest, State *src) { memcpy(dest->board, src->board, sizeof(src->board)); dest->zero_row = src->zero_row; dest->zero_col = src->zero_col; strcpy(dest->path, src->path); } // 交换两个位置的值 void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // 移动空格 void move(State *state, int row, int col) { swap(&state->board[state->zero_row][state->zero_col], &state->board[row][col]); state->zero_row = row; state->zero_col = col; } // 判断状态是否合法 int is_valid_state(State *state) { int i, j, k, count = 0; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { k = state->board[i][j]; if (k == 0) { continue; } count++; if (k != i * 3 + j + 1) { return 0; } } } return count == 8; } // 打印状态 void print_state(State *state) { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d ", state->board[i][j]); } printf("\n"); } printf("%s\n", state->path); } // 深度优先搜索 int dfs(State *state, int depth, int max_depth) { if (depth == max_depth) { return 0; } if (is_valid_state(state)) { print_state(state); return 1; } State next_state; int i; // 向上移动 if (state->zero_row > 0) { copy_state(&next_state, state); move(&next_state, state->zero_row - 1, state->zero_col); next_state.path[depth] = 'U'; next_state.path[depth + 1] = '\0'; if (dfs(&next_state, depth + 1, max_depth)) { return 1; } } // 向下移动 if (state->zero_row < 2) { copy_state(&next_state, state); move(&next_state, state->zero_row + 1, state->zero_col); next_state.path[depth] = 'D'; next_state.path[depth + 1] = '\0'; if (dfs(&next_state, depth + 1, max_depth)) { return 1; } } // 向左移动 if (state->zero_col > 0) { copy_state(&next_state, state); move(&next_state, state->zero_row, state->zero_col - 1); next_state.path[depth] = 'L'; next_state.path[depth + 1] = '\0'; if (dfs(&next_state, depth + 1, max_depth)) { return 1; } } // 向右移动 if (state->zero_col < 2) { copy_state(&next_state, state); move(&next_state, state->zero_row, state->zero_col + 1); next_state.path[depth] = 'R'; next_state.path[depth + 1] = '\0'; if (dfs(&next_state, depth + 1, max_depth)) { return 1; } } return 0; } // 广度优先搜索 void bfs(State *state) { Queue queue; init_queue(&queue); enqueue(&queue, *state); while (!is_queue_empty(&queue)) { State cur_state = dequeue(&queue); if (is_valid_state(&cur_state)) { print_state(&cur_state); return; } State next_state; int i; // 向上移动 if (cur_state.zero_row > 0) { copy_state(&next_state, &cur_state); move(&next_state, cur_state.zero_row - 1, cur_state.zero_col); next_state.path[strlen(cur_state.path)] = 'U'; next_state.path[strlen(cur_state.path) + 1] = '\0'; enqueue(&queue, next_state); } // 向下移动 if (cur_state.zero_row < 2) { copy_state(&next_state, &cur_state); move(&next_state, cur_state.zero_row + 1, cur_state.zero_col); next_state.path[strlen(cur_state.path)] = 'D'; next_state.path[strlen(cur_state.path) + 1] = '\0'; enqueue(&queue, next_state); } // 向左移动 if (cur_state.zero_col > 0) { copy_state(&next_state, &cur_state); move(&next_state, cur_state.zero_row, cur_state.zero_col - 1); next_state.path[strlen(cur_state.path)] = 'L'; next_state.path[strlen(cur_state.path) + 1] = '\0'; enqueue(&queue, next_state); } // 向右移动 if (cur_state.zero_col < 2) { copy_state(&next_state, &cur_state); move(&next_state, cur_state.zero_row, cur_state.zero_col + 1); next_state.path[strlen(cur_state.path)] = 'R'; next_state.path[strlen(cur_state.path) + 1] = '\0'; enqueue(&queue, next_state); } } } int main() { State state; init_state(&state); bfs(&state); return 0; } ``` 以上程序实现了八数码问题的解法,可以通过深度优先搜索或广度优先搜索来求解。其中深度优先搜索使用了递归实现,广度优先搜索使用了队列实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值