Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- #include<cmath>
- #include<algorithm>
- #include<string>
- #include<vector>
- #include<iterator>
- using namespace std;
- int a, i, sost, m, j, b, c, n, d, res = 0;
- vector<vector<char>>str;
- vector<vector<int>>vec;
- void rek(int x, int y) {
- if (x - 1 >= 0) {
- if (str[x - 1][y] != '#' &&
- (vec[x - 1][y] == 0
- || vec[x - 1][y] > vec[x][y] + 1)) {
- vec[x-1][y] = vec[x][y] + 1;
- rek(x - 1, y);
- }
- }
- if (y - 1 >= 0) {
- if (str[x][y - 1] != '#' && (vec[x][y - 1] == 0 || vec[x][y - 1] > vec[x][y] + 1)) {
- vec[x][y - 1] = vec[x][y] + 1;
- rek(x, y - 1);
- }
- }
- if (x + 1 < n) {
- if (str[x + 1][y] != '#' && (vec[x + 1][y] == 0 || vec[x + 1][y] > vec[x][y] + 1)) {
- vec[x + 1][y] = vec[x][y] + 1;
- rek(x + 1, y);
- }
- }
- int k = y;
- if (k < m - 1) {
- k++;
- if (int(str[x][k]) == int('.'))
- if(vec[x][k] == 0
- || vec[x][k] > vec[x][k-1] + 1) {
- vec[x][k] = vec[x][k-1] + 1;
- rek(x, k);
- }
- }
- }
- int main(){
- int k;
- cin >> n >> m >> k;
- vec.resize(n);
- str.resize(n);
- for (i = 0; i < n; ++i) {
- str[i].resize(m);
- for (int j = 0; j < m;++j)cin >> str[i][j];
- vec[i].resize(m);
- }
- cin >> i >> j >> a >> b;
- rek(i-1, j-1);
- cout << vec[a][b];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement