51. N-Queens

Problem's Link

----------------------------------------------------------------------------

Mean:

N-Queen问题.

analyse:

dfs基本功.

Time complexity: O(N)

view code

1.第一发超时了,回过头来看看自己像shi一样的代码也是醉了.

);
       );
           ; )) --x,++y;
       while(x<n && y)
       {
           if(mat[x][y]=='Q' && (!(x==row && y==col)))
               return false;
           ++x,--y;
       }
       return true;
   }
   void __init(int n)
   {
       res.clear();
       mat=*(new vector<string>(n,string(n,'.')));
   }
private:
   vector<string> mat;
   vector<vector<string>> res;
};

2.优化了dfs调用和check()函数,效率提升了一个档次.

; ; ; ;; && j<n;--i,++j)
           if(mat[i][j]=='Q')
               return false;
       return true;
   }
   void __init(int n)
   {
       res.clear();
       mat=*(new vector<string>(n,string(n,'.')));
   }
private:
   vector<string> mat;
   vector<vector<string>> res;
};

LeetCode - 51. N-Queens的更多相关文章

  1. [LeetCode] 51. N-Queens N皇后问题

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  2. LeetCode: 51. N-Queens(Medium)

    1. 原题链接 https://leetcode.com/problems/n-queens/description/ 2. 题目要求 游戏规则:当两个皇后位于同一条线上时(同一列.同一行.同一45度 ...

  3. 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  4. leetcode@ [51/52] N-Queens

    https://leetcode.com/problems/n-queens/ class Solution { public: void dfs(vector<vector<string ...

  5. [leetcode]51. N-QueensN皇后

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  6. leetcode 51. N-Queens 、52. N-Queens II

    51. N-Queens 使用isValid判断当前的位置是否合法 每次遍历一行,使用queenCol记录之前行的存储位置,一方面是用于判断合法,另一方面可以根据存储结果输出最终的结果 class S ...

  7. LeetCode 51. N-QueensN皇后 (C++)(八皇后问题)

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  8. 【leetcode】1222. Queens That Can Attack the King

    题目如下: On an 8x8 chessboard, there can be multiple Black Queens and one White King. Given an array of ...

  9. Java实现 LeetCode 51 N皇后

    51. N皇后 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后问题的解决 ...

随机推荐

  1. 继续送假期干货——响应式图片工具smartImg

    中午看<众妙之门>看到一个响应式图片处理工具(点此查看)的介绍,然后就心血来潮想着不妨自己写一个基于JQ的吧,于是就又有了这么一个干货给大家. smartImg 的全部文件可以从我的Git ...

  2. SWT: 发起事件 post event

    有很多学习SWT的同志遇到过一类需求,为某些控件添加了诸如MouseListener.KeyListener之类的监听,然后呢,希望使用代码模拟鼠标.键盘来执行点击.按键等操作. 首先说明一点,这是可 ...

  3. android知识杂记(一)

    记录项目中用的零碎知识点,用以备忘. android:screenOrientation:portrait 限制横屏 activity启动状态 singleTop 只执行一次,通常用在欢迎页面 sin ...

  4. FusionCharts简单教程(一)---建立第一个FusionCharts图形

    由于项目需求需要做一个报表,选择FusionCharts作为工具使用.由于以前没有接触过报表,网上也没有比较详细的fusionCharts教程,所以决定好好研究FusionCharts,同时做一个比较 ...

  5. Hibernate的Annotation注解

    当项目变得比较大的时候,如何还使用hbm.xml文件来配置Hibernate实体就会变得比较复杂.这里Hibernate提供了Annotation注解方式,使得Hibernate的映射文件变得很方便管 ...

  6. Android多线程分析之一:使用Thread异步下载图像

    Android多线程分析之一:使用Thread异步下载图像 罗朝辉 (http://www.cnblogs.com/kesalin) CC 许可,转载请注明出处   打算整理一下对 Android F ...

  7. EF架构~EF6配置需要注意的几个地方

    回到目录 从EF5升级到EF6之后,可能会出现一些问题,这是正常的,任何系统的升级都没有一帆风顺的,当然这些不是我要说的重点,我真正要说的是,当出现这些问题时,我们应该如何去应对它,下面我总结了几个方 ...

  8. Atitit dsl exer v3 qb3 新特性

    Atitit dsl exer v3 qb3 新特性 /atiplat_cms/src/com/attilax/dsl/DslParser.java V3 支持typeed参数,与简化的notyp参数 ...

  9. Atitit 开发2d游戏的技术选型attilax总结

    Atitit 开发2d游戏的技术选型attilax总结 1.1. 跨平台跨平台:一定要使用跨平台的gui技术,目前最好的就是h5(canvas,webgl,dom) +js了..1 1.2. 游戏前后 ...

  10. Dapper sql in

    应用场景: 使用 sql的 Case When Then 批量更新某张表,底层数据库用到了Dapper 代码示例: public int UpdateClientReceivedResult(Dict ...