Pagini recente » Cod sursa (job #322697) | Cod sursa (job #1814696) | Cod sursa (job #721979) | Cod sursa (job #1261293) | Cod sursa (job #3235767)
//dial
#include <bits/stdc++.h>
std :: ifstream in ("car.in");
std :: ofstream out ("car.out");
const int NMAX = 5e2 + 5;
const int INF = 2e9;
const int dx[] = {-1, -1, 0, 1, 1, 1, 0, - 1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n;
int m;
int xs;
int ys;
int xf;
int yf;
int cnt;
int actual;
int mini = INF;
bool a[NMAX][NMAX];
bool visited[NMAX][NMAX][8];
int dist[NMAX][NMAX][8];
std :: queue <std :: pair<std :: pair<int, int>, int>> q[3];
inline bool inbound(int i, int j)
{
return i >= 1 && j >= 1 && i <= n && j <= n;
}
void dial()
{
while(cnt)
{
while(q[actual].empty())
{
actual ++;
actual %= 3;
}
int x = q[actual].front().first.first;
int y = q[actual].front().first.second;
int dir = q[actual].front().second;
q[actual].pop();
cnt --;
if(visited[x][y][dir] == 0)
{
visited[x][y][dir] = true;
//pastrez dir
for(int i = -2; i <= 2; i ++)
{
int ac_dir = dir + i;
if(ac_dir < 0)
{
ac_dir += 8;
}
ac_dir %= 8;
int l = x + dx[ac_dir];
int c = y + dy[ac_dir];
if(inbound(l, c) && a[l][c] == 0 && dist[x][y][dir] + abs(i) < dist[l][c][ac_dir])
{
cnt ++;
q[(actual + abs(i)) % 3].push(std :: make_pair(std :: make_pair(l, c), ac_dir));
dist[l][c][ac_dir] = dist[x][y][dir] + abs(i);
}
}
}
}
}
int main()
{
in >> n >> m;
in >> xs >> ys >> xf >> yf;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
in >> a[i][j];
}
}
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
for(int k = 0; k < 8; k ++)
{
dist[i][j][k] = INF;
}
}
}
for(int i = 0; i < 7; i ++)
{
q[0].push(std :: make_pair(std :: make_pair(xs, ys), i));
dist[xs][ys][i] = 0;
cnt ++;
}
dial();
for(int i = 0; i < 8; i ++)
{
mini = std :: min(mini, dist[xf][yf][i]);
}
if(mini == INF)
{
out << -1;
}
else
{
out << mini;
}
return 0;
}