dfs

HDU1016 Prime Ring Problem DFS

。_饼干妹妹 提交于 2020-01-14 23:24:47
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, …, n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Input n (0 < n < 20). Output The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order. You are to write a program that completes above process. Print a

Lake Counting

断了今生、忘了曾经 提交于 2020-01-14 22:04:29
洛谷 P1596 [USACO10OCT]湖计数Lake Counting 思路: 从任意W开始,不停地将邻接部分用‘.’代替 ,一次dfs后与初始地w连接地所有W就都替换成了‘.’; 直到图中不存在W为止,进行dfs的次数即为所求。 代码: # include <bits/stdc++.h> using namespace std ; const int maxn = 1e2 ; int N , M ; char field [ maxn ] [ maxn + 1 ] ; void dfs ( int x , int y ) { field [ x ] [ y ] = '.' ; for ( int dx = - 1 ; dx <= 1 ; dx ++ ) //遍历W附近的8个点 for ( int dy = - 1 ; dy <= 1 ; dy ++ ) { int nx = x + dx , ny = y + dy ; if ( 0 <= nx && nx < N && 0 <= ny && ny < M && field [ nx ] [ ny ] == 'W' ) //位置合法且是水洼 dfs ( nx , ny ) ; } } int main ( ) { cin >> N >> M ; for ( int i = 0 ; i < N ; i ++ ) for (

Educational Codeforces Round 78 (Rated for Div. 2)E(构造,DFS)

