Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _GLIBCXX_DEBUG
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef long double ld;
- #define mk make_pair
- #define inb push_back
- #define X first
- #define Y second
- #define all(v) v.begin(), v.end()
- #define sqr(x) (x) * (x)
- #define TIME 1.0 * clock() / CLOCKS_PER_SEC
- #define y1 AYDARBOG
- //continue break pop_back return
- int solve();
- int main()
- {
- //ios_base::sync_with_stdio(0);
- //cin.tie(0);
- #define TASK "lzss"
- #ifndef _DEBUG
- //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- #endif
- solve();
- #ifdef _DEBUG
- fprintf(stderr, "\nTIME: %.3f\n", TIME);
- #endif
- }
- const int BUFSZ = (int)1e5 + 7;
- char buf[BUFSZ];
- string get_str()
- {
- scanf(" %s", buf);
- return string(buf);
- }
- int solve()
- {
- int n, m;
- scanf("%d %d", &n, &m);
- swap(n, m);
- vector<vi> h(n, vi(m));
- pii st, fn;
- scanf("%d %d", &st.Y, &st.X);
- --st.Y, --st.X;
- scanf("%d %d", &fn.Y, &fn.X);
- --fn.Y, --fn.X;
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < m; ++j)
- {
- scanf("%d", &h[i][j]);
- }
- }
- vector<vi> d(n, vi(m, -1));
- d[st.X][st.Y] = 0;
- queue<pii> q;
- q.push(st);
- while (!q.empty())
- {
- int x = q.front().X;
- int y = q.front().Y;
- q.pop();
- //horr
- {
- int ch = h[x][y] + 1;
- int nx = x;
- int ny = y + 1;
- while (ny < m)
- {
- if (ch >= h[nx][ny])
- {
- if (d[nx][ny] == -1)
- {
- d[nx][ny] = d[x][y] + 1;
- q.push(mk(nx, ny));
- }
- }
- else
- {
- break;
- }
- ++ny;
- --ch;
- }
- }
- //horl
- {
- int ch = h[x][y] + 1;
- int nx = x;
- int ny = y - 1;
- while (ny >= 0)
- {
- if (ch >= h[nx][ny])
- {
- if (d[nx][ny] == -1)
- {
- d[nx][ny] = d[x][y] + 1;
- q.push(mk(nx, ny));
- }
- }
- else
- {
- break;
- }
- --ny;
- --ch;
- }
- }
- //veru
- {
- int ch = h[x][y] + 1;
- int nx = x - 1;
- int ny = y;
- while (nx >= 0)
- {
- if (ch >= h[nx][ny])
- {
- if (d[nx][ny] == -1)
- {
- d[nx][ny] = d[x][y] + 1;
- q.push(mk(nx, ny));
- }
- }
- else
- {
- break;
- }
- --nx;
- --ch;
- }
- }
- //verd
- {
- int ch = h[x][y] + 1;
- int nx = x + 1;
- int ny = y;
- while (nx < n)
- {
- if (ch >= h[nx][ny])
- {
- if (d[nx][ny] == -1)
- {
- d[nx][ny] = d[x][y] + 1;
- q.push(mk(nx, ny));
- }
- }
- else
- {
- break;
- }
- ++nx;
- --ch;
- }
- }
- }
- if (d[fn.X][fn.Y] == -1)
- puts("IMPOSSIBLE"), exit(0);
- printf("%d\n", d[fn.X][fn.Y]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement