1
1
//! 模拟类题目
2
- //!
2
+ //!
3
3
//! 题目
4
4
//! * 简单
5
5
//! * 中等
6
+ //! * [2596. Check Knight Tour Configuration](check_valid_grid)
7
+ //! * [1222. Queens That Can Attack the King](queens_attackthe_king)
6
8
//! * 困难
7
- //!
9
+ //!
8
10
9
-
10
- /// [2596. Check Knight Tour Configuration](https://leetcode.cn/problems/check-knight-tour-configuration/description)
11
+ /// [2596. Check Knight Tour Configuration](https://leetcode.cn/problems/check-knight-tour-configuration/)
11
12
///
12
13
/// 思路:
13
14
/// 1. 先根据config, 产出一个`(step, x, y)`的数组
@@ -63,12 +64,93 @@ pub fn check_valid_grid(grid: Vec<Vec<i32>>) -> bool {
63
64
true
64
65
}
65
66
67
+ /// [1222. Queens That Can Attack the King](https://leetcode.cn/problems/queens-that-can-attack-the-king/)
68
+ ///
69
+ /// 象棋规则: Queen可以横, 竖, 斜走, 但是不能跨越棋子
70
+ /// 思路:
71
+ /// 1. 从king的位置开始, 向8个方向遍历, 找到第一个queen
72
+ pub fn queens_attackthe_king ( queens : Vec < Vec < i32 > > , king : Vec < i32 > ) -> Vec < Vec < i32 > > {
73
+ use std:: collections:: HashSet ;
74
+ let queens = queens. into_iter ( ) . collect :: < HashSet < Vec < i32 > > > ( ) ;
75
+ let mut result = vec ! [ ] ;
76
+
77
+ let ( x, y) = ( king[ 0 ] , king[ 1 ] ) ;
78
+
79
+ for dir in [
80
+ ( 0 , 1 ) ,
81
+ ( 1 , 0 ) ,
82
+ ( 0 , -1 ) ,
83
+ ( -1 , 0 ) ,
84
+ ( 1 , 1 ) ,
85
+ ( -1 , -1 ) ,
86
+ ( -1 , 1 ) ,
87
+ ( 1 , -1 ) ,
88
+ ]
89
+ . iter ( )
90
+ {
91
+ let ( mut x, mut y) = ( x, y) ;
92
+ loop {
93
+ x += dir. 0 ;
94
+ y += dir. 1 ;
95
+ if x < 0 || y < 0 || x >= 8 || y >= 8 {
96
+ break ;
97
+ }
98
+ if queens. contains ( & vec ! [ x, y] ) {
99
+ result. push ( vec ! [ x, y] ) ;
100
+ break ;
101
+ }
102
+ }
103
+ }
104
+ result
105
+ }
66
106
67
107
#[ cfg( test) ]
68
- mod tests{
108
+ mod tests {
69
109
use super :: * ;
70
110
use crate :: vec2;
71
111
112
+ #[ test]
113
+ fn test_queens_attackthe_king ( ) {
114
+ struct Testcase {
115
+ queens : Vec < Vec < i32 > > ,
116
+ king : Vec < i32 > ,
117
+ expect : Vec < Vec < i32 > > ,
118
+ }
119
+
120
+ vec ! [
121
+ Testcase {
122
+ queens: vec2![ [ 0 , 1 ] , [ 1 , 0 ] , [ 4 , 0 ] , [ 0 , 4 ] , [ 3 , 3 ] , [ 2 , 4 ] ] ,
123
+ king: vec![ 0 , 0 ] ,
124
+ expect: vec2![ [ 0 , 1 ] , [ 1 , 0 ] , [ 3 , 3 ] ] ,
125
+ } ,
126
+ Testcase {
127
+ queens: vec2![ [ 0 , 0 ] , [ 1 , 1 ] , [ 2 , 2 ] , [ 3 , 4 ] , [ 3 , 5 ] , [ 4 , 4 ] , [ 4 , 5 ] ] ,
128
+ king: vec![ 3 , 3 ] ,
129
+ expect: vec2![ [ 2 , 2 ] , [ 3 , 4 ] , [ 4 , 4 ] ] ,
130
+ } ,
131
+ ]
132
+ . into_iter ( )
133
+ . enumerate ( )
134
+ . for_each (
135
+ |(
136
+ idx,
137
+ Testcase {
138
+ queens,
139
+ king,
140
+ expect,
141
+ } ,
142
+ ) | {
143
+ use std:: collections:: HashSet ;
144
+ let actual = queens_attackthe_king ( queens, king) ;
145
+
146
+ let actual = actual. into_iter ( ) . collect :: < HashSet < Vec < i32 > > > ( ) ;
147
+ let expect = expect. into_iter ( ) . collect :: < HashSet < Vec < i32 > > > ( ) ;
148
+
149
+ assert_eq ! ( expect, actual, "case {} failed" , idx) ;
150
+ } ,
151
+ )
152
+ }
153
+
72
154
#[ test]
73
155
fn test_check_valid_grid ( ) {
74
156
struct Testcase {
@@ -99,4 +181,4 @@ mod tests{
99
181
assert_eq ! ( expect, actual, "case {} failed" , idx) ;
100
182
} )
101
183
}
102
- }
184
+ }
0 commit comments