社区讨论

bfs

P1588[USACO07OPEN] Catch That Cow S参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lr7avhbf
此快照首次捕获于
2024/01/10 12:48
2 年前
此快照最后确认于
2024/01/10 18:34
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using  namespace std;
const int N = 1000 + 5;
const int M = 1000000 + 5; 
int d[M], step[M];
bool vis[M];
int main(){
	int T;
	cin>> T;
	while(T--){
		memset(vis, 0, sizeof(vis));
		memset(d, 0, sizeof(d));
		int head, tail;
		int x, y;
		cin>> x >> y;
		head = 0;
		tail = 0;
		d[head] = x;
		step[x] = 0;
		vis[x] = 1;
		if (d[head] == y){
			cout<< step[d[head]] << endl;
			continue; 
		}
		while (head <= tail){
			int u = d[head];
			int su = step[u];
			int v;
			if (vis[v] == 0 && v < 2 * y) {
				d[++tail] = v;
				vis[v] = 1;
				step[v] = step[u] + 1;
				if (v == y) {
					cout << step[v] << endl;
					break;
				}
			}
			v = u + 1;
			if (vis[v] == 0 && v <= y) {
				d[++tail] = v;
				vis[v] = 1;
				step[v] = step[u] + 1;
				if (v == y) {
					cout << step[v] << endl;
					break;
				}
			}
			v = u - 1;
			if (vis[v] == 0 && v >= 0) {
				d[++tail] = v;
				vis[v] = 1;
				step[v] = step[u] + 1;
				if (v == y) {
					cout << step[v] << endl;
					break;
				}
			}
			head++;
		}
	}
	return 0;
}
0分

回复

0 条回复,欢迎继续交流。

正在加载回复...