Bayan 2015 Contest Warm Up

算法与编程技巧解析
本文深入探讨了编程中常见的算法应用及实现技巧,包括但不限于排序、动态规划、哈希算法等核心概念,以及如何在实际项目中高效地解决复杂问题。

Bayan 2015 Contest Warm Up

题目链接

A:水题,把对应位置和标号一一匹配上,然后每次k多少对应就去坐多少即可

B:其实只要找周围4条,看能不能形成一个环,因为如果不成环,肯定有一个点会到不了,如果成环,那么中间任意点,就可以到达

C:找出左上角的点,然后以一边为长,枚举另一边的长度,然后dfs一遍,dfs的过程利用容斥可以加速

D:记录下gcd,每次多一个数字,就和前面的数字都取gcd,然后合并掉,下次利用这些已有的gcd去做,然后合并的过程要利用到gcd的递减性质,就可以直接从头往后找即可

代码:

A:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int k;
string ans[6];

int x[35], y[35];

int main() {
	ans[0] = "+------------------------+";
	ans[1] = "|#.#.#.#.#.#.#.#.#.#.#.|D|)";
	ans[2] = "|#.#.#.#.#.#.#.#.#.#.#.|.|";
	ans[3] = "|#.......................|";
	ans[4] = "|#.#.#.#.#.#.#.#.#.#.#.|.|)";
	ans[5] = "+------------------------+";
	x[0] = 1; y[0] = 1;
	x[1] = 2; y[1] = 1;
	x[2] = 3; y[2] = 1;
	x[3] = 4; y[3] = 1;
	int cnt = 3;
	for (int i = 4; i < 34; i++) {
		if ((i - 4) % 3 == 0) {
			x[i] = 1; y[i] = cnt;
		}
		if ((i - 4) % 3 == 1) {
			x[i] = 2; y[i] = cnt;
		}
		if ((i - 4) % 3 == 2) {
			x[i] = 4; y[i] = cnt;
			cnt += 2;
		}
	}
	int k;
	scanf("%d", &k);
	for (int i = 0; i < k; i++)
		ans[x[i]][y[i]] = 'O';
	for (int i = 0; i < 6; i++)
		cout << ans[i] << endl;
	return 0;
}

B:

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

int n, m;
char a[25], b[25];

bool judge(char up, char down, char left, char right) {
	if (up == down) return false;
	if (left == right) return false;
	if (up == '<' && left == '^') return false;
	if (down == '<' && left == 'v') return false;
	if (up == '>' && right == '^') return false;
	if (down == '>' && right == 'v') return false;
	return true;
}

int main() {
	scanf("%d%d", &n, &m);
	scanf("%s%s", a, b);
	char up = a[0], down = a[n - 1];
	char left = b[0], right = b[m - 1];
	if (judge(up, down, left, right)) printf("YES\n");
	else printf("NO\n");
	return 0;
}

C:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;

#define sum(x1,y1,x2,y2) (g[x2][y2] - g[x1 - 1][y2] - g[x2][y1 - 1] + g[x1 - 1][y1 - 1])

const int INF = 0x3f3f3f3f;

const int N = 1005;
int n, m, g[N][N], ans = INF;
char str[N][N];

int dfs(int x,int y,int wx,int wy) {
	if(sum(x, y + 1, x + wx - 1, y + wy) == wx * wy) return wx + dfs(x, y + 1, wx, wy);
	if(sum(x + 1, y, x + wx, y + wy - 1) == wx * wy) return wy + dfs(x + 1, y, wx, wy);
	return 0;
}

int main() {
	scanf("%d%d",&n,&m);
	int flag = 0,px,py;
	for(int i = 1; i <= n; i++) scanf("%s",str[i] + 1);
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) {
			if(str[i][j] == 'X') {
				if(!flag) {flag = 1; px = i; py = j;}
				g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1] + 1;
			}
			else g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];
		}
	int tmp,wx,wy;
	for(tmp = px; str[tmp][py] == 'X'; tmp++);
	wx = tmp - px;
	for(int i = py; str[px][i] == 'X'; i++)
		if(dfs(px, py, wx, i - py + 1) + wx * (i - py + 1) == g[n][m])
			ans = min(ans, wx * (i - py + 1));
	for(tmp = py; str[px][tmp] == 'X'; tmp++);
	wy = tmp - py;
	for(int i = px; str[i][py] == 'X'; i++)
		if(dfs(px, py, i - px + 1, wy) + (i - px + 1) * wy == g[n][m]) 
			ans = min(ans, (i - px + 1) * wy);
	if (ans == INF) ans = -1;
	printf("%d\n", ans);
	return 0;
}

D:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;

typedef long long ll;
typedef pair<int, ll> pii;
const int N = 100005;

int a[N];
pii save[N];
map<int, ll> ans;

int n;

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

void build() {
	int top = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < top; j++) save[j].first = gcd(save[j].first, a[i]);
		save[top++] = make_pair(a[i], 1LL);
		int sn = 1;
		for (int j = 1; j < top; j++) {
			if (save[sn - 1].first == save[j].first) save[sn - 1].second += save[j].second;
			else save[sn++] = save[j];
		}
		top = sn;
		for (int j = 0; j < top; j++) {
			ans[save[j].first] += save[j].second;
		}
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	build();
	int q;
	scanf("%d", &q);
	while (q--) {
		int x;
		scanf("%d", &x);
		printf("%lld\n", ans[x]);
	}
	return 0;
}


【源码免费下载链接】:https://renmaiwang.cn/s/nhrcw 深度优先搜索(DFS,Depth-First Search)是一种用于遍历或搜索树或图的算法,它选择一个节点并尽可能深地探索其分支。在迷宫生成中,DFS被用来创建复杂的路径结构。以下是对给定内容的详细解释:1. **Python深度优先算法生成迷宫的原理**: 迷宫生成的基本思想是随机地在空白区域添加墙壁,形成一条可以从起点到终点的路径。DFS在这里的作用是从起始点开始,随机选择一个方向进行移动,并将该路径标记为已访问。当遇到障碍(已存在的墙壁)或者到达终点时,算法回溯到上一步,选择其他未尝试过的路径。2. **代码解析**: - 定义矩阵`dfs`来记录迷宫中每个单元格是否已被访问。 - 定义矩阵`maze`来表示最终生成的迷宫,其中`#`代表墙壁,空格代表可通行路径。 - `operation`字典存储了四个可能的方向(上、下、左、右)对应的坐标偏移量。 - `direction`列表包含所有可能的方向,用于随机选择移动方向。 - `stack`用于存储深度优先搜索过程中的路径。3. **函数说明**: - `show(graph)`:打印迷宫矩阵,便于观察迷宫结构。 - `showRouter(stack)`:打印DFS过程中访问的路径,展示从起点到终点的路径。 - `generateMaze(start)`:核心函数,使用DFS生成迷宫。首先将起始点标记为已访问,然后随机排序方向,依次尝试这些方向,如果新位置未被访问且在有效范围内,则打通墙壁并递归调用自身。4. **迷宫生成流程**: - 创建一个全墙的初始迷宫矩阵,奇数行和奇数列的位置代表实际的墙壁,偶数位置代表路径。 - 起点设为`(0, 0)`,调用`generateMaze((0,0))`开始生成迷宫。 - 在递归过程中,每次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值