Pagini recente » Cod sursa (job #1698776) | Cod sursa (job #2854499) | Cod sursa (job #1634175) | Cod sursa (job #1053396) | Cod sursa (job #3260279)
#include <bits/stdc++.h>
using namespace std;
int n, m, xs, ys, xf, yf, a, b, mt[205][205];
queue<pair<int,int>> Q;
void Lee() {
Q.push(make_pair(xs , ys));
int dx[] = {1, 0, -1, 0, -1, -1, 1 , 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
mt[xs][ys] = 1;
while(! Q.empty()) {
int i = Q.front().first;
int j = Q.front().second;
Q.pop();
for(int k = 0 ; k < 8 ; k ++) {
int iv = i + dx[k];
int jv = j + dy[k];
int score = 1;
if(mt[iv][jv] != 1 && mt[iv][jv] > mt[i][j] + 1) {
mt[iv][jv] = mt[i][j] + score;
Q.push(make_pair(iv , jv));
}
}
}
}
void Bordare() {
int i;
for (i = 0; i <= n + 1; i++) {
mt[0][i] = mt[n + 1][i] = mt[i][0] = mt[i][n + 1] = 1;
}
}
int main() {
ifstream cin ("car.in");
ofstream cout ("car.out");
cin >> n >> m;
cin >> xs >> ys >> xf >> yf;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
cin >> mt[i][j];
if (mt[i][j] != 1) {
mt[i][j] = 200000;
}
}
Bordare();
Lee();
cout << mt[xf][yf];
return 0;
}