省选模拟(12.10) T3 stwell

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值