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

目的:

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

【运行结果】

相关推荐
Forget the Dream3 分钟前
设计模式之迭代器模式
java·c++·设计模式·迭代器模式
️Carrie️29 分钟前
10.2 继承与多态
c++·多态·继承
卑微小文33 分钟前
2025国内网络反爬新高度:代理IP智能轮换算法揭秘
后端·算法·架构
Nicole Potter36 分钟前
内存泄漏出现的时机和原因,如何避免?
c++·游戏·面试·c#
却道天凉_好个秋41 分钟前
c++ 嵌入汇编的方式实现int型自增
开发语言·汇编·c++
tyler-泰勒2 小时前
c++:迭代器的失效
开发语言·c++
决斗小饼干2 小时前
震惊!C++程序真的从main开始吗?99%的程序员都答错了
c++
辰尘_星启2 小时前
【vscode】一键编译运行c/c++程序
c语言·c++·vscode·debug·cmake
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧2 小时前
C语言_数据结构总结7:顺序队列(循环队列)
c语言·开发语言·数据结构·算法·visualstudio·visual studio
LIUJH12332 小时前
数据结构——单调栈
开发语言·数据结构·c++·算法