<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 騎士的可能舉動 > 原文: [https://www.geeksforgeeks.org/possible-moves-knight/](https://www.geeksforgeeks.org/possible-moves-knight/) 給定尺寸為 m * n 的棋盤。 查找騎士可以從給定位置在棋 board 上移動的可能動作數。 如果 mat [i] [j] = 1,則該塊將填充其他內容,否則為空。 假設該棋盤由所有相同顏色的棋子組成,即沒有被攻擊的方塊。 **示例**: ``` Input : mat[][] = {{1, 0, 1, 0}, {0, 1, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 1}} pos = (2, 2) Output : 4 Knight can moved from (2, 2) to (0, 1), (0, 3), (1, 0) ans (3, 0). ``` 我們可以觀察到騎士在棋盤上移動了: 1.水平移動 2 步,垂直移動 2.垂直移動 2 步,水平移動 1 步 這個想法是存儲所有可能的騎士動作,然后計算有效動作的數量。 在以下情況下,移動將無效: 1.一個塊已被另一塊占用。 2.移動超出了棋盤。 ## C++ ```cpp // CPP program to find number of possible moves of knight #include <bits/stdc++.h> #define n 4 #define m 4 using namespace std; // To calculate possible moves int findPossibleMoves(int mat[n][m], int p, int q) { ????// All possible moves of a knight ????int X[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; ????int Y[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; ????int count = 0; ????// Check if each possible move is valid or not ????for (int i = 0; i < 8; i++) { ????????// Position of knight after move ????????int x = p + X[i]; ????????int y = q + Y[i]; ????????// count valid moves ????????if (x >= 0 && y >= 0 && x < n && y < m ????????????&& mat[x][y] == 0) ????????????count++; ????} ????// Return number of possible moves ????return count; } // Driver program to check findPossibleMoves() int main() { ????int mat[n][m] = { { 1, 0, 1, 0 }, ??????????????????????{ 0, 1, 1, 1 }, ??????????????????????{ 1, 1, 0, 1 }, ??????????????????????{ 0, 1, 1, 1 } }; ????int p = 2, q = 2; ????cout << findPossibleMoves(mat, p, q); ????return 0; } ``` ## Java ```java // Java program to find number of possible moves of knight public class Main { public static final int n = 4; public static final int m = 4; ????// To calculate possible moves ????static int findPossibleMoves(int mat[][], int p, int q) ????{ ????????// All possible moves of a knight ????????int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 }; ????????int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 }; ????????int count = 0; ????????// Check if each possible move is valid or not ????????for (int i = 0; i < 8; i++) { ????????????// Position of knight after move ????????????int x = p + X[i]; ????????????int y = q + Y[i]; ????????????// count valid moves ????????????if (x >= 0 && y >= 0 && x < n && y < m ????????????????&& mat[x][y] == 0) ????????????????count++; ????????} ????????// Return number of possible moves ????????return count; ????} ????// Driver program to check findPossibleMoves() ????public static void main(String[] args) ????{ ????????int mat[][] = { { 1, 0, 1, 0 }, ????????????????????????{ 0, 1, 1, 1 }, ????????????????????????{ 1, 1, 0, 1 }, ????????????????????????{ 0, 1, 1, 1 } }; ????????int p = 2, q = 2; ????????System.out.println(findPossibleMoves(mat, p, q)); ????} } ``` ## Python3 ```py # Python3 program to find number # of possible moves of knight n = 4; m = 4; # To calculate possible moves def findPossibleMoves(mat, p, q): ????global n, m; ????# All possible moves of a knight ????X = [2, 1, -1, -2, -2, -1, 1, 2]; ????Y = [1, 2, 2, 1, -1, -2, -2, -1]; ????count = 0; ????# Check if each possible move ????# is valid or not ????for i in range(8): ????????# Position of knight after move ????????x = p + X[i]; ????????y = q + Y[i]; ????????# count valid moves ????????if(x >= 0 and y >= 0 and x < n and ???????????y < m and mat[x][y] == 0): ????????????count += 1; ????# Return number of possible moves ????return count; # Driver code if __name__ == '__main__': ????mat = [[1, 0, 1, 0], [0, 1, 1, 1],? ???????????[1, 1, 0, 1], [0, 1, 1, 1]]; ????p, q = 2, 2; ????print(findPossibleMoves(mat, p, q)); # This code is contributed by 29AjayKumar ``` ## C# ```cs // C# program to find number of // possible moves of knight using System; class GFG { ????static int n = 4; ????static int m = 4; ????// To calculate? ????// possible moves ????static int findPossibleMoves(int [,]mat,? ?????????????????????????????????int p, int q) ????{ ????????// All possible moves ????????// of a knight ????????int []X = { 2, 1, -1, -2, ???????????????????-2, -1, 1, 2 }; ????????int []Y = { 1, 2, 2, 1,? ???????????????????-1, -2, -2, -1 }; ????????int count = 0; ????????// Check if each possible ????????// move is valid or not ????????for (int i = 0; i < 8; i++) ????????{ ????????????// Position of knight ????????????// after move ????????????int x = p + X[i]; ????????????int y = q + Y[i]; ????????????// count valid moves ????????????if (x >= 0 && y >= 0 &&? ????????????????x < n && y < m &&? ????????????????mat[x, y] == 0) ????????????????count++; ????????} ????????// Return number? ????????// of possible moves ????????return count; ????} ????// Driver Code ????static public void Main () ????{ ????????int [,]mat = { { 1, 0, 1, 0 }, ???????????????????????{ 0, 1, 1, 1 }, ???????????????????????{ 1, 1, 0, 1 }, ???????????????????????{ 0, 1, 1, 1 }}; ????????int p = 2, q = 2; ????????Console.WriteLine(findPossibleMoves(mat,? ????????????????????????????????????????????p, q)); ????} } // This code is contributed by m_kit ``` ## PHP ```php <?php // PHP program to find number? // of possible moves of knight $n = 4; $m = 4; // To calculate possible moves function findPossibleMoves($mat,? ???????????????????????????$p, $q) { ????global $n; ????global $m; ????// All possible moves? ????// of a knight ????$X = array(2, 1, -1, -2,? ??????????????-2, -1, 1, 2); ????$Y = array(1, 2, 2, 1, ??????????????-1, -2, -2, -1); ????$count = 0; ????// Check if each possible ????// move is valid or not ????for ($i = 0; $i < 8; $i++)? ????{ ????????// Position of ????????// knight after move ????????$x = $p + $X[$i]; ????????$y = $q + $Y[$i]; ????????// count valid moves ????????if ($x >= 0 && $y >= 0 &&? ????????????$x < $n && $y < $m &&? ????????????$mat[$x][$y] == 0) ????????????$count++; ????} ????// Return number? ????// of possible moves ????return $count; } // Driver Code $mat = array(array(1, 0, 1, 0), ?????????????array(0, 1, 1, 1), ?????????????array(1, 1, 0, 1), ?????????????array(0, 1, 1, 1)); $p = 2; $q = 2; echo findPossibleMoves($mat,? ???????????????????????$p, $q); // This code is contributed by ajit ?> ``` **輸出**: ``` 4 ``` **參考**: [https://en.wikipedia.org/wiki/Knight_(chess)](https://en.wikipedia.org/wiki/Knight_(chess)) 本文由 貢獻。 如果您喜歡 GeeksforGeeks 并希望做出貢獻,則還可以使用 [tribution.geeksforgeeks.org](http://www.contribute.geeksforgeeks.org) 撰寫文章,或將您的文章郵寄至 tribution@geeksforgeeks.org。 查看您的文章出現在 GeeksforGeeks 主頁上,并幫助其他 Geeks。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看