Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- ifstream in("in.txt");
- ofstream out("out.txt");
- int n, m, i_begin, j_begin, i_end, j_end;
- vector <vector<int>> matrix;
- void solve(int i, int j, int temp) {
- matrix[i][j] = temp;
- if (matrix[i][j] != -1) {
- if (matrix[i + 1][j + 2] > temp + 1 || matrix[i + 1][j + 2] == 0) solve(i + 1, j + 2, temp + 1);
- if (matrix[i - 1][j + 2] > temp + 1 || matrix[i - 1][j + 2] == 0) solve(i - 1, j + 2, temp + 1);
- if (matrix[i + 1][j - 2] > temp + 1 || matrix[i + 1][j - 2] == 0) solve(i + 1, j - 2, temp + 1);
- if (matrix[i - 1][j - 2] > temp + 1 || matrix[i - 1][j - 2] == 0) solve(i - 1, j - 2, temp + 1);
- if (matrix[i + 2][j + 1] > temp + 1 || matrix[i + 2][j + 1] == 0) solve(i + 2, j + 1, temp + 1);
- if (matrix[i - 2][j + 1] > temp + 1 || matrix[i - 2][j + 1] == 0) solve(i - 2, j + 1, temp + 1);
- if (matrix[i + 2][j - 1] > temp + 1 || matrix[i + 2][j - 1] == 0) solve(i + 2, j - 1, temp + 1);
- if (matrix[i - 2][j - 1] > temp + 1 || matrix[i - 2][j - 1] == 0) solve(i - 2, j - 1, temp + 1);
- }
- }
- int main() {
- in >> n;
- in >> m;
- matrix.resize(n + 4, vector <int>(m + 4, -1));
- int n_temp = n + 2, m_temp = m + 2;
- for (size_t i = 2; i < n_temp; i++) {
- for (size_t j = 2; j < m_temp; j++) {
- in >> matrix[i][j];
- }
- }
- in >> i_begin;
- in >> j_begin;
- in >> i_end;
- in >> j_end;
- if (i_begin == i_end && j_begin == j_end) {
- out << "0";
- return 0;
- }
- solve(i_begin + 1, j_begin + 1, 0);
- matrix[i_end + 1][j_end + 1] > 0 ? out << matrix[i_end + 1][j_end + 1] : out << "No";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement