zl程序教程

您现在的位置是:首页 >  其它

当前栏目

codeforces B. Strongly Connected City(dfs水过)

Codeforces DFS Connected City
2023-09-14 08:57:55 时间
题意:有横向和纵向的街道,每个街道只有一个方向,垂直的街道相交会产生一个节点,这样每个节点都有两个方向,
问是否每一个节点都可以由其他的节点到达....
思路:规律没有想到,直接爆搜!每一个节点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