Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1222 #1016

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 17 additions & 14 deletions leetcode/1201-1300/1222.Queens-That-Can-Attack-the-King/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# [1222.Queens That Can Attack the King][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
On a **0-indexed** `8 x 8` chessboard, there can be multiple black queens and one white king.

You are given a 2D integer array `queens` where `queens[i] = [xQueeni, yQueeni]` represents the position of the `ith` black queen on the chessboard. You are also given an integer array `king` of length `2` where `king = [xKing, yKing]` represents the position of the white king.

Return the coordinates of the black queens that can directly attack the king. You may return the answer in **any order**.

**Example 1:**
**Example 1:**

![1](./1.jpg)

```
Input: a = "11", b = "1"
Output: "100"
Input: queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
Output: [[0,1],[1,0],[3,3]]
Explanation: The diagram above shows the three queens that can directly attack the king and the three queens that cannot attack the king (i.e., marked with red dashes).
```

## 题意
> ...
**Example 2:**

## 题解
![2](./2.jpg)

### 思路1
> ...
Queens That Can Attack the King
```go
```

Input: queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
Output: [[2,2],[3,4],[4,4]]
Explanation: The diagram above shows the three queens that can directly attack the king and the three queens that cannot attack the king (i.e., marked with red dashes).
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(queens [][]int, king []int) [][]int {
ans := make([][]int, 0)
board := [8][8]int{}
for _, q := range queens {
board[q[0]][q[1]] = 1
}
x, y := king[0], king[1]
for r := y - 1; r >= 0; r-- {
if board[x][r] == 1 {
ans = append(ans, []int{x, r})
break
}
}
for r := y + 1; r < 8; r++ {
if board[x][r] == 1 {
ans = append(ans, []int{x, r})
break
}
}

for r := x - 1; r >= 0; r-- {
if board[r][y] == 1 {
ans = append(ans, []int{r, y})
break
}
}

for r := x + 1; r < 8; r++ {
if board[r][y] == 1 {
ans = append(ans, []int{r, y})
break
}
}

// 对
for l, r := x-1, y-1; l >= 0 && r >= 0; l, r = l-1, r-1 {
if board[l][r] == 1 {
ans = append(ans, []int{l, r})
break
}
}

for l, r := x-1, y+1; l >= 0 && r < 8; l, r = l-1, r+1 {
if board[l][r] == 1 {
ans = append(ans, []int{l, r})
break
}
}

for l, r := x+1, y+1; l < 8 && r < 8; l, r = l+1, r+1 {
if board[l][r] == 1 {
ans = append(ans, []int{l, r})
break
}
}

for l, r := x+1, y-1; l < 8 && r >= 0; l, r = l+1, r-1 {
if board[l][r] == 1 {
ans = append(ans, []int{l, r})
break
}
}

return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
queens [][]int
king []int
expect [][]int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{0, 1}, {1, 0}, {4, 0}, {0, 4}, {3, 3}, {2, 4}}, []int{0, 0}, [][]int{{0, 1}, {1, 0}, {3, 3}}},
{"TestCase2", [][]int{{0, 0}, {1, 1}, {2, 2}, {3, 4}, {3, 5}, {4, 4}, {4, 5}}, []int{3, 3}, [][]int{{3, 4}, {2, 2}, {4, 4}}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.queens, c.king)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.queens, c.king)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading