# 省选模拟（12.10） T3 stwell

STWELL

12.10 省选模拟T3

1. 转移到(x + dx[pos], y + dy[pos], pos), 更新答案为CurK + 1

2. 如果(x, y) 处无山则分别向四个方向距离为CurK的点转移更新答案为CurK

Source:

/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>

static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}

///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}

template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}

inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/

const int MAXN = 2000 + 10;

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};

int n, m, k, sx, sy, tx, ty;
int d[MAXN][MAXN][4], map[MAXN][MAXN];

struct node {
int x, y, pos;
node(int x = 0, int y = 0, int pos = 0) : x(x), y(y), pos(pos) {};
} ;

R(n), R(m), R(k), R(sx), R(sy), R(tx), R(ty);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
R(map[i][j]);
}

std::deque<node> q;
inline void update(int x, int y, int pos, int w) {
if (d[x][y][pos] > w) {
d[x][y][pos] = w;
if (q.empty()) {
q.push_back(node(x, y, pos));
return ;
}
node e = q.front();
if (d[x][y][pos] <= d[e.x][e.y][e.pos]) q.push_front(node(x, y, pos));
else q.push_back(node(x, y, pos));
}
}

inline void solve() {
memset(d, 127, sizeof(d));
for (int i = 0; i < 4; ++i) update(tx, ty, i, 0);
while (!q.empty()) {
node e = q.front();
q.pop_front();
int w = d[e.x][e.y][e.pos];
if (e.x == sx && e.y == sy && w <= k) std::cout << "Possible", exit(0);
int x = e.x + dx[e.pos], y = e.y + dy[e.pos];
if (x >= 0 && y >= 0 && x < n && y < m) update(x, y, e.pos, w + 1);
if (map[e.x][e.y] == 1) continue ;
for (int i = 0; i < 4; ++i) {
int x = e.x + dx[i] * w, y = e.y + dy[i] * w;
if (x >= 0 && y >= 0 && x < n && y < m) update(x, y, i, w);
}
}
std::cout << "Impossible", exit(0);
}

int main() {
freopen("stwell.in", "r", stdin);
freopen("stwell.out", "w", stdout);
}