NOIP提高组1580~1590集合答案

1880:【13NOIP提高组】华容道

时间限制: 1000 ms         内存限制: 131072 KB
提交数: 298     通过数: 124 

【题目描述】
小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次。于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间。 

小 B 玩的华容道与经典的华容道游戏略有不同,游戏规则是这样的: 

1.在一个 n*m 棋盘上有 n*m 个格子,其中有且只有一个格子是空白的,其余 n*m-1个格子上每个格子上有一个棋子,每个棋子的大小都是 1*1 的; 

2.有些棋子是固定的,有些棋子则是可以移动的; 

3.任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上。 游戏的目的是把某个指定位置可以活动的棋子移动到目标位置。 

给定一个棋盘,游戏可以玩 q 次,当然,每次棋盘上固定的格子是不会变的,但是棋盘上空白的格子的初始位置、指定的可移动的棋子的初始位置和目标位置却可能不同。第 i 次玩的时候,空白的格子在第 EXi 行第 EYi 列,指定的可移动棋子的初始位置为第 SXi 行第 SYi列,目标位置为第 TXi 行第 TYi 列。 

假设小 B 每秒钟能进行一次移动棋子的操作,而其他操作的时间都可以忽略不计。请你告诉小 B 每一次游戏所需要的最少时间,或者告诉他不可能完成游戏。

【输入】
第一行有 3 个整数,每两个整数之间用一个空格隔开,依次表示 n、m 和 q; 

接下来的 n 行描述一个 n*m 的棋盘,每行有 m 个整数,每两个整数之间用一个空格隔开,每个整数描述棋盘上一个格子的状态,0 表示该格子上的棋子是固定的,1 表示该格子上的棋子可以移动或者该格子是空白的。 

接下来的 q 行,每行包含 6 个整数依次是 EXi、EYi、SXi、SYi、TXi、TYi,每两个整数之间用一个空格隔开,表示每次游戏空白格子的位置,指定棋子的初始位置和目标位置。

【输出】
输出有 q 行,每行包含 1 个整数,表示每次游戏所需要的最少时间,如果某次游戏无法完成目标则输出−1。

【输入样例】
3 4 2
0 1 1 1
0 1 1 0
0 1 0 0
3 2 1 2 2 2
1 2 2 2 3 2
【输出样例】
2
-1

【提示】
【输入输出样例说明】 

棋盘上划叉的格子是固定的,红色格子是目标位置,圆圈表示棋子,其中绿色圆圈表示目标棋子。 

1. 第一次游戏,空白格子的初始位置是(3,2)(图中空白所示),游戏的目标是将初始位置在(1,2)上的棋子(图中绿色圆圈所代表的棋子)移动到目标位置(2,  2)(图中红色的格子)上。 

移动过程如下:



2. 第二次游戏,空白格子的初始位置是(1, 2)(图中空白所示),游戏的目标是将初始位置在(2,2)上的棋子(图中绿色圆圈所示)移动到目标位置(3, 2)上。



要将指定块移入目标位置,必须先将空白块移入目标位置,空白块要移动到目标位置,必然是从位置(2,2)上与当前图中目标位置上的棋子交换位置,之后能与空白块交换位置的只有当前图中目标位置上的那个棋子,因此目标棋子永远无法走到它的目标位置,游戏无法完成。 

【数据范围】 

对于 30%的数据,1 ≤ n, m ≤ 10,q = 1; 

对于 60%的数据,1 ≤ n, m ≤ 30,q ≤ 10; 

