博客
关于我
洛谷P1219 :八皇后(DFS+回溯)
阅读量:209 次
发布时间:2019-02-28

本文共 1386 字,大约阅读时间需要 4 分钟。

??????????????????????????????????????????????????????????????????????????????????????????

????

  • ???????????????????????????????????N x N???????N!????
  • ?????????????????1?-1????????????????????????????
  • ???????DFS???????DFS?????????????????????????????
  • ???????????????????????????????????????????????????
  • ????

    #include 
    #include
    using namespace std;int n;int a[100], b[100], c[100], d[100];int way[100];int ans = 0;void print() { if (ans < 3) { for (int i = 1; i <= n; ++i) { cout << a[i] << " "; } cout << endl; } ans++;}void dfs(int i) { if (i > n) { print(); return; } for (int j = 1; j <= n; ++j) { if (b[j] == 0 && c[i + j] == 0 && d[i - j + n] == 0) { a[i] = j; b[j] = 1; c[i + j] = 1; d[i - j + n] = 1; dfs(i + 1); a[i] = 0; b[j] = 0; c[i + j] = 0; d[i - j + n] = 0; } }}int main() { cin >> n; // ????????0 ms(a, 0); ms(b, 0); ms(c, 0); ms(d, 0); dfs(1); cout << ans << endl;}

    ????

  • ???????????N?
  • ????????ms???????a?b?c?d?0?a????????b????????c?d????????
  • DFS?????dfs(int i)??????????????????????????????????
  • ?????????????????????????????????????
  • ???????????????????print()????????????????
  • ?????????????????
  • ????????????????????????????????????N??6 ? N ? 13??

    转载地址:http://vibp.baihongyu.com/

    你可能感兴趣的文章
    Open-Sora代码详细解读(2):时空3D VAE
    查看>>
    Open-Source Service Discovery
    查看>>
    open-vm-tools-dkms : 依赖: open-vm-tools (>= 2:9.4.0-1280544-5ubuntu3) 但是它将不会被安装
    查看>>
    open3d-Dll缺失,未找到指定模块解决
    查看>>
    openai Midjourney代理服务 gpt大模型第三方api平台汇总 支持国内外各种大模型 持续更新中...
    查看>>
    OpenAll:Android打开组件新姿势【仅供用于学习了解ButterKnife框架基本原理】
    查看>>
    OpenASR 项目使用教程
    查看>>
    Openbox-桌面图标设置
    查看>>
    opencart出现no such file or dictionary
    查看>>
    OpenCV 3.1 imwrite()函数写入异常问题解决方法
    查看>>
    OpenCV 4.1.0版drawContours
    查看>>
    Opencv cv2.putText 函数详解
    查看>>
    opencv glob 内存溢出异常
    查看>>
    opencv Hog Demo
    查看>>
    opencv Hog学习总结
    查看>>
    opencv Mat push_back
    查看>>
    opencv putText中文乱码
    查看>>
    OpenCV Python围绕特定点将图像旋转X度
    查看>>
    opencv resize
    查看>>
    Opencv Sift和Surf特征实现图像无缝拼接生成全景图像
    查看>>