↘锁芯ラ 提交于 2020-01-14 19:07:11
DFS,把和当前结点相连的点全都括在当前结点左右区间里,它们的左端点依次++,然后对这些结点进行DFS,优先对左端点更大的进行DFS,这样它右端点会先括起来,和它同层的结点(后DFS的那些)的区间会把它括起来,这样它们就不会相交了。 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int cnt=1; 5 vector<int>v[500007]; 6 int l[500007],r[500007]; 7 void dfs(int x,int fa){ 8 for(int i=0;i<v[x].size();++i) 9 if(v[x][i]!=fa) 10 l[v[x][i]]=++cnt; 11 r[x]=++cnt; 12 for(int i=v[x].size()-1;i>=0;--i) 13 if(v[x][i]!=fa) 14 dfs(v[x][i],x); 15 } 16 int main(){ 17 ios::sync_with_stdio(false); 18 cin.tie(NULL); 19 cout.tie(NULL); 20 int n; 21 cin>>n; 22 for(int i=1;i<n;++i){ 23 int x,y; 24

D - DFS HDU - 2660

夙愿已清 提交于 2020-01-14 02:09:51
D - DFS HDU - 2660 I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won't accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable necklace my mother will accept. Input The first line of input is the number of cases. For each case, the first line contains two integers N (N <= 20), the total number of stones, and K (K <= N), the exact number of stones to make a necklace. Then N lines follow, each containing two integers: a (a<=1000), representing the value of each precious

dfs枚举

心不动则不痛 提交于 2020-01-14 00:30:58
题意:n件物品,给你x个箱子,每个箱子的容量为w,问这些箱子能否装下这n件物品。 解法:枚举物品搜索每一个箱子,找到就放没找到就回溯(挪动其他物品)。 优化:1、物品从大到小排序,大的先放下,可以避免多次回溯(挪动)。2、枚举到第 l 个物品时,只需要在前 l 个箱子找合适的箱子,因为最坏的情况就是前 l 个物品(<w)最多放前 l 个箱子。 好像还有种状压dp的解法待续。 #include<stdio.h> #include<string.h> #include<math.h> #include<queue> #include<algorithm> #include<iostream> #include<map> #define inf 0x3f3f3f3f #define ll long long #define maxx 5000000 #define mod 2147493647//注意这是一个ll型的数,会爆int using namespace std; int a[30] ; int b[30] ; int n , x , w ; int ok ; bool cmp(int a, int b) { return a > b ; } void dfs(int l) { if(ok) return ; if(l >= n)//表示n个物品全部放入箱子中 { ok = 1

2019 GDUT 新生专题I选集 B题(POJ - 2386)

孤者浪人 提交于 2020-01-13 22:14:07
B - DFS/BFS 链接 来源: POJ-2386 题目描述 给出一份由’W’和’.'组成的图,算出其中水池的个数。其中水池由满足 八个方向(上下、左右、对角线方向)至少有一个方向上有’W’的 ’W’组成。 题目分析 遍历图中的每一个点,遇到’W’就开始dfs,当周围没有未访问过的’W’就停止。为了便于判断其他的水池,将访问过的’W’都换成’.’(参考了白书) 代码 # include <stdio.h> int a , b ; char field [ 100 ] [ 100 ] ; void dfs ( int x , int y ) { field [ x ] [ y ] = '.' ; for ( int dx = - 1 ; dx <= 1 ; dx ++ ) { for ( int dy = - 1 ; dy <= 1 ; dy ++ ) { int tx = x + dx , ty = y + dy ; if ( tx >= 0 && ty >= 0 && tx < a && ty < b && field [ tx ] [ ty ] == 'W' ) dfs ( tx , ty ) ; //边界有四条 } } } int main ( ) { scanf ( "%d%d" , & a , & b ) ; getchar ( ) ; for ( int i = 0

简单DFS

懵懂的女人 提交于 2020-01-13 21:35:34
题目: 给定一个正整数n,请编写一个程序来寻找n的一个非零的倍数m,这个m应当在十进制表示时每一位上只包含0或者1。你可以假定n不大于200且m不多于100位。 提示:本题采用Special Judge,你无需输出所有符合条件的m,你只需要输出任一符合条件的m即可。 Input 输入包含多组数据,每组数据仅一行,只包含一个正整数n (1 <= n <= 200). Output 对于输入的每组n,都输出任一符合条件的m。即使有多个符合条件的m,你也只需要输出一个即可。 Sample Input 2 6 19 0 Sample Output 10 100100100100100100 111111111111111111 题解: 这道题说是n小于100位,其实连20位都没有,用long long就可以存下了,所以我们其实可以从1开始递归,求出由1和0组成的所有可能的数,再%n,即可解答。 代码如下: # include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace std ; int flag = 0 ; long long n = 1 , k ; void dfs ( long long int n , int w ) { if ( flag )

2019_GDUT_新生专题I选集DFS/BFS解连通块问题

跟風遠走 提交于 2020-01-13 17:06:32
2019_GDUT_新生专题I选集DFS/BFS解连通块问题 题目链接: http://poj.org/problem?id=2386 题意:由于最近的降雨,在农夫约翰田地的不同地方积聚了水,用N x M(1 <= N <= 100; 1 <= M <= 100)正方形的矩形表示。 每个方格包含水(‘W’)或旱地(’。’)。 农夫约翰想弄清楚他的田地里形成了多少个池塘。 池塘是一组相连的正方形,里面有水,其中一个正方形被认为与八个池塘相邻。给定农夫约翰的田野图,确定他有多少个池塘。 搜索里面经典的连通块问题,思路是比较简单的,在找到某个地方有积水后,从这一格开始往八个不同的方向找有没有积水。 代码: # include <iostream> # include <cstdio> # include <cmath> # include <algorithm> using namespace std ; int map [ 1005 ] [ 1005 ] ; int ax [ 8 ] = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; int ay [ 8 ] = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; void dfs ( int x , int y ) { if ( ! map [ x ] [ y ]

C - DFS/BFS POJ - 1979

故事扮演 提交于 2020-01-13 01:38:15
C - DFS/BFS POJ - 1979 There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. Write a program to count the number of black tiles which he can reach by repeating the moves described above. Input The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not

浅谈树的直径

拈花ヽ惹草 提交于 2020-01-12 20:23:04
浅谈树的直径 定义:   树的直径指树上最长链(最远点对) 求解:    树的直径存在两种求解方式均为O(n)复杂度,其各有优劣 1.贪心法   任取一点作为起点,找到树上距离该点的最远点,记作st,再以st为起点,找到树上距离st最远的点,记作ed,st至ed即为树的直径。   (找最远点操作DFS和BFS均可)   优点:起点与终点方便获得。    缺点:负边权就GG。 inline void dfs(int now,int fa,int deep) { if(deep>res) { res=deep; ed=now; } f[now]=fa; dep[now]=deep; for(int i=head[now];i;i=a[i].nxt) { int t=a[i].to; if(t==fa) continue; dfs(t,now,deep+a[i].val); } } dfs(1,0,0); res=0; st=ed; dfs(st,0,0); 2.树型DP    任取一点作为起点,记录树上每一点向下的最远距离和非严格次远距离,直径长度即为每一点二者之和的最大值。   优点:能处理负边权。   缺点:起点终点难以记录。 inline void dfs(int now,int fa) { for(int i=head[now];i;i=a[i].nxt) { int t=a