蓝桥杯复习8天(day4)

目录

1.ASC

 ​

2、修改数组(19年省赛——3月28日)

3、大胖子走迷宫(19年国赛——3月25日)

4、数字三角形(20年省赛——3月19日)


1.ASC

 

答案:76 


2、修改数组(19年省赛——3月28日)

注意:初始化的时候初始到1e6+5

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
int n,a[100005],p[1000010];

void init() {
	for(int i=1; i<=1000005; i++)
	p[i]=i;
}

int find(int x) {
	return p[x]==x ? x : p[x]=find(p[x]);
}

int main() {
	init();
	cin>>n;
	for(int i=1; i<=n; i++)
	cin>>a[i];
	for(int i=1; i<=n; i++) {
		int x=find(a[i]);
		p[x]++;
		cout<<x<<" ";
	}

	return 0;
}


3、大胖子走迷宫(19年国赛——3月25日)

 

 注意:5*5 3*3可以在原地不动,需要特判,起点终点不是两个角。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
const int dr[]= {0,0,-1,1};
const int dc[]= {1,-1,0,0};
int S,n,k,vis[305][305];
char a[305][305];

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

bool inside(int xx,int yy) {
	if((xx+S/2)>=1 && (xx+S/2)<=n && (yy+S/2)>=1 && (yy+S/2)<=n) return true;
	else return false;
}

bool judge(int xx,int yy) {
	for(int i=xx-S/2; i<=xx+S/2; i++)
		for(int j=yy-S/2; j<=yy+S/2; j++)
			if(a[i][j]=='*') return false;
	return true;
}

void bfs() {
	queue<node> q;
	node u(3,3,0);
	vis[3][3]=1;
	q.push(u);
	while(!q.empty()) {
		node u=q.front();
		q.pop();
		if(u.x==n-2 && u.y==n-2) {
			cout<<u.t<<endl;
			return;
		}
		//判定体型
		if(u.t<k) S=5;
		if(u.t>=k && u.t<2*k) S=3;
		if(u.t>=2*k) S=1;
		if(S!=1) q.push(node(u.x, u.y, u.t + 1));
		for(int i=0; i<4; i++) {
			int nx=u.x+dr[i];
			int ny=u.y+dc[i];
			if(inside(nx,ny) && judge(nx,ny) && !vis[nx][ny]) {
				vis[nx][ny]=1;
				node v(nx, ny, u.t + 1);
				q.push(v);
			}
		}
	}
}



int main() {
	cin>>n>>k;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			cin>>a[i][j];
	bfs();
	return 0;
}


4、数字三角形(20年省赛——3月19日)

 注意:虽然是一道线性dp问题,还是一个经典题,可是有要求:向左下走的次数与向右下走的次数相差不能超过 1。

数字三角形可以自顶向下划分,也可以自底向上划分。不过这题有要求,所以自顶向下更容易控制,直接取最后一行的中间元素即可

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define MOD 1000000009
using namespace std;
typedef long long LL;
int dp[105][105],a[105][105],n;
int maxn;
int main() {
	cin>>n;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=i; j++)
			cin>>a[i][j];
	dp[1][1]=a[1][1];
	for(int i=2; i<=n; i++)
		for(int j=1; j<=i; j++)
			dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+a[i][j];
	if(n%2) cout<<dp[n][n/2+1];
	else cout<<max(dp[n][n/2],dp[n][n/2+1]);
	return 0;
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_谦言万语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值