codeforces B. Strongly Connected City(dfs水过)
Codeforces DFS Connected City
2023-09-14 08:57:55 时间
题意:有横向和纵向的街道,每个街道只有一个方向,垂直的街道相交会产生一个节点,这样每个节点都有两个方向,
问是否每一个节点都可以由其他的节点到达....
思路:规律没有想到,直接爆搜!每一个节点dfs一次,记录每个节节点被访问的次数!如果每个节点最终的访问次数
LeetCode 321. Create Maximum Number 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k = m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。 求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。
AtCoder--755——dfs 题目描述 You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally “Seven-Five-Three numbers”) are there? Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution us
问是否每一个节点都可以由其他的节点到达....
思路:规律没有想到,直接爆搜!每一个节点dfs一次,记录每个节节点被访问的次数!如果每个节点最终的访问次数
和所有节点的数目相同,则输出“YES", 否则输出”NO“
#include queue #include string #include cstdio #include cstring #include iostream #include algorithm using namespace std; char s1[25], s2[25]; int vis[25][25]; int cnt[25][25]; int n, m; void dfs(int x, int y){ if(x 1 || x n || y 1 || y m || vis[x][y]) return; ++cnt[x][y]; vis[x][y] = 1; if(s1[x] == ) dfs(x, y-1); else dfs(x, y+1); if(s2[y] == v) dfs(x+1, y); else dfs(x-1, y); int main(void) scanf("%d%d", n, scanf("%s%s", s1+1, s2+1); for(int i=1; i ++i) for(int j=1; j ++j){ memset(vis, 0, sizeof(vis)); dfs(i, j); int s = n*m; for(int i =1; i ++i) for(int j=1; j ++j) if(cnt[i][j] != s){ cout "NO" endl; return 0; cout "YES" endl; return 0; }
LeetCode 321. Create Maximum Number 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k = m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。 求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。
AtCoder--755——dfs 题目描述 You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally “Seven-Five-Three numbers”) are there? Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward solution us
相关文章
- Codeforces 414B
- 【题解】codeforces-1194B
- CodeForces – 1312D 组合数
- Codeforces Round #784 (Div. 4)(A~F)
- Codeforces Global Round 23(A~C)
- Codeforces Round #828 (Div. 3) (A~D)
- C. Polycarp Restores PermutationCodeforces Round #547 (Div. 3)
- Codeforces Round#806(Div.4) 题解
- C. Prefixes and Suffixes ( Codeforces Round #527 (Div. 3) )
- Codeforces 842B Gleb And Pizza几何,水详解编程语言
- Codeforces 842A Kirill And The Game暴力,水详解编程语言