# LeetCode - 1958. Check if Move is Legal ### 題目網址:https://leetcode.com/problems/check-if-move-is-legal/ ###### tags: `LeetCode` `Medium` `模擬` `陣列` ```cpp= /* -LeetCode format- Problem: 1958. Check if Move is Legal Difficulty: Medium by Inversionpeter */ #define INSIDE(r, c) ( 0 <= r && r < 8 && 0 <= c && c < 8 ) int dr[8] = { -1, -1, -1, 0, 1, 1, 1, 0 }, dc[8] = { -1, 0, 1, 1, 1, 0, -1, -1 }; class Solution { public: bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) { int newRow, newColumn, length; board[rMove][cMove] = color; for (int i = 0; i < 8; ++i) { length = 2; newRow = rMove + dr[i]; newColumn = cMove + dc[i]; while (INSIDE(newRow, newColumn) && board[newRow][newColumn] != '.' && board[newRow][newColumn] != color) { newColumn += dc[i]; newRow += dr[i]; ++length; } if (length >= 3 && INSIDE(newRow, newColumn) && board[newRow][newColumn] == color) return true; } return false; } }; ```