Algunas preguntas DFS 2020/3/11

1.

 

 

 

Ejemplo de entrada 
6 9 
.... #. 
..... # 
...... 
...... 
...... 
...... 
...... 
# @ # ... 
. # .. #. 
11 9 
. # ......... 
. #. #######. 
. #. # ..... #. 
. #. #. ###. #. 
. #. # .. @ #. #. 
. #. #####. #. 
. # ....... #. 
. #########. 
........... 
11 6 
.. # .. # .. # .. 
.. .. # # # .. .. 
.. .. .. # # ### 
.. #. . # .. # @. 
# # .. .. .. .. # 
.. # .. # .. .. # 
7 7 
.. #. # .. 
.. #. # .. 
###. ### 
... @. .. 
###. ### 
.. #. # .. 
.. #. # .. 
0 0 
Ejemplo de salida 
45
59 
6 
13

 Resolver el código de

#include <bits / STDC ++ h.> 
using namespace std; 

int const N = 100; 
bool vis [100] [100]; 
int ans = 0; 
char a [100] [100]; 
DFS void (int x, int y) { 
	int dir [10] = {0, 0, 1, 0, -1, 1, 0, -1, 0}; 
	for (int i = 1; i <= 8; i + = 2) { 
		int temp1 = x, temp2 = y; 
		temp1 + = dir [i]; 
		temp2 + = dir [i + 1]; 
		si (Temp 1> = 0 && Temp 2> = 0 && un [Temp 1] [Temp 2] == && vis [Temp 1] [Temp 2] ''!) { 
			ans ++; 
			un [Temp 1] [Temp 2] = ''; 
			vis [temp1] [temp2] = 1; 
			DFS (temp1, temp2); 
		} 
	} 
} 

Int main () { 
	int m, n; 
	mientras que (cin >> M >>
		memset (vis, 0, sizeof vis); 
		ans = 0; 
		si (m == 0 && n == 0) { 
			break; 
		} 
		For (int i = 0; i <n; ++ i) { 
			cin >> a [i]; 
		} 
		Int x, y; 
		for (int i = 0; i <n; ++ i) { 
			for (int j = 0; j <m; ++ j) { 
				si (a [i] [j] == '@') { 
					x = yo; 
					y = j; 
				} 
			} 
		} 
		// cout << x << y << endl; 
		DFS (x, y); 
		tribunal << ans + 1 << endl; 
	} 
}

 

2.

 

 

Ejemplo de entrada 
3 4 5 
3 2 
2 2 
3 1 
2 3 
1 1 
Salida de muestra 
4
// POJ 3620 
# include <iostream> 
using namespace std; 
bool vis [110] [110]; 
char a [110] [110]; 
ans int; 
int n, m, k; 
temp int; 
int DFS (int x, int y) { 
	temp ++; 
	int dir [10] = {0, 1, 0, -1, 0, 0, 1, 0, -1}; 
	for (int i = 1; i <= 8; i + = 2) { 
		vis [x] [y] = 1; //标记之前的点
		int temp1 = x + dir [i]; 
		int temp2 = y + dir [i + 1]; 
		si (temp1> = 1 && temp2> = 1 && temp1 <= m && temp2 <= n &&! vis [temp1] [temp2] && un [temp1] [temp2] == '#') { 
			DFS (temp1, temp2 ); 
		} 
	} 
	Temp retorno; 
} 
Int main () { 
	// memset (a, '0', sizeof a);
	ans = 0; 
	while (k--) { 
		int x, y; 
		cin >> x >> y; 
		una [x] [y] = '#'; 
	} 
	// for (int i = 1; i <= n; ++ i) { 
	// for (int j = 1; j <= m; ++ j) { 
	// cout << a [i] [j ]; 
	//} 
	// cout << endl; 
	//} 
	for (int i = 1; i <= m; ++ i) { 
		for (int j = 1; j <= n; j ++) { 
			if (! Vis [i] [j] && un [ i] [j] == '#') { 
				temp = 0; 
				DFS (i, j); 
				si (temp> ans) { 
					ans = temp; 
				} 
			} 
		} 
	} 
	Cout << ans << endl; 
}

 

 3.

 

 

//洛谷1605 
#include <bits / STDC ++ h.> 
Using namespace std; 
bool vis [10] [10]; 
char a [10] [10]; 
ans int; 
int sx, sy, ex, ey; 
int n, m, t; 
DFS void (int x, int y) { 
	int dir [10] = {0, 1, 0, -1, 0, 0, 1, 0, -1}; 
	si (x == ex && y == ey) { 
		ans ++; 
		regreso; 
	} 
	For (int i = 1; i <= 8; i + = 2) { 
		vis [x] [y] = 1; //标记之前的点
		int temp1 = x + dir [i]; 
		int temp2 = y + dir [i + 1]; 
		si (temp1> = 1 && temp2> = 1 && temp1 <= n && temp2 <= m &&! vis [temp1] [temp2] && un [temp1] [temp2]! = '#') { 
			DFS (temp1, temp2 ); 
			vis [temp1] [temp2] = 0;
} 
Int main () { 

	cin >> n >> m >> t; 
	cin >> >> sx sy ex >> >> ey; 
	ans = 0; 
	while (t--) { 
		int x, y; 
		cin >> x >> y; 
		una [x] [y] = '#'; 
	} 
	DFS (sx, sy); 
	ans tribunal << << endl; 
}

 

Supongo que te gusta

Origin www.cnblogs.com/lightac/p/12466973.html
Recomendado
Clasificación