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

目的:

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

【运行结果】

相关推荐
zylyehuo1 小时前
C++基础编程
c++
猿究院--王升2 小时前
jvm三色标记
java·jvm·算法
一车小面包2 小时前
逻辑回归 从0到1
算法·机器学习·逻辑回归
tt5555555555552 小时前
C/C++嵌入式笔试核心考点精解
c语言·开发语言·c++
lg_cool_3 小时前
Qt 中最经典、最常用的多线程通信场景
c++·qt6.3
科大饭桶3 小时前
C++入门自学Day14-- Stack和Queue的自实现(适配器)
c语言·开发语言·数据结构·c++·容器
tt5555555555553 小时前
字符串与算法题详解:最长回文子串、IP 地址转换、字符串排序、蛇形矩阵与字符串加密
c++·算法·矩阵
元亓亓亓4 小时前
LeetCode热题100--101. 对称二叉树--简单
算法·leetcode·职场和发展
rainFFrain5 小时前
Boost搜索引擎项目(详细思路版)
网络·c++·http·搜索引擎
不会学习?5 小时前
算法03 归并分治
算法