oj/1376

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <algorithm>

using namespace std;
char sts[50][50];
bool visited[50][50];

int DFS(int i, int j, int x2, int y2, int m, int n);
int  minValue4(int sum1,int sum2,int sum3,int sum4);
int  minValue3(int sum1,int sum2,int sum3);
int  minValue2(int sum1,int sum2);
int main() {
    int m, n;
    int x1, y1, x2, y2;
    cin >> m >> n;
    cin >> x1 >> y1 >> x2 >> y2;

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> sts[i][j];
            visited[i][j] = false;
        }
    }
    int sum;

    if (sts[x1][y1] == '#') {
        visited[x1][y1] = true;
    } else {
        if (!visited[x1][y1]) {
            sum = DFS(x1, y1, x2, y2, m, n);
        }
    }

    printf("%d\n", sum);
    return 0;
}

int DFS(int i, int j, int x2, int y2, int m, int n) {
    visited[i][j] = true;
    if (i == x2 && j == y2) return 0;
    int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
    if (i + 1 < m && sts[i + 1][j] == '.'&&visited[i+1][j]== false) {
        sum1 = 1 + DFS(i + 1, j, x2, y2, m, n);
    }
    if (j + 1 < n && sts[i][j + 1] == '.'&&visited[i][j+1]== false) {
        sum2 = 1 + DFS(i, j + 1, x2, y2, m, n);
    }
    if (i - 1 >= 0 && sts[i - 1][j] == '.'&&visited[i-1][j]== false) {
        sum3 = 1 + DFS(i - 1, j, x2, y2, m, n);
    }
    if (j - 1 >= 0 && sts[i][j - 1] == '.'&&visited[i][j-1]== false) {
        sum4 = 1 + DFS(i, j - 1, x2, y2, m, n);
    }
    return  minValue4(sum1,sum2,sum3,sum4);
//    return min(min(sum1,sum2),min(sum3,sum4));
}
int  minValue4(int sum1,int sum2,int sum3,int sum4){
    if(sum1==0){
      return   minValue3(sum2,sum3,sum4);
    }else if(sum2==0){
        return   minValue3(sum1,sum3,sum4);
    }else if(sum3 ==0){
        return   minValue3(sum1,sum2,sum4);
    }else if(sum4==0){
        return   minValue3(sum1,sum2,sum3);
    }else{
        return min(min(sum1,sum2),min(sum3,sum4));
    }
}
int  minValue3(int sum1,int sum2,int sum3){
    if(sum1==0){
        return minValue2(sum2,sum3);
    }else if(sum2==0){
        return minValue2(sum1,sum3);
    }else if(sum3==0){
        return minValue2(sum1,sum2);
    }else{
        return min(sum1,min(sum2,sum3));
    }
}
int  minValue2(int sum1,int sum2){
      if(sum1==0){
          return sum2;
      }else{
          return sum1;
      }
}

  

posted on 2019-12-24 10:45  我想静静_zjf  阅读(116)  评论(0编辑  收藏  举报

导航