1. Title Description: Click to open the link
2. Problem-Solving ideas: Use DFS to solve this problem. The subject asks whether there is a circle of the same color in a graph. Obviously need to use DFS to find. So how do you find it? The topic has already told us how to judge a circle. Then just write DFS based on the test instructions description. Starting with a node that has not been searched, each time you look for a node that is adjacent to it and has the same color, you need to add a precursor node to the DFS parameter list in order to prevent repeated expansion. In this way, once a node has been found to have been marked, it indicates that a circle has been found. Output yes directly and exit the loop.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 110int Vis[n][n];char g[n][n]; const int dx[] = {-1, 1, 0, 0};const int dy[] = {0, 0,-1, 1};int N, m;bool inside (int x, int y) {return x >= 0 &am p;& x < n&&y >= 0 && y < m; }bool dfs (int x, int y, int prex, int prey, char c) {Vis[x][y] = 1;for (int i = 0; i < 4; i++) {int xx = x + dx[i];int yy = y + dy[i];if (xx = = Prex&&yy = = Prey) continue;//If found exactly equal to the predecessor node, skip if (Inside (xx, yy) && g[xx][yy] = = c) {if (Vis[xx][yy]) return true;//found a node that was once marked, stating that it formed a circle if (Dfs (xx, yy, X, Y, c)) return true;}} return false;} int main () {//freopen ("T.txt","R", stdin), while (~SCANF ("%d%d", &n, &m)) {for (int i = 0; i < n; i++) scanf ("%s", G[i]), memset (Vis, 0, sizeof (v is)) (int i = 0; i < n;i++) for (int j = 0; J < m;j++) if (!vis[i][j]) {if (Dfs (i, J,-1,-1, g[i][j])) {puts ("yes") ; goto X1;}} Puts ("No"); X1:;} return 0;}
#290 (Div.2) b. Fox and both Dots