pezhzh的快乐寒假作业[2024/1/18]

文章讲述了作者在解决洛谷编程题时,从使用深度优先搜索(DFS)遇到时间复杂度过高,到学习并应用状态压缩动态规划解决迷宫和单词游戏问题的过程,以及如何应对自然数拆分问题的直接暴力解法。
摘要由CSDN通过智能技术生成

这个虽说是1/18的寒假作业,但也如上篇文章所说的那样,现在是4/4,我在补作业;

A - 吃奶酪

洛谷:P1433;

看到这种当然会想到dfs搜索;

于是写了一个带小剪枝的dfs试试水;

#include<bits/stdc++.h>
using namespace std;
struct awa{
	double x, y;
}b[20];
int n, a[20];
double res = 1000;
double way(int i, int t){
	return sqrt(pow(b[i].x - b[t].x, 2) + pow(b[i].y - b[t].y, 2));
}
void dfs(int x, int t, double num){
	if(num > res) return;
	if(t == n){
		res = num;
		return;
	}for(int i = 1; i <= n; i ++){
		if(!a[i]){
			a[i] = 1;
			dfs(i, t + 1, num + way(x, i));
			a[i] = 0;
		}
	}
}
int main(){
	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> b[i].x >> b[i].y;
	a[0] = 1;
	dfs(0, 0, 0);
	printf("%.2f", res);
}

没想到绿题确实有它的本事;

难受,特别看到加强测试点一看就是为了卡我TLE的;

于是疯狂地找规律,画图,看哪里还有没剪的枝;

一万年过去了.......

顶不住去看一下讨论版吧;

QAQ;

紧急学一下状态压缩DP;

在经过的节点与最终的落点相同时,记录最短的dp结果;

经过节点用二进制表示,1为走过了,0为没走过;

掺杂进dfs里;

#include<bits/stdc++.h>
using namespace std;
struct awa{
	double x, y;
}b[16];
int n, a[16];
double res = 1e9, dp[1 << 15][16];
double way(int i, int t){
	return sqrt(pow(b[i].x - b[t].x, 2) + pow(b[i].y - b[t].y, 2));
}
void dfs(int x, int t, double num, int k){
	if(num > res) return;
	if(t == n){
		res = num;
		return;
	}for(int i = 1; i <= n; i ++){
		if(!a[i]){
			int o = k + (1 << (i - 1));
			if(dp[o][i] != 0 && dp[o][i] <= num + way(i, x)) continue;
			a[i] = 1;
			dp[o][i] = num + way(i, x);
			dfs(i, t + 1, num + way(x, i), o);
			a[i] = 0;
		}
	}
}
int main(){
	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> b[i].x >> b[i].y;
	a[0] = 1;
	dfs(0, 0, 0, 0);
	printf("%.2f", res);
}

B - 迷宫

洛谷:P1605;

一个深搜的走迷宫;

标志性数组的1与0的区别就像是障碍物一样,但唯一的不同是我走过的路要变1再变0,但障碍只要一直是1就好;

因为懒得做边界判断所以让地图的最外边一圈都设定成1,也就是main里那两个for的作用;

#include<bits/stdc++.h>
using namespace std;
int n, m, a[6][6], enx, eny, cnt;
int dx[] = {0, -1, 1, 0};
int dy[] = {1, 0, 0, -1};
void dfs(int x, int y){
	if(a[x][y])return;
	if(x == enx && y == eny){
		cnt ++;
		return;
	}a[x][y] = 1;
	for(int i = 0; i < 4; i++)
		dfs(x + dx[i], y + dy[i]);
	a[x][y] = 0;
}
int main(){
	int bex, bey, x, y, t;
	cin >> n >> m >> t >> bex >> bey >> enx >> eny;
	for(int i = 0; i <= n + 1; i ++){
		a[0][i] = 1;
		a[m + 1][i] = 1;
	}
	for(int i = 0; i <= m + 1; i ++){
		a[i][0] = 1;
		a[i][m + 1] = 1;
	}
	while(t --){
		cin >> x >> y;
		a[x][y] = 1;
	}
	dfs(bex, bey);
	cout << cnt;
}

C - 单词接龙

洛谷:P1019;

简单的搜索,但是难点主要在找能否接龙的部分;

首先,每个单词能在龙里出现两次;

一般的标志性数组只有0和1,但是我们要存0,1和2来判定单词能否使用;

其次是如何看单词重复部分;

以当前单词为基准,下一个单词能否作为接在后面的选项的判定方法是看长度相等的当前单词的尾巴与长度相等的下一单词的头是否完全重合;(string库的find函数);

str1.find(str2, k);(从str1的第k个开始, 查找子串st2, 返回找到的位置, 找不到返回-1);

随后将能接的单词接在龙的后面(string库的replace函数);

str1.replace(k, n, str2); (从str1的第k个开始, 往后n个,替换成str2);

