博客
关于我
洛谷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/

    你可能感兴趣的文章
    python一个一个获取迭代器的元素
    查看>>
    python一维数组 提取_python – 如何从numpy中的1维数组中提取零维切片
    查看>>
    python三大库之--numpy(一)
    查看>>
    python三大库之--numpy(二)
    查看>>
    python上的字无效字符_标识中的无效字符
    查看>>
    python下用decode和encode 中文转码
    查看>>
    Python下调用Linux的Shell命令
    查看>>
    Python接口自动化测试实战系列
    查看>>
    python下载_安装_配置_以及第一行python程序---python工作笔记009
    查看>>
    Python不协调机器人消息.内容
    查看>>
    Python与C#之间内存共享互传信息
    查看>>
    python与c语言混合编程_你试过C语言和Python一起混合编程吗?两者相加不是已经无敌了!...
    查看>>
    python与excel
    查看>>
    Python与Excel的完美邂逅:深入浅出,手把手教你轻松玩转Excel数据操作
    查看>>
    python与flask_Python编程中flask的简介与简单使用
    查看>>
    python与图灵机器人交互(WXPY版本)
    查看>>
    python与运算符_python运算符
    查看>>
    python两个列表获取交集,并集,差集
    查看>>
    python两个字典合并,两个list合并
    查看>>
    Python两个版本共存时,命令行升级pip
    查看>>