对于 100%的数据,1 ≤ n, m ≤ 30,q ≤ 500。
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 35, INF = 0x3f3f3f3f;
const int MOVE[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

bool ori[N][N];
int n, m, qq;
struct point{
	int x, y;
	inline void input() {
		scanf("%d%d", &x, &y);
	}
	inline bool operator == (const point &rhs) const {
		return x == rhs.x and y == rhs.y;
	}
	inline point move(int k) {
		point now = (point){x + MOVE[k][0], y + MOVE[k][1]};
		return now;
	}
	inline bool psb() {
		return ori[x][y] && 1 <= x && x <= n && 1 <= y && y <= m;
	}
};

typedef pair<point, int> pwd;
queue<pwd> pq;
queue<point> q;
int distB[N][N], dist[N][N][4][4];
bool visit[N][N];
#define mp make_pair
inline void initBlank(point center) {
	for (int k = 0; k < 4; k++) {
		point tmp = center.move(k);
		if (tmp.psb()) pq.push(mp(tmp, k));
	}
	while (! pq.empty()) {
		point st = pq.front().first; 
		int k1 = pq.front().second; pq.pop();
		while (! q.empty()) q.pop();
		memset(visit, 0, sizeof(visit));
		distB[st.x][st.y] = 0; q.push(st); visit[st.x][st.y] = true;
		while (! q.empty()) {
			point dir = q.front(); q.pop();
			for (int k = 0; k < 4; k++) {
				point tmp = dir.move(k);
				if (! tmp.psb() or tmp == center) continue;
				if (! visit[tmp.x][tmp.y]) {
					distB[tmp.x][tmp.y] = distB[dir.x][dir.y] + 1;
					q.push(tmp); visit[tmp.x][tmp.y] = true;
				}
			}
		}
		for (int k2 = 0; k2 < 4; k2++) if (k1 != k2) {
			point tmp = center.move(k2);
			if (tmp.psb() && visit[tmp.x][tmp.y]) {
				dist[center.x][center.y][k1][k2] = distB[tmp.x][tmp.y];
			}
		}
	}
}

inline void preWork() {
	memset(dist, 0x3f, sizeof(dist));
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) if (ori[i][j]) {
			point tmp = (point){i, j};
			initBlank(tmp);
		}
}

point blank, src, tar;

inline void preSpfa() {
	memset(distB, 0x3f, sizeof(distB));
	distB[blank.x][blank.y] = 0;
	q.push(blank);
	while (! q.empty()) {
		point dir = q.front(); q.pop();
		for (int k = 0; k < 4; k++) {
			point tmp = dir.move(k);
			if (tmp.psb() && ! (tmp == src))
				if (distB[tmp.x][tmp.y] == INF) {
					distB[tmp.x][tmp.y] = distB[dir.x][dir.y] + 1;
					q.push(tmp);
				}
		}
	}
}

int dis[N][N][4];
bool inq[N][N][4];
#define inQ(rhs) inq[rhs.first.x][rhs.first.y][rhs.second]
#define diS(rhs) dis[rhs.first.x][rhs.first.y][rhs.second]
inline int spfa() {
	if (src == tar) return 0;
	preSpfa();
	memset(dis, 0x3f, sizeof(dis));
	for (int k = 0; k < 4; k++) {
		point dir = src.move(k);
		if (dir.psb() && distB[dir.x][dir.y] != INF) {
			dis[src.x][src.y][k] = distB[dir.x][dir.y];
			pwd now = mp(src, k);
			pq.push(now); inQ(now) = true;
		}
	}
	
	while (! pq.empty()) {
		pwd dir = pq.front(); pq.pop();
		inQ(dir) = false;
		point tmp1 = dir.first.move(dir.second);
		pwd tmp2 = mp(tmp1, dir.second^1);
		if (diS(tmp2) > diS(dir) + 1) {
			diS(tmp2) = diS(dir) + 1;
			if (! inQ(tmp2)) {
				pq.push(tmp2); inQ(tmp2) = true;
			}
		}

		for (int i = 0; i < 4; i++) if (i != dir.second) {
			tmp1 = dir.first.move(i);
			int &l = dist[dir.first.x][dir.first.y][dir.second][i];
			if (! tmp1.psb() || l == INF) continue;
			tmp2 = mp(dir.first, i);
			if (diS(tmp2) > diS(dir) + l) {
				diS(tmp2) = diS(dir) + l;
				if (! inQ(tmp2)) {
					pq.push(tmp2); inQ(tmp2) = true;
				}
			}
		}
	}
	int ans = INF;
	for (int k = 0; k < 4; k++)
		ans = min(ans, dis[tar.x][tar.y][k]);
	return ans == INF ? -1 : ans;
}

int main() {
	cin >> n >> m >> qq;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			int tmp; scanf("%d", &tmp);
			ori[i][j] = tmp;
		}
	preWork();
	while (qq--) {
		blank.input(); src.input(); tar.input();
		printf("%d\n", spfa());
	}
	return 0;
}
1881:【14NOIP提高组】生活大爆炸版石头剪子布

时间限制: 1000 ms         内存限制: 131072 KB
提交数: 1304     通过数: 897 

【题目描述】
石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值