lqb省赛日志[6/37]

本文记录了一位小菜鸟在备考蓝桥杯过程中的笔记,重点介绍了DFS算法的应用,如层序遍历和华容道问题的解法,以及队列在解决问题中的巧妙运用。作者还分享了卡片换位题目的心得和省赛准备不足的反思。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一只小蒟蒻备考蓝桥杯的日志

笔记

遍历 (DFS主题)

参考 BFS 的使用场景:层序遍历、最短路径问题
DFS -> 层次遍历 -> 无权图的最短路径 (Dijkstra 算法平替)

实现:
用队列存储, 出队, 孩子进队

隐式图遍历:
华容道, 出发点是空的那个位置, 真难想到

#include <stdio.h>
#include <iostream>
#include <queue>
#include <map>

using namespace std;

int mov[4][3] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
//                right    down    left      up

map<string, int> dist;

int main() {
	string str1;
	string str2;
	getline(cin, str1);
	getline(cin, str2);
	string str = str1 + str2;
	int A = str.find('A');
	int B = str.find('B');
	queue<string> q;
	q.push(str);
	int ans;
	dist[str] = 0;
	
	while(q.empty() == false){
		string tmp = q.front();
		q.pop();
//		cout << tmp << endl;
		
		if(A == tmp.find('B') && B == tmp.find('A')) {
			ans = dist[tmp];
			break;
		}
		
		int index = tmp.find(' ');
		int x = index % 3;
		int y = index / 3;
		int distance = dist[tmp];
		
		for(int i = 0; i < 4; i++) {
			int x1 = x + mov[i][0];
			int y1 = y + mov[i][1];
			
			if(x1 < 0 || x1 > 2 || y1 < 0 || y1 > 1) {
				continue;
			}
			
			int xy2_1 = y1 * 3 + x1;
			swap(tmp[xy2_1], tmp[index]);
			if(dist.count(tmp) == 0) { //not exist
				q.push(tmp);
				dist[tmp] = distance + 1;
			}
//			cout << "swap: " << tmp << endl;
			swap(tmp[xy2_1], tmp[index]);
			
		}
	}
	
	cout << ans << endl;
	return 0;
}

刷题

  1. 卡片换位

心得

  1. 第1题, 挺好的, 学到很多
    1. 队列的运用queue, q.front(), q.pop(), q.push()

    2. 是否走过该位置, 那个二维换成一维比对的方法很巧,
      if(dist.count(tmp) == 0) //not exist

      int x = index % 3;
      int y = index / 3;
      int xy2_1 = y1 * 3 + x1; //二维->一维

    3. map的运用, map<string, int> dist;

    4. 上下左右移位, 原来可以通过存一个数组, 遍历实现, int mov[4][3] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

    5. 建议背诵笔记第一点demo, 因为小蒟蒻一边一定记不住这些用法

小结

为什么省赛日志没有 [5/37] 呢,因为昨天没准备…标题其实是倒计时…

好heartbroken的一天,很疲惫,但很失意和徒劳无获,可能是前端吧…
又水了一篇,水了一天
“聪明的,你告诉我,为什么我们的日子一去不复返了呢?”

“业精于勤荒于嬉,行成于思毁于随”
小蒟蒻一个月,冲省一!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值