{"id":4892,"date":"2019-02-24T09:45:03","date_gmt":"2019-02-24T17:45:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4892"},"modified":"2019-02-24T12:59:00","modified_gmt":"2019-02-24T20:59:00","slug":"leetcode-999-available-captures-for-rook","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-999-available-captures-for-rook\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 999. Available Captures for Rook"},"content":{"rendered":"\n<p>On an 8 x 8 chessboard, there is one white rook.&nbsp; There also may be empty squares, white bishops, and black pawns.&nbsp; These are given as characters &#8216;R&#8217;, &#8216;.&#8217;, &#8216;B&#8217;, and &#8216;p&#8217; respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.<\/p>\n\n\n\n<p>The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies.&nbsp; Also, rooks cannot move into the same square as other friendly bishops.<\/p>\n\n\n\n<p>Return the number of pawns the rook can capture in one move.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/02\/20\/1253_example_1_improved.PNG\" alt=\"\" width=\"299\" height=\"303\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"R\",\".\",\".\",\".\",\"p\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong>\nIn this example the rook is able to capture all the pawns.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/02\/19\/1253_example_2_improved.PNG\" alt=\"\" width=\"298\" height=\"304\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\"p\",\"p\",\"p\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"B\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"B\",\"R\",\"B\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"B\",\"p\",\"p\",\".\",\".\"],[\".\",\"p\",\"p\",\"p\",\"p\",\"p\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]\n<strong>Output: <\/strong>0\n<strong>Explanation: <\/strong>\nBishops are blocking the rook to capture any pawn.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/02\/20\/1253_example_3_improved.PNG\" alt=\"\" width=\"294\" height=\"299\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\"p\",\"p\",\".\",\"R\",\".\",\"p\",\"B\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"B\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]\n<strong>Output: <\/strong>3\n<strong>Explanation: <\/strong>\nThe rook can capture the pawns at positions b5, d6 and f5.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>board.length == board[i].length == 8<\/code><\/li><li><code>board[i][j]<\/code>&nbsp;is either&nbsp;<code>'R'<\/code>,&nbsp;<code>'.'<\/code>,&nbsp;<code>'B'<\/code>, or&nbsp;<code>'p'<\/code><\/li><li>There is exactly one cell with&nbsp;<code>board[i][j] == 'R'<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int numRookCaptures(vector<vector<char>>& board) {        \n    int ans = 0;\n    auto check = [&board](int x, int y, int dx, int dy) {\n      x += dx;\n      y += dy;\n      while (x >= 0 && x < 8 &#038;&#038; y >= 0 && y < 8) {\n        if (board[y][x] == 'p') return 1;\n        if (board[y][x] != '.') break;\n        x += dx;\n        y += dy;\n      }\n      return 0;\n    };\n    \n    array<int, 5> dirs{1, 0, -1, 0, 1};\n    for (int i = 0; i < 8; ++i)\n      for (int j = 0; j < 8; ++j)\n        if (board[i][j] == 'R')\n          for (int d = 0; d < 4; ++d)          \n            ans += check(j, i, dirs[d], dirs[d + 1]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>On an 8 x 8 chessboard, there is one white rook.&nbsp; There also may be empty squares, white bishops, and black pawns.&nbsp; These are given&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[7,119,222,179],"class_list":["post-4892","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-board","tag-chess","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4892","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4892"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4892\/revisions"}],"predecessor-version":[{"id":4907,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4892\/revisions\/4907"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}