标签:info std str ++i alt out memset ima temp
1.

Sample Input 6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0 Sample Output 45 59 6 13
求解代码
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
bool vis[100][100];
int ans = 0;
char a[100][100];
void DFS(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];
if(temp1 >= 0 && temp2 >= 0 && a[temp1][temp2] == ‘.‘ && !vis[temp1][temp2]) {
ans++;
a[temp1][temp2] = ‘!‘;
vis[temp1][temp2] = 1;
DFS(temp1, temp2);
}
}
}
int main () {
int m, n;
while(cin >> m >> n) {
memset(a, 0, sizeof a);
memset(vis, 0, sizeof vis);
ans = 0;
if(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) {
if(a[i][j] == ‘@‘) {
x = i;
y = j;
}
}
}
//cout << x << y << endl;
DFS(x, y);
cout << ans + 1 << endl;
}
}
2.

Sample Input 3 4 5 3 2 2 2 3 1 2 3 1 1 Sample Output 4
//POJ 3620
#include <iostream>
using namespace std;
bool vis[110][110];
char a[110][110];
int ans;
int n, m, k;
int temp;
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];
if(temp1 >= 1 && temp2 >= 1 && temp1 <= m && temp2 <= n && !vis[temp1][temp2] && a[temp1][temp2] == ‘#‘) {
DFS(temp1, temp2);
}
}
return temp;
}
int main () {
//memset(a, ‘0‘, sizeof a);
cin >> m >> n >> k;
ans = 0;
while(k--) {
int x, y;
cin >> x >> y;
a[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] && a[i][j] == ‘#‘) {
temp = 0;
DFS(i, j);
if(temp > ans) {
ans = temp;
}
}
}
}
cout << ans << endl;
}
3.

//洛谷1605
#include <bits/stdc++.h>
using namespace std;
bool vis[10][10];
char a[10][10];
int ans;
int sx, sy, ex, ey;
int n, m, t;
void DFS(int x, int y) {
int dir[10] = {0, 1, 0, -1, 0, 0, 1, 0, -1};
if(x == ex && y == ey) {
ans++;
return;
}
for(int i = 1; i <= 8; i += 2) {
vis[x][y] = 1; //标记之前的点
int temp1 = x + dir[i];
int temp2 = y + dir[i + 1];
if(temp1 >= 1 && temp2 >= 1 && temp1 <= n && temp2 <= m && !vis[temp1][temp2] && a[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;
a[x][y] = ‘#‘;
}
DFS(sx, sy);
cout << ans << endl;
}
标签:info std str ++i alt out memset ima temp
原文地址:https://www.cnblogs.com/lightac/p/12466973.html