记录编号 576420 评测结果 AAAAAAAAAA
题目名称 [USACO Mar12] 拖拉机 最终得分 100
用户昵称 GravatarHeSn 是否通过 通过
代码语言 C++ 运行时间 1.438 s
提交时间 2022-10-08 19:05:23 内存使用 13.61 MiB
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n, g[1010][1010] = {0}, f[1010][1010], maxn = 0, maxm = 0;
int tx[4] = {0, 0, 1, -1}, ty[4] = {1, -1, 0, 0};
int main() {
	freopen("tractor.in", "r", stdin);
	freopen("tractor.out", "w", stdout);
	int x, y;
	cin >> n >> x >> y;
	for(int i = 1; i <= n; i ++) {
		int a, b;
		cin >> a >> b;
		maxn = max(maxn, a);
		maxm = max(maxm, b);
		g[a][b] = 1;
	}
	maxn = maxm = 1001;
	memset(f, -1, sizeof(f));
	deque<int> qx, qy;
	qx.push_front(x);
	qy.push_front(y);
	f[x][y] = 0;
	while(!qx.empty()) {
		x = qx.front();
		y = qy.front();
		qx.pop_front();
		qy.pop_front();
		for(int i = 0; i <= 3; i ++) {
			int u = x + tx[i], v = y + ty[i];
			if(u > maxn || u < 0 || v > maxm || v < 0 || f[u][v] != -1) {
				continue;
			}
			if(g[u][v]) {
				f[u][v] = f[x][y] + 1;
				qx.push_back(u);
				qy.push_back(v);
			}
			else {
				f[u][v] = f[x][y];
				qx.push_front(u);
				qy.push_front(v);
			}
		}
	}
//	for(int i = 0; i <= maxn; i ++) {
//		for(int j = 0; j <= maxn; j ++) {
//			cout << g[i][j] << ' ';
//		}
//		cout << endl;
//	}
	cout << f[0][0];
	return 0;
}