report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Path on binary matrix - FlandreOJ: Flandre Online Judge

Path on binary matrix

Time limit: 1000 ms
Memory limit: 256 MB
You are given an binary matrix $A$ of $n$ rows and $m$ columns. In a move, from one cell, you can go to any of 4 adjacent cells (i.e. share the same edge). You cannot move to cell with number $1$. Find the shortest distance from $(1, 1)$ to $(n, m)$. ### Input - The first line contains 2 integers $n, m$. - The next $n$ lines, each line contains a binary string of length $m$, matrix $A$. ### Output - Print the distance from $(1, 1)$ to $(n, m)$, or print `-1` if no path exists. ### Constraints - $1 \le n, m \le 10^3$. ### Example Input: ``` 4 5 01000 01010 00010 11010 ``` Output: ``` 11 ```
BFS / DFS
Connected component
Shortest path
Finding the path
Path on binary matrix
Garden
Operations on number
Bipartite graph
Tom and Jerry
Festival 1
Bamboo Forest of the Lost
Radar
Festival 2
Go
Escape from... dolls
Long leg
Lexicographically smallest path
Graph coloring
Topic
Graph
Shortest path
Rating 800
Solution (0) Solution