0x20 搜索
文章平均质量分 91
搜索,dfs、bfs、记忆化搜索、剪枝
默_silence
记得一定要努力,不然往后余生,做饭是你,洗衣是你,做家务是你……是你是你还是你
展开
-
星星之夜(dfs)
夜空深处,闪亮的星星以星群的形式出现在人们眼中,形态万千。一个星群是指一组非空的在水平,垂直或对角线方向相邻的星星的集合。一个星群不能是一个更大星群的一部分。星群可能是相似的。如果两个星群的形状、包含星星的数目相同,那么无论它们的朝向如何,都认为它们是相似的。通常星群可能有 8 种朝向,如下图所示:现在,我们用一个二维 01 矩阵来表示夜空,如果一个位置上的数字是 1,那么说明这个位置上有一个星星,否则这个位置上的数字应该是 0。给定一个夜空二维矩阵,请你将其中的所有星群用小写字母进行标记,原创 2021-01-27 22:48:25 · 251 阅读 · 2 评论 -
poj1191 棋盘分割(记忆化搜索)
题目传送门:http://poj.org/problem?id=1191Description将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘。(每次切割都只能沿着棋盘格子的边进行)原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和。现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小。均方差 σ=Σi=1n(xi−xˉ)2nσ=\sqrt \原创 2021-01-16 13:53:47 · 323 阅读 · 1 评论 -
D - Fliptile
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one原创 2020-09-15 21:43:11 · 159 阅读 · 0 评论 -
I - Fire Game
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids wh原创 2020-09-15 13:19:40 · 103 阅读 · 0 评论 -
F - Prime Path
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.— It is a matter of security to change such things every now and then, to keep原创 2020-09-14 22:36:34 · 327 阅读 · 0 评论 -
Summer Online Training Camp 1 for ICPC Training League (Recursion & Backtracking)
文章目录A - 放苹果F - The Sultan's SuccessorsA - 放苹果把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。Input第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。Output对输入的每组数据M和N,用一行输出相应的K。Sample Input17 3Sample Output原创 2020-07-26 12:13:10 · 465 阅读 · 0 评论 -
POJ1664 放苹果
放苹果把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。Input第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。Output对输入的每组数据M和N,用一行输出相应的K。Sample Input17 3Sample Output8dfs枚举,因为不能有重复的情况,那么就从小到大进行枚举,每次选的数字要大于等于上原创 2020-07-17 14:00:27 · 221 阅读 · 0 评论 -
滑雪(记忆化搜索)
链接:https://ac.nowcoder.com/acm/problem/105685来源:牛客网题目描述Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子1 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11原创 2020-06-29 17:48:40 · 394 阅读 · 0 评论 -
比赛(dfs枚举)
链接:https://ac.nowcoder.com/acm/problem/14734来源:牛客网题目描述你在打比赛,这场比赛总共有12个题对于第i个题,你的队伍有a[i]的几率解决她如果解决不了她呢?由于所有人讨论的都很大声所以你有b[i]的概率从左边那个队那里听会这个题的做法有c[i]的概率从右边那个队那里听会这个题的做法请问最终你们队伍解出0-12题的概率分别是多少输入描述:第一行12个数表示a[1] -> a[12]第二行12个数表示b[1] -> b[12]原创 2020-06-27 20:54:06 · 280 阅读 · 0 评论 -
P2895 [USACO08FEB]Meteor Shower S
题目链接:https://www.luogu.com.cn/problem/P2895题目描述Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe loca原创 2020-06-12 09:35:56 · 442 阅读 · 0 评论 -
暴力枚举、搜索、动态规划
四种面值的货币:1, 5, 10, 25,每种均有无数张可用,给定一个整数aim表示要凑齐的金额,计算有多少种方法可以凑齐?可以看出,上面的题目是一个完全背包问题,我们尝试用五种方法来对它进行求解,来体会不同方法的差异,了解动态规划的推导暴力枚举方法#include <iostream>using namespace std;int v[] = {1, 5, 10, 25};int main(void){ int res = 0, aim;原创 2020-06-07 19:29:41 · 2386 阅读 · 0 评论 -
P1135 奇怪的电梯
宽搜模板题,好久不写已经快忘记怎么写了#include <iostream>#include <queue>#include <cstring> using namespace std;const int INF = 0x3f3f3f3f;int arr[205], d[205];int n, a, b;int bfs(){ que...原创 2020-04-12 10:21:29 · 270 阅读 · 0 评论 -
dfs模板
需要注意,state == t 一定要放在最前面#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int a[25];void dfs(int u, int state){ if (state == t){//达到某种条件 ...原创 2020-03-15 16:13:37 · 135 阅读 · 0 评论 -
Sum It Up
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four di...原创 2020-03-15 15:59:06 · 249 阅读 · 0 评论 -
容斥原理模板
题目链接:https://ac.nowcoder.com/acm/problem/14686来源:牛客网集合中的质数题目描述给出一个集合和一个数m。 集合里面有n个质数。 请你求出从 1 到 m 的所有数中,至少能被集合中的一个数整除的数的个数。输入描述:第一行两个正整数 n 和 m 。第二行n个正整数,分别为集合中的质数。输出描述:输出一个整数,表示符合要求的正整数的...原创 2020-02-09 17:36:49 · 279 阅读 · 0 评论 -
N - Find a way
DescriptionPass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.Yifenfei’s home i...原创 2019-05-30 20:17:54 · 119 阅读 · 0 评论 -
B - Dungeon Master
DescriptionYou are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit ...原创 2019-05-30 17:10:20 · 147 阅读 · 0 评论 -
E - Find The Multiple
DescriptionGiven a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1.You may assume that n is not greater than 20...原创 2019-05-30 15:48:07 · 173 阅读 · 0 评论 -
C - Catch That Cow
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,0...原创 2019-05-19 12:59:55 · 116 阅读 · 0 评论 -
L - Oil Deposits
DescriptionThe GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid tha...原创 2019-04-18 19:49:09 · 166 阅读 · 0 评论 -
K - 迷宫问题
Description定义一个二维数组:int maze[5][5] = {0, 1, 0, 0, 0,0, 1, 0, 1, 0,0, 0, 0, 0, 0,0, 1, 1, 1, 0,0, 0, 0, 1, 0,};它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。Input一个5 × ...原创 2019-04-17 22:40:01 · 232 阅读 · 0 评论 -
A - 棋盘问题
Description在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。Input输入含有多组测试数据。每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , ...原创 2019-04-16 21:21:33 · 172 阅读 · 0 评论 -
A - Flip Game
DescriptionInputThe input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.OutputWrite to the output file a single integer number - the minimum number of roun...原创 2019-04-10 23:49:25 · 242 阅读 · 0 评论 -
剪枝
167. 木棒坑点1:最开始我每次枚举的是小木棍,因为不会剪枝,过不了坑点2:需要忽略掉大于50的数据坑点3:代码35行的两个剪枝必须要有,否则就会超时(感觉这道题的剪枝卡到极限了)#include <bits/stdc++.h>using namespace std;int n, cnt, len, sum; // 小木棒,每根小木棒是否被使用过 int a[100], vis[100];bool cmp(int a, int b){ return a >原创 2020-06-30 22:26:27 · 210 阅读 · 0 评论 -
总结与练习
189. 乳草的入侵注意输入的行和列是相反的#include <bits/stdc++.h>using namespace std;const int N = 105;typedef pair<int, int> P;int x, y, mx, my;int d[N][N];char m[N][N];int res;int bfs(){ queue<P> que; m[mx][my] = 'M'; que.push(P(mx,原创 2020-06-17 15:36:25 · 153 阅读 · 0 评论 -
深度优先搜索
165. 小猫爬山每次安排猫的时候有两种方法:用已经使用过的车来装、用没有使用过的车来装需要进行剪枝,否则会TLE#include <iostream>#include <algorithm>using namespace std;int n, w, res;int c[20], rest[20];//已经安排了 u 只猫,使用了 state 辆车 void dfs(int u, int state){ //这个剪枝非常重要,没有就会超时 if (原创 2020-06-16 23:48:56 · 140 阅读 · 0 评论 -
树与图的遍历
164. 可达性统计一开始直接暴力搜,结果TLE了#include <cstdio>#include <cstring>#include <vector> #include <algorithm>using namespace std;const int N = 3e4 + 5;bool vis[N];vector<...原创 2020-03-01 20:38:42 · 277 阅读 · 0 评论 -
广度优先搜索
173. 矩阵距离如果全部搜一遍一定会超时,所以需要将所有的1全部入队,再进行宽搜,这样记录的距离就是最短距离#include <iostream>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int N = 1e3...原创 2020-01-28 19:47:18 · 186 阅读 · 0 评论