A12771 | Treasure Island
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
All of us love treasures, right? That's why young Vasya is heading for a Treasure Island.
Treasure Island may be represented as a rectangular table $n \times m$ which is surrounded by the ocean. Let us number rows of the field with consecutive integers from $1$ to $n$ from top to bottom and columns with consecutive integers from $1$ to $m$ from left to right. Denote the cell in $r$ -th row and $c$ -th column as $(r, c)$ . Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell $(n, m)$ .
Vasya got off the ship in cell $(1, 1)$ . Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell $(x, y)$ he can move only to cells $(x+1, y)$ and $(x, y+1)$ . Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells $(1, 1)$ where Vasya got off his ship and $(n, m)$ where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
Treasure Island may be represented as a rectangular table $n \times m$ which is surrounded by the ocean. Let us number rows of the field with consecutive integers from $1$ to $n$ from top to bottom and columns with consecutive integers from $1$ to $m$ from left to right. Denote the cell in $r$ -th row and $c$ -th column as $(r, c)$ . Some of the island cells contain impassable forests, and some cells are free and passable. Treasure is hidden in cell $(n, m)$ .
Vasya got off the ship in cell $(1, 1)$ . Now he wants to reach the treasure. He is hurrying up, so he can move only from cell to the cell in next row (downwards) or next column (rightwards), i.e. from cell $(x, y)$ he can move only to cells $(x+1, y)$ and $(x, y+1)$ . Of course Vasya can't move through cells with impassable forests.
Evil Witch is aware of Vasya's journey and she is going to prevent him from reaching the treasure. Before Vasya's first move she is able to grow using her evil magic impassable forests in previously free cells. Witch is able to grow a forest in any number of any free cells except cells $(1, 1)$ where Vasya got off his ship and $(n, m)$ where the treasure is hidden.
Help Evil Witch by finding out the minimum number of cells she has to turn into impassable forests so that Vasya is no longer able to reach the treasure.
输入格式
First line of input contains two positive integers $n$ , $m$ ( $3 \le n \cdot m \le 1\,000\,000$ ), sizes of the island.
Following $n$ lines contains strings $s_i$ of length $m$ describing the island, $j$ -th character of string $s_i$ equals "\#" if cell $(i, j)$ contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell $(1, 1)$ , i.e. the first cell of the first row, and he wants to reach cell $(n, m)$ , i.e. the last cell of the last row.
It's guaranteed, that cells $(1, 1)$ and $(n, m)$ are empty.
Following $n$ lines contains strings $s_i$ of length $m$ describing the island, $j$ -th character of string $s_i$ equals "\#" if cell $(i, j)$ contains an impassable forest and "." if the cell is free and passable. Let us remind you that Vasya gets of his ship at the cell $(1, 1)$ , i.e. the first cell of the first row, and he wants to reach cell $(n, m)$ , i.e. the last cell of the last row.
It's guaranteed, that cells $(1, 1)$ and $(n, m)$ are empty.
输出格式
Print the only integer $k$ , which is the minimum number of cells Evil Witch has to turn into impassable forest in order to prevent Vasya from reaching the treasure.
输入输出样例
输入 #1
2 2 .. ..
输出 #1
2
输入 #2
4 4 .... #.#. .... .#..
输出 #2
1
输入 #3
3 4 .... .##. ....
输出 #3
2
The following picture illustrates the island in the third example. Blue arrows show possible paths Vasya may use to go from $(1, 1)$ to $(n, m)$ . Red illustrates one possible set of cells for the Witch to turn into impassable forest to make Vasya's trip from $(1, 1)$ to $(n, m)$ impossible.


C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted