[kuangbin专题1]简单搜索

棋盘问题

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;

int n, k;
char s[10][10];
int vis[10];
int ans;

void dfs(int x, int t) {
	if (t == k) {
		ans++;
		return ;
	}
	if (x == n + 1) {
		return ;
	}
	for (int i = 1; i <= n; i++) {
		if (vis[i] == 0 && s[x][i] == '#') {
			vis[i] = 1;
			dfs(x + 1, t + 1);
			vis[i] = 0;
		}
	}
	dfs(x + 1, t);
}


int main() {
	while (~scanf("%d %d", &n, &k)) {
		if (n == -1 && k == -1) {
			break;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				scanf(" %c", *(s + i) + j);
			}
		}
		ans = 0;
		memset(vis, 0, sizeof(vis));
		dfs(1, 0);
		printf("%d\n", ans);
	}
	return 0;
}

Dungeon Master

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;

int L, R, C;
int ans;
int vis[35][35][35];
char s[35][35][35];
struct step {
	int x, y, c, sp;
	step(int tx, int ty, int tc, int ts): x(tx), y(ty), c(tc), sp(ts) {

	}
};

int gox[6] = {0, 1, 0, 0, 0, -1};

int goy[6] = {1, 0, 0, 0, -1, 0};

int goc[6] = {0, 0, 1, -1, 0, 0};
int ec, ex, ey;

int bfs(int c, int x, int y) {
	queue<step> que;
	que.push(step(x, y, c, 0));
	while (!que.empty()) {
		step p = que.front();
		que.pop();
		//printf("%d %d %d %d\n", p.x, p.y, p.c, p.sp);
		if (p.x == ex && p.y == ey && p.c == ec) {
			return p.sp;
		}
		int tx, ty, tc;
		for (int i = 0; i <= 5; i++) {
			tx = p.x + gox[i];
			ty = p.y + goy[i];
			tc = p.c + goc[i];
			if (tx >= 1 && tx <= R && ty >= 1 && ty <= C && tc >= 1 && tc <= L && s[tc][tx][ty] != '#' && vis[tc][tx][ty] == 0) {
				vis[tc][tx][ty] = 1;
				que.push(step(tx, ty, tc, p.sp + 1));
				//printf();
			}
		}
	}
	return 0;
}

void solve() {
	ans = 0;
	memset(vis, 0, sizeof(vis));
	int tx, ty, tc;
	for (int i = 1; i <= L; i++) {
		for (int j = 1; j <= R; j++) {
			for (int k = 1; k <= C; k++) {
				scanf(" %c", &s[i][j][k]);
				if (s[i][j][k] == 'S') {
					tc = i;
					tx = j;
					ty = k;
				}
				if (s[i][j][k] == 'E') {
					ec = i;
					ex = j;
					ey = k;
				}
			}
		}
	}
	int res = bfs(tc, tx, ty);
	if (res) {
		printf("Escaped in %d minute(s).\n", res);
	} else {
		printf("Trapped!\n");
	}
}

int main() {
	while (~scanf("%d %d %d", &L, &R, &C)) {
		if (L == 0 && R == 0 && C == 0) {
			break;
		}
		solve();
	}
	return 0;
}

Catch that cow

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 505;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;

int vis[MAXN];
int N, K;
struct node {
	int x, cost;
	node(int tx, int tcost): x(tx), cost(tcost) {
	}

};

int solve() {
	queue<node> que;
	que.push(node(N, 0));
	while (!que.empty()) {
		node t = que.front();
		que.pop();
		//printf("%d %d\n", t.x, t.cost);
		if (t.x == K) {
			return t.cost;
		}
		if (t.x - 1 >= 0 && !vis[t.x - 1]) {
			vis[t.x - 1] = 1;
			que.push(node(t.x - 1, t.cost + 1));
		}
		if (t.x + 1 <= 100000 && !vis[t.x + 1]) {
			vis[t.x + 1] = 1;
			que.push(node(t.x + 1, t.cost + 1));
		}
		if (t.x * 2 <= 100000 && !vis[t.x * 2]) {
			vis[t.x * 2] = 1;
			que.push(node(t.x * 2, t.cost + 1));
		}
	}
}

int main() {
	scanf("%d %d", &N, &K);
	printf("%d", solve());
	return 0;
}

Find The Multiple

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);

int m;
int flag;
void dfs(int step, unsigned long long x) {
	if (step > 19 || flag == 1) {
		return ;
	}
	if (x % m == 0) {
		flag = 1;
		printf("%lld\n", x);
		return;
	}
	dfs(step + 1, x * 10);
	dfs(step + 1, x * 10 + 1);
}


int main() {
	while (scanf("%d", &m) && m) {
		dfs(1, 1);
		flag = 0;
	}
	return 0;
}

Prime Path

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);

const int N = 10005;
bool isprime[N];
int a, b;
typedef pair<int, int> P;
int vis[N];

void sieve() {
	for (int i = 0; i <= N; i++) {
		isprime[i] = true;
	}
	isprime[0] = isprime[1] = 0;
	for (int i = 2; i <= N; i++) {
		if (isprime[i]) {
			//printf("%d\t", i);
			for (int j = 2 * i; j <= N; j += i) {
				isprime[j] = false;
			}
		}
	}
}

inline int getnum(int n4, int n3, int n2, int n1) {
	return n4 * 1000 + n3 * 100 + n2 * 10 + n1;
}

bool check(int n4, int n3, int n2, int n1) {
	int tmp = getnum(n4, n3, n2, n1);
	if (tmp >= 1000 && isprime[tmp] && vis[tmp] == 0) {
		return true;
	}
	return false;
}


void bfs() {
	queue<P> que;
	que.push(P(a, 0));
	vis[a] = 1;
	while (!que.empty()) {
		P k = que.front();
		que.pop();
		//printf("%d %d\n", k.first, k.second);
		if (k.first == b) {
			printf("%d\n", k.second);
			return ;
		}
		int n1 = k.first % 10;
		int n2 = k.first / 10 % 10;
		int n3 = k.first / 100 % 10;
		int n4 = k.first / 1000;
		for (int i = 0; i <= 9; i++) {
			int num;
			if (check(n4, n3, n2, i)) {
				num = getnum(n4, n3, n2, i);
				vis[num] = 1;
				que.push(P(num, k.second + 1));
			}
			if (check(n4, n3, i, n1)) {
				num = getnum(n4, n3, i, n1);
				vis[num] = 1;
				que.push(P(num, k.second + 1));
			}
			if (check(n4, i, n2, n1)) {
				num = getnum(n4, i, n2, n1);
				vis[num] = 1;
				que.push(P(num, k.second + 1));
			}
			if (check(i, n3, n2, n1)) {
				num = getnum(i, n3, n2, n1);
				vis[num] = 1;
				que.push(P(num, k.second + 1));
			}
		}
	}
	printf("Impossible\n");
}


int main() {
	int t;
	scanf("%d", &t);
	sieve();
	while (t--) {
		scanf("%d %d", &a, &b);
		bfs();
		memset(vis, 0, sizeof(vis));
	}
	return 0;
}

Shuffle'm Up

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);



int pos = 1;
void solve() {
	int len;
	scanf("%d", &len);
	string s1, s2, s3, s, ss;
	cin >> s1 >> s2 >> s;
	ss = s1 + s2;
	int cnt = 0;
	while (1) {
		s3 = "";
		for (int i = 0; i < len; i++) {
			s3 = s3 + s2[i];
			s3 = s3 + s1[i];
		}
		cnt++;
		//cout << s3 << endl;
		if (s3 == s) {
			printf("%d %d\n", pos++, cnt);
			return ;
		}
		for (int i = 0; i < len; i++) {
			s1[i] = s3[i];
			s2[i] = s3[i + len];
		}
		//cout << s1 << "  " << s2 << endl;
		if (ss == s1 + s2) {
			printf("%d -1\n", pos++);
			return ;
		}
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		solve();
	}
	return 0;
}

Fire!

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);

int R, C;
char s[1010][1010];
int fire[1010][1010];
int vis[1005][1005];
int go[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int sx, sy;
//int fx, fy;
typedef pair<int, int> P;
int pos = 0;

struct ff {
	int x, y;
	ff() {
	}
	ff(int tx, int ty): x(tx), y(ty) {
	}
} ;
ff Fire[1005];

void firetomap() {
	memset(fire, INF, sizeof(fire));
	queue<ff> que;
	for (int i = 0; i < pos; i++) {
		que.push(ff(Fire[i].x, Fire[i].y));
		fire[Fire[i].x][Fire[i].y] = 0;
	}
	while (!que.empty()) {
		ff t = que.front();
		que.pop();
		for (int i = 0; i <= 3; i++) {
			int tx = t.x + go[i][0];
			int ty = t.y + go[i][1];
			if (tx >= 1 && tx <= R  && ty >= 1 && ty <= C  && s[tx][ty] != '#' && fire[tx][ty] == INF) {
				fire[tx][ty] = fire[t.x][t.y] + 1;
				que.push(ff(tx, ty));
			}
		}
	}/*
	for (int i = 1; i <= R; i++) {
		for (int j = 1; j <= C; j++) {
			printf("%d ", fire[i][j]);
		}
		printf("\n");
	}*/
}

struct node {
	int x, y, time;
	node(int tx, int ty, int t): x(tx), y(ty), time(t) {
	}
};

void bfs() {
	firetomap();
	queue<node> que;
	vis[sx][sy] = 1;
	que.push(node(sx, sy, 0));
	while (!que.empty()) {
		node t = que.front();
		que.pop();
		//printf("%d %d %d\n", t.x, t.y, t.time);
		if (t.x == 1 || t.x == R  || t.y == 1 || t.y == C ) {
			printf("%d\n", t.time + 1);
			return ;
		}
		for (int i = 0; i <= 3; i++) {
			int tx = t.x + go[i][0];
			int ty = t.y + go[i][1];
			int tm = t.time + 1;
			if (tx >= 1 && tx <= R  && ty >= 1 && ty <= C  && vis[tx][ty] == 0 && s[tx][ty] != '#' && tm < fire[tx][ty] ) {
				vis[tx][ty] = 1;
				que.push(node(tx, ty, tm));
			}
		}
	}
	printf("IMPOSSIBLE\n");
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		pos = 0;
		scanf("%d %d", &R, &C);
		for (int i = 1; i <= R; i++) {
			for (int j = 1; j <= C; j++) {
				scanf(" %c", *(s + i) + j);
				if (s[i][j] == 'J') {
					sx = i;
					sy = j;
				}
				if (s[i][j] == 'F') {
					Fire[pos].x = i;
					Fire[pos++].y = j;
				}
			}
		}
		bfs();
		memset(vis, 0, sizeof(vis));
		memset(Fire, 0, sizeof(Fire));
	}
	return 0;
}

迷宫问题

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
typedef pair<int, int> P;



int vis[10][10];
int maze[10][10];
int go[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

struct node {
	int x, y;
	string s;
	//node(int tx, int ty, const char *ss): x(tx), y(ty), s(ss) {
	//}
	node(int tx, int ty, string ts): x(tx), y(ty), s(ts) {
	}
};

string bfs() {
	queue<node> que;
	vis[0][0] = 1;
	que.push(node(0, 0, "(0, 0)\n"));
	while (!que.empty()) {
		node t = que.front();
		que.pop();
		int tx, ty;
		//cout << t.s;
		if (t.x == 4 && t.y == 4) {
			return t.s;
		}
		for (int i = 0; i <= 3; i++) {
			tx = t.x + go[i][0];
			ty = t.y + go[i][1];
			//	printf("%d %d\n", tx, ty);
			if (tx >= 0 && tx < 5 && ty >= 0 && ty < 5 && vis[tx][ty] == 0 && maze[tx][ty] == 0) {
				vis[tx][ty] = 1;
				string s = "(, )\n" ;
				char ch1 = '0' + tx, ch2 = '0' + ty;
				s.insert(s.begin() + 1, ch1);
				s.insert(s.begin() + 4, ch2);
				//cout << "S:" << s << endl;
				que.push(node(tx, ty, t.s + s));
			}
		}
	}
	return "Impossible\n";
}


int main() {

	for (int i = 0; i <= 4; i++) {
		for (int j = 0; j <= 4; j++) {
			scanf("%d", *(maze + i) + j);
		}
	}
	cout << bfs() << endl;
	return 0;
}

非常可乐

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false);
typedef pair<int, int> P;

struct node {
	int x, y, z, step;
	node(int tx, int ty, int tz, int c): x(tx), y(ty), z(tz), step(c) {
	}
};

int S, N, M;
int vis[105][105][105];
void bfs() {
	queue<node> que;
	que.push(node(0, 0, S, 0));
	int kk = S / 2;
	while (!que.empty()) {
		node t = que.front();
		que.pop();
		//printf("%d %d %d %d\n", t.z, t.x, t.y, t.step);
		if ((t.z == kk && t.x == kk) || (t.x == kk && t.y == kk) || (t.z == kk && t.y == kk)) {
			printf("%d\n", t.step);
			return;
		}
		if (vis[0][t.y][t.x + t.z] == 0) {
			vis[0][t.y][t.x + t.z] = 1;
			que.push(node(0, t.y, t.x + t.z, t.step + 1));
		}
		if (vis[t.x][0][t.z + t.y] == 0) {
			vis[t.x][0][t.z + t.y] = 1;
			que.push(node(t.x, 0, t.y + t.z, t.step + 1));
		}
		if (t.z + t.x > N && vis[N][t.y][t.z + t.x - N] == 0) {
			vis[N][t.y][t.z + t.x - N] = 1;
			que.push(node(N, t.y, t.z - (N - t.x), t.step + 1));
		} else if (t.z + t.x <= N && vis[t.x + t.z][t.y][0] == 0) {
			vis[t.x + t.z][t.y][0] = 1;
			que.push(node(t.x + t.z, t.y, 0, t.step + 1));
		}
		if (t.y + t.z > M && vis[t.x][M][t.y + t.z - M] == 0) {
			vis[t.x][M][t.y + t.z - M] = 1;
			que.push(node(t.x, M, t.y + t.z - M, t.step + 1));
		} else if (t.y + t.z <= M && vis[t.x][t.y + t.z][0] == 0) {
			vis[t.x][t.y + t.z][0] = 1;
			que.push(node(t.x, t.y + t.z, 0, t.step + 1));
		}
		if (t.y + t.x > N && vis[N][t.y + t.x - N][t.z] == 0) {
			vis[N][t.y + t.x - N][t.z] = 1;
			que.push(node(N, t.y + t.x - N, t.z, t.step + 1));
		} else if (t.x + t.y <= N && vis[t.x + t.y][0][t.z] == 0) {
			vis[t.x + t.y][0][t.z] = 1;
			que.push(node(t.x + t.y, 0, t.z, t.step + 1));
		}
		if (t.y + t.x > M && vis[t.x + t.y - M][M][t.z] == 0) {
			vis[t.x + t.y - M][M][t.z] = 1;
			que.push(node(t.x + t.y - M, M, t.z, t.step + 1));
		} else if (t.y + t.x <= M && vis[t.x + t.y][0][t.z] == 0) {
			vis[t.x + t.y][0][t.z] = 1;
			que.push(node(t.x + t.y, 0, t.z, t.step + 1));
		}
	}
	printf("NO\n");
}

int main() {
	while (scanf("%d %d %d", &S, &N, &M) && (S + N + M)) {
		if (S % 2 != 0) {
			printf("NO\n");
			continue;
		}
		bfs();
		memset(vis, 0, sizeof(vis));
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin带你飞」专题五并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值