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

目的:

  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;
}

【运行结果】

相关推荐
Source.Liu5 分钟前
【CMakeLists.txt】QtSvg 头文件包含配置详解
c++·qt·librecad
郝学胜-神的一滴7 分钟前
Linux中的`fork`函数详解:深入解析
linux·服务器·开发语言·c++·算法
penguin_bark18 分钟前
C++调用MySQL数据库完整教程
数据库·c++·mysql
大数据张老师1 小时前
数据结构——BF算法
数据结构·算法·1024程序员节
让我们一起加油好吗1 小时前
【数论】欧拉定理 && 扩展欧拉定理
c++·算法·数论·1024程序员节·欧拉定理·欧拉降幂·扩展欧拉定理
Yupureki1 小时前
从零开始的C++学习生活 14:map/set的使用和封装
c语言·数据结构·c++·学习·visual studio·1024程序员节
一匹电信狗1 小时前
【LeetCode_876_2.02】快慢指针在链表中的简单应用
c语言·数据结构·c++·算法·leetcode·链表·stl
keineahnung23451 小时前
C++中的Aggregate initialization
c++·1024程序员节
胖咕噜的稞达鸭1 小时前
算法入门---专题二:滑动窗口2(最大连续1的个数,无重复字符的最长子串 )
c语言·数据结构·c++·算法·推荐算法·1024程序员节
兮山与1 小时前
算法18.0
算法