省选模拟(12.10) T3 stwell

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/78874463

STWELL

题目背景:

12.10 省选模拟T3

分析:BFS

 

注意到我们可以过来做会更简单一些。我们把每个点拆成四个点分别表示这个点是从哪里转移来的然后我们要求到达每个点所需

的能量最小值,如果我们当前在(x, y), pos转移来那么我们有两种转移

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>

inline char read() {
	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) {};
} ;

inline void read_in() {
	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); 
	read_in();
	solve();
	return 0;
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页