|
59 | 59 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 60 |
|
61 | 61 | ```python
|
62 |
| - |
| 62 | +class Solution: |
| 63 | + def checkMove(self, board: List[List[str]], rMove: int, cMove: int, color: str) -> bool: |
| 64 | + dirs = [(1, 0), (0, 1), (-1, 0), (0, -1), |
| 65 | + (1, 1), (1, -1), (-1, 1), (-1, -1)] |
| 66 | + n = 8 |
| 67 | + for a, b in dirs: |
| 68 | + i, j = rMove, cMove |
| 69 | + t = 0 |
| 70 | + while 0 <= i + a < n and 0 <= j + b < n: |
| 71 | + t += 1 |
| 72 | + i, j = i + a, j + b |
| 73 | + if board[i][j] in ['.', color]: |
| 74 | + break |
| 75 | + if board[i][j] == color and t > 1: |
| 76 | + return True |
| 77 | + return False |
63 | 78 | ```
|
64 | 79 |
|
65 | 80 | ### **Java**
|
66 | 81 |
|
67 | 82 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
68 | 83 |
|
69 | 84 | ```java
|
| 85 | +class Solution { |
| 86 | + private static final int[][] DIRS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; |
| 87 | + private static final int N = 8; |
| 88 | + |
| 89 | + public boolean checkMove(char[][] board, int rMove, int cMove, char color) { |
| 90 | + for (int[] d : DIRS) { |
| 91 | + int i = rMove, j = cMove; |
| 92 | + int t = 0; |
| 93 | + int a = d[0], b = d[1]; |
| 94 | + while (0 <= i + a && i + a < N && 0 <= j + b && j + b < N) { |
| 95 | + ++t; |
| 96 | + i += a; |
| 97 | + j += b; |
| 98 | + if (board[i][j] == '.' || board[i][j] == color) { |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + if (board[i][j] == color && t > 1) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +### **C++** |
| 112 | + |
| 113 | +```cpp |
| 114 | +class Solution { |
| 115 | +public: |
| 116 | + vector<vector<int>> dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; |
| 117 | + int n = 8; |
| 118 | + |
| 119 | + bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) { |
| 120 | + for (auto& d : dirs) |
| 121 | + { |
| 122 | + int a = d[0], b = d[1]; |
| 123 | + int i = rMove, j = cMove; |
| 124 | + int t = 0; |
| 125 | + while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n) |
| 126 | + { |
| 127 | + ++t; |
| 128 | + i += a; |
| 129 | + j += b; |
| 130 | + if (board[i][j] == '.' || board[i][j] == color) break; |
| 131 | + } |
| 132 | + if (board[i][j] == color && t > 1) return true; |
| 133 | + } |
| 134 | + return false; |
| 135 | + } |
| 136 | +}; |
| 137 | +``` |
70 | 138 |
|
| 139 | +### **Go** |
| 140 | + |
| 141 | +```go |
| 142 | +func checkMove(board [][]byte, rMove int, cMove int, color byte) bool { |
| 143 | + dirs := [8][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}} |
| 144 | + n := 8 |
| 145 | + for _, d := range dirs { |
| 146 | + a, b := d[0], d[1] |
| 147 | + i, j := rMove, cMove |
| 148 | + t := 0 |
| 149 | + for 0 <= i+a && i+a < n && 0 <= j+b && j+b < n { |
| 150 | + t++ |
| 151 | + i += a |
| 152 | + j += b |
| 153 | + if board[i][j] == '.' || board[i][j] == color { |
| 154 | + break |
| 155 | + } |
| 156 | + } |
| 157 | + if board[i][j] == color && t > 1 { |
| 158 | + return true |
| 159 | + } |
| 160 | + } |
| 161 | + return false |
| 162 | +} |
71 | 163 | ```
|
72 | 164 |
|
73 | 165 | ### **...**
|
|
0 commit comments