竞赛课第四周(八数码问题+八皇后问题)

目的:

  1. 掌握递归和排序

  2. 掌握BFS与队列

  3. 掌握DFS和递归

  4. 熟悉并理解回溯问题

实验内容:

1.八数码问题:

在一个3×3的棋盘上,放置编号为1~8的8个方块,每个占一格,另外还有一个空格。与空格相邻的数字方块可以移动到空格里。

任务1:指定初始棋局和目标棋局,计算出最少的移动步数;

任务2:输出数码的移动序列。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

string Start, End;  // 定义起始状态和目标状态字符串
int dx[] = { 1, -1, 0, 0 }, dy[] = { 0, 0, 1, -1 };  // 定义四个方向的偏移量
int stepCount = 0;  // 记录步数

int bfs()
{
    queue<string> q;  // 使用队列进行广度优先搜索
    map<string, int> mp;  // 使用哈希表记录每个状态的步数
    mp[Start] = 0;
    q.push(Start);
    while (!q.empty())
    {
        Start = q.front();
        cout << Start << endl;  // 输出当前状态
        q.pop();
        stepCount = mp[Start];
        int FormerX, FormerY, FormerLocation;
        FormerLocation = Start.find('.');  // 找到空格的位置
        FormerX = FormerLocation / 3;  // 计算空格所在的行
        FormerY = FormerLocation % 3;  // 计算空格所在的列
        for (int i = 0; i < 4; i++)
        {
            int nowX = FormerX + dx[i];
            int nowY = FormerY + dy[i];
            if (nowX > 2 || nowX < 0 || nowY > 2 || nowY < 0) // 判断是否越界
                continue;
            int NewLocation = nowX * 3 + nowY;
            swap(Start[NewLocation], Start[FormerLocation]);  // 交换空格和相邻位置的数字
            if (!mp.count(Start))
            {
                mp[Start] = stepCount + 1;
                
                if (Start == End)  // 判断是否达到目标状态
                    return mp[Start];
                
                q.push(Start);  // 将当前状态加入队列
            }
            swap(Start[NewLocation], Start[FormerLocation]);  // 恢复原始状态
        }
    }
	
    return -1;  // 如果无法到达目标状态,返回-1
}

int main()
{
    // 请在此输入您的代码
    cin >> Start >> End;  // 输入起始状态和目标状态
    cout << "Moving sequence: " << endl;
    cout << bfs() << endl;  // 调用bfs函数并输出结果
    return 0;
}
/*
test:
12345678.
123.46758
*/

【运行结果】

2.八皇后问题:

在棋盘上放置8个皇后,使它们不同行、不同列、不同对角线。问有多少种合法的情况。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

int mapcolumn[9] = {0};
int situation = 0;

bool check(int row, int column)
{
	for(int i=0; i<row; i++)
	{
		if(mapcolumn[i] == column || abs(i-row) == abs(mapcolumn[i]-column))
			return false;
	}
	return true;
}

void dfs(int row)
{
	if(row == 8)
	{
		situation++;
		return;
	}	
	for(int column=0; column<8; column++)
	{
		if(check(row, column))
		{
			mapcolumn[row] = column;
			dfs(row+1);
		}
	}	
}

int main()
{
	dfs(0);
	cout << situation << endl;
}

【运行结果】

相关推荐
索迪迈科技20 小时前
Flink Task线程处理模型:Mailbox
java·大数据·开发语言·数据结构·算法·flink
元亓亓亓21 小时前
LeetCode热题100--230. 二叉搜索树中第 K 小的元素--中等
算法·leetcode·职场和发展
草莓熊Lotso21 小时前
《算法闯关指南:优选算法-双指针》--01移动零,02复写零
c语言·c++·经验分享·算法·leetcode
焜昱错眩..1 天前
代码随想录算法训练营第三十九天|62.不同路径 63.不同路径ll
算法
ajassi20001 天前
开源 C++ QT Widget 开发(十五)多媒体--音频播放
linux·c++·qt·开源
焦耳加热1 天前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn1 天前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
u6061 天前
常用排序算法核心知识点梳理
算法·排序
鹅毛在路上了1 天前
C++, ffmpeg, libavcodec-RTSP拉流,opencv实时预览
c++·opencv·ffmpeg
John_ToDebug1 天前
定制 ResourceBundle 的实现与 DuiLib 思想在 Chromium 架构下的应用解析
c++·chrome·ui