#include <bits/stdc++.h>
using namespace std;
int n, res, a[21], k;
string b[21], str;
char ch;
void dfs(int t, string d){
    if(d.length() > res) res = d.length();
    for(int i = 0; i < n; i++){
        if(a[i] < 2){
            for(int j = 1; j <= b[i].length(); j++){
                k = d.find(b[i].substr(0, j), d.length() - j);
                if(k != -1){
                    str = d;
                    str.replace(k, b[i].length(), b[i]);
					a[i] ++;
                    dfs(t + 1, str);
                    a[i] --;
                }
            }
        }
    }
}
int main(){
    cin >> n;
    for(int i = 0; i < n; i ++)
        cin >> b[i];
    cin >> ch;
    for(int i = 0; i < n; i ++){
        if(b[i][0] == ch){
            a[i] ++;
            dfs(1, b[i]);
        }
    }
    cout << res << endl;
    return 0;
}

一开始难死了,但是硬着头皮也能写;

D - 单词方阵

洛谷:P1101;

看到题的时候几乎没有想到任何的思路与算法,看数据量也不大的样子,就走上了暴力的不归路;

大致想法是对于所有的‘y’,都去查看它的8个方向,若每个字母都能对上yizhong,就把这7个字母存到初始都为*的答案数组里;

程序实现与bug调试耗时巨大;

#include<bits/stdc++.h>
using namespace std;
const int c1[8]={0,-1,-1,-1,0,1,1,1}, c2[8]={1,1,0,-1,-1,-1,0,1};
int n;
string hc[111], ch[111];
char ch1[] = {"yizhong"};
int main(){
	cin >> n;
	for(int i = 0; i < n; i ++){
		cin >> ch[i];
		for(int j = 0; j < n; j ++)
		hc[i] += '*';
	}
	for(int i = 0; i < n; i ++){
		for(int j = 0; j < n; j ++){
			if(ch[i][j] == 'y'){
				for(int jj = 0; jj < 8; jj ++){
					int x = i, y = j, b = 1;
					for(int k = 1; k < 7; k ++){
						x += c1[jj];
						y += c2[jj];
						if(x < 0 || x > n || y < 0 || y > n) b = 0;
						if(b) if(ch[x][y] != ch1[k]) b = 0;
						if(!b) break;
					}
					x = i, y = j;
					if(b)
						for(int k = 0; k < 7; k ++)
							hc[x][y] = ch[x][y], x += c1[jj], y += c2[jj];
				}
			}
		}
	}
	for(int i = 0; i < n; i ++)
		cout << hc[i] << endl;
}

i,j为坐标;

jj为查找的方向,对应c1与c2;

k为第几个字母,对照ch1;

ch数组为输入数组,hc数组为输出数组;

E - 自然数的拆分问题

洛谷:P2404;

哈,7个数,打表秒了(不建议)

#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
	cin >> n;
	if(n == 2){
		cout << "1+1" << endl;
	}else if(n == 3){
		cout << "1+1+1" << endl;
		cout << "1+2" << endl;
	}else if(n == 4){
		cout << "1+1+1+1" << endl;
		cout << "1+1+2" << endl;
		cout << "1+3" << endl;
		cout << "2+2" << endl;
	}else if(n == 5){
		cout << "1+1+1+1+1" << endl;
		cout << "1+1+1+2" << endl;
		cout << "1+1+3" << endl;
		cout << "1+2+2" << endl;
		cout << "1+4" << endl;
		cout << "2+3" << endl;
	}else if(n == 6){
		cout << "1+1+1+1+1+1" << endl;
		cout << "1+1+1+1+2" << endl;
		cout << "1+1+1+3" << endl;
		cout << "1+1+2+2" << endl;
		cout << "1+1+4" << endl;
		cout << "1+2+3" << endl;
		cout << "1+5" << endl;
		cout << "2+2+2" << endl;
		cout << "2+4" << endl;
		cout << "3+3" << endl;
	}else if(n == 7){
		cout << "1+1+1+1+1+1+1" << endl;
		cout << "1+1+1+1+1+2" << endl;
		cout << "1+1+1+1+3" << endl;
		cout << "1+1+1+2+2" << endl;
		cout << "1+1+1+4" << endl;
		cout << "1+1+2+3" << endl;
		cout << "1+1+5" << endl;
		cout << "1+2+2+2" << endl;
		cout << "1+2+4" << endl;
		cout << "1+3+3" << endl;
		cout << "1+6" << endl;
		cout << "2+2+3" << endl;
		cout << "2+5" << endl;
		cout << "3+4" << endl;
	}else{
		cout << "1+1+1+1+1+1+1+1" << endl;
		cout << "1+1+1+1+1+1+2" << endl;
		cout << "1+1+1+1+1+3" << endl;
		cout << "1+1+1+1+2+2" << endl;
		cout << "1+1+1+1+4" << endl;
		cout << "1+1+1+2+3" << endl;
		cout << "1+1+1+5" << endl;
		cout << "1+1+2+2+2" << endl;
		cout << "1+1+2+4" << endl;
		cout << "1+1+3+3" << endl;
		cout << "1+1+6" << endl;
		cout << "1+2+2+3" << endl;
		cout << "1+2+5" << endl;
		cout << "1+3+4" << endl;
		cout << "1+7" << endl;
		cout << "2+2+2+2" << endl;
		cout << "2+2+4" << endl;
		cout << "2+3+3" << endl;
		cout << "2+6" << endl;
		cout << "3+5" << endl;
		cout << "4+4" << endl;
	}
}

写完已经4/5的凌晨力QAQ;

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值