博客
关于我
洛谷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 gil_Python中GIL的使用详解
    查看>>
    python glob.glob使用
    查看>>
    python glob的安装和使用
    查看>>
    Python google drive API 下载,文件在哪里?
    查看>>
    Python GPS 模块:读取最新的 GPS 数据
    查看>>
    python grpc入门
    查看>>
    python gRPC测试helloworld
    查看>>
    python gRPC简单示例
    查看>>
    Python GUI 开发:全面指南
    查看>>
    Python hashlib模块
    查看>>
    python hashlib模块
    查看>>
    python if,循环的练习
    查看>>
    Python in open course (week3)
    查看>>
    Python IO编程
    查看>>
    python join split
    查看>>
    Python Kivy ListView:如何删除选定的 ListItemButton?
    查看>>
    Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll
    查看>>
    Python ldap3 代码从 SID 获取用户名
    查看>>
    Python list += iterable 的行为是否记录在任何地方?
    查看>>