-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1222.cs
44 lines (39 loc) · 1.19 KB
/
Solution1222.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1222
{
/// <summary>
/// 1222. Queens That Can Attack the King - Medium
/// <a href="https://leetcode.com/problems/queens-that-can-attack-the-king">See the problem</a>
/// </summary>
public IList<IList<int>> QueensAttacktheKing(int[][] queens, int[] king)
{
var result = new List<IList<int>>();
var queenSet = new HashSet<string>();
foreach (var queen in queens)
{
queenSet.Add($"{queen[0]},{queen[1]}");
}
int[][] directions = [
[0, 1], [1, 0], [0, -1], [-1, 0],
[1, 1], [-1, -1], [-1, 1], [1, -1]
];
foreach (var direction in directions)
{
int x = king[0], y = king[1];
while (true)
{
x += direction[0];
y += direction[1];
if (x < 0 || x >= 8 || y < 0 || y >= 8) break;
if (queenSet.Contains($"{x},{y}"))
{
result.Add(new List<int> { x, y });
break;
}
}
}
return result;
}
}