#include <iostream>
#include <vector>
#include <queue>
#include <cstdlib>
#include <ctime>
using namespace std;
bool dfs(vector<vector<char>>& map, int i, int j, vector<vector<bool>>& visited);
vector<vector<char>> generateRandomMap(int n, int m);
// 暴力方法
int number1(vector<vector<char>>& map) {
int ans = 0;
int n = map.size();
int m = map[0].size();
vector<vector<bool>> visited(n, vector<bool>(m, false));
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map[0].size(); j++) {
if (dfs(map, i, j, visited)) {
ans++;
}
}
}
return ans;
}
// 暴力方法
bool dfs(vector<vector<char>>& map, int i, int j, vector<vector<bool>>& visited) {
if (i < 0 || i == map.size() || j < 0 || j == map[0].size() || visited[i][j]) {
return false;
}
visited[i][j] = true;
bool ans = false;
if (map[i][j] == 'O') {
ans = true;
}
else {
if (map[i][j] == 'U') {
ans = dfs(map, i - 1, j, visited);
}
else if (map[i][j] == 'D') {
ans = dfs(map, i + 1, j, visited);
}
else if (map[i][j] == 'L') {
ans = dfs(map, i, j - 1, visited);
}
else if (map[i][j] == 'R') {
ans = dfs(map, i, j + 1, visited);
}
else {
ans = dfs(map, i - 1, j, visited) || dfs(map, i + 1, j, visited) || dfs(map, i, j - 1, visited)
|| dfs(map, i, j + 1, visited);
}
}
visited[i][j] = false;
return ans;
}
// 正式方法
int number2(vector<vector<char>>& map) {
int n = map.size();
int m = map[0].size();
vector<vector<bool>> visited(n, vector<bool>(m, false));
vector<pair<int, int>> queue(n * m);
int l = 0;
int r = 0;
int ans = 0;
// O在哪,目的地
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == 'O') {
visited[i][j] = true;
queue[r++] = make_pair(i, j);
break;
}
}
}
// [] [] [] [] [] ...
// l ...... r
while (l < r) { // 队列里还有位置!
ans++;
pair<int, int> cur = queue[l++];
int row = cur.first;
int col = cur.second;
if (row - 1 >= 0 && !visited[row - 1][col] && (map[row - 1][col] == 'D' || map[row - 1][col] == '.')) {
visited[row - 1][col] = true;
queue[r++] = make_pair(row - 1, col);
}
if (row + 1 < n && !visited[row + 1][col] && (map[row + 1][col] == 'U' || map[row + 1][col] == '.')) {
visited[row + 1][col] = true;
queue[r++] = make_pair(row + 1, col);
}
if (col - 1 >= 0 && !visited[row][col - 1] && (map[row][col - 1] == 'R' || map[row][col - 1] == '.')) {
visited[row][col - 1] = true;
queue[r++] = make_pair(row, col - 1);
}
if (col + 1 < m && !visited[row][col + 1] && (map[row][col + 1] == 'L' || map[row][col + 1] == '.')) {
visited[row][col + 1] = true;
queue[r++] = make_pair(row, col + 1);
}
}
return ans;
}
// 生成随机地图
vector<vector<char>> generateRandomMap(int n, int m) {
vector<vector<char>> map(n, vector<char>(m));
srand(time(0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int r = rand() % 5;
if (r == 0) {
map[i][j] = 'U';
}
else if (r == 1) {
map[i][j] = 'D';
}
else if (r == 2) {
map[i][j] = 'L';
}
else if (r == 3) {
map[i][j] = 'R';
}
else {
map[i][j] = '.';
}
}
}
map[rand() % n][rand() % m] = 'O';
return map;
}
// 为了测试
int main() {
int n = 10;
int m = 10;
int testTimes = 1000;
cout << ("测试开始") << endl;
for (int i = 0; i < testTimes; i++) {
vector<vector<char>> map = generateRandomMap(n, m);
int ans1 = number1(map);
int ans2 = number2(map);
if (ans1 != ans2) {
cout << ("出错了!") << endl;
}
}
cout << ("测试结束") << endl;
return 0;
}
评论