{ "cells": [ { "cell_type": "markdown", "source": [ "On an 8 x 8 chessboard, there is one white rook. There also may be empty\n", "squares, white bishops, and black pawns. These are given as characters 'R',\n", "'.', 'B', and 'p' respectively. Uppercase characters represent white pieces,\n", "and lowercase characters represent black pieces.\n", "\n", "The rook moves as in the rules of Chess: it chooses one of four cardinal\n", "directions (north, east, west, and south), then moves in that direction until\n", "it chooses to stop, reaches the edge of the board, or captures an opposite\n", "colored pawn by moving to the same square it occupies. Also, rooks cannot\n", "move into the same square as other friendly bishops.\n", "\n", "Return the number of pawns the rook can capture in one move.\n", "\n", "\n", "\n", "**Example 1:**\n", "\n", "![](https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG)\n", "\n", "\n", "\n", " Input: [[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"R\",\".\",\".\",\".\",\"p\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]\n", " Output: 3\n", " Explanation:\n", " In this example the rook is able to capture all the pawns.\n", "\n", "\n", "**Example 2:**\n", "\n", "![](https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG)\n", "\n", "\n", "\n", " Input: [[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\"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", " Output: 0\n", " Explanation:\n", " Bishops are blocking the rook to capture any pawn.\n", "\n", "\n", "**Example 3:**\n", "\n", "![](https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG)\n", "\n", "\n", "\n", " Input: [[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\"p\",\"p\",\".\",\"R\",\".\",\"p\",\"B\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"B\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\"p\",\".\",\".\",\".\",\".\"],[\".\",\".\",\".\",\".\",\".\",\".\",\".\",\".\"]]\n", " Output: 3\n", " Explanation:\n", " The rook can capture the pawns at positions b5, d6 and f5.\n", "\n", "\n", "\n", "\n", "**Note:**\n", "\n", " 1. `board.length == board[i].length == 8`\n", " 2. `board[i][j]` is either `'R'`, `'.'`, `'B'`, or `'p'`\n", " 3. There is exactly one cell with `board[i][j] == 'R'`" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "num_rook_captures (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function num_rook_captures(board::Matrix{Char})::Int\n", " res = 0\n", " i, j = findfirst(==('R'), board).I\n", "\n", " for v in (\n", " @view(board[i, (j + 1):end]), ## right\n", " @view(board[i, (j - 1):-1:1]), ## left\n", " @view(board[(i - 1):-1:1, j]), ## up\n", " @view(board[(i + 1):end, j]), ## down\n", " )\n", " x_ind = findfirst(!=('.'), v)\n", " if !isnothing(x_ind) && v[x_ind] == 'p'\n", " res += 1\n", " end\n", " end\n", " return res\n", "end\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }