Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)

Posted Asurudo Jyo の 倉 庫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)相关的知识,希望对你有一定的参考价值。

 1 typedef pair<int,int> P;
 2 typedef long long ll;
 3 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
 4 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
 5 #define INF 0x3f3f3f3f
 6 #define MOD 100000000
 7 #define maxn 1003
 8 #define pb push_back
 9 #define debug() printf("Miku Check OK!
")
10 
11 
12 class Solution
13 {
14     public:
15         int dx[8] = {-1,-1,-1,0,0,1,1,1};
16         int dy[8] = {-1,0,1,-1,1,-1,0,1};
17         vector<vector<int>> rnt;
18         vector<vector<int>> q;
19         int vis[10][10];
20         bool isexist(int x,int y)
21         {
22             _for(i,0,q.size())
23                 if(q[i][0]==x && q[i][1]==y)
24                     return true;
25             return false;
26         }
27         bool valid(int x,int y)
28         {
29             return x>=0 && y>=0 && x<8 && y<8;
30         }
31         void dfs(int x,int y,int type)
32         {
33             int nx = x+dx[type];
34             int ny = y+dy[type];
35             if(valid(nx,ny) && isexist(nx,ny))
36                 rnt.pb({nx,ny});
37             else if(valid(nx,ny))
38                 dfs(nx,ny,type);
39         }
40         vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king)
41         {
42             q = queens;
43             dfs(king[0],king[1],0);
44             dfs(king[0],king[1],1);
45             dfs(king[0],king[1],2);
46             dfs(king[0],king[1],3);
47             dfs(king[0],king[1],4);
48             dfs(king[0],king[1],5);
49             dfs(king[0],king[1],6);
50             dfs(king[0],king[1],7);
51             return rnt;
52         }
53 };

 

以上是关于Leetcode-5223 Queens That Can Attack the King(可以攻击国王的皇后)的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 1222. Queens That Can Attack the King 可以攻击国王的皇后

[Lintcode]33. N-Queens/[Leetcode]51. N-Queens

51. N-Queens

[leetcode]N-Queens II

安装OpenStack QUEENS版本一:基础环境

N-Queens 回溯/递归逻辑错误?