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!\n")
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 };

 

posted @ 2019-10-13 12:10  Asurudo  阅读(202)  评论(0编辑  收藏  举报