8.1深度优先搜索(DFS)
#include <cstdio>const int MAXN = 5;
int n, m, maze[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
int counter = 0;const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool isValid(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0 && !visited[x][y];
}void DFS(int x, int y) {if (x == n - 1 && y == m - 1) {counter++;return;}visited[x][y] = true;for (int i = 0; i < MAXD; i++) {int nextX = x + dx[i];int nextY = y + dy[i];if (isValid(nextX, nextY)) {DFS(nextX, nextY);}}visited[x][y] = false;
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}DFS(0, 0);printf("%d", counter);return 0;
}
#include <cstdio>const int MAXN = 5;
int n, m, k, maze[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
bool canReach = false;const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool isValid(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0 && !visited[x][y];
}void DFS(int x, int y, int step) {if (canReach) {return;}if (x == n - 1 && y == m - 1) {if (step == k) {canReach = true;}return;}visited[x][y] = true;for (int i = 0; i < MAXD; i++) {int nextX = x + dx[i];int nextY = y + dy[i];if (step < k && isValid(nextX, nextY)) {DFS(nextX, nextY, step + 1);}}visited[x][y] = false;
}int main() {scanf("%d%d%d", &n, &m, &k);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}DFS(0, 0, 0);printf(canReach ? "Yes" : "No");return 0;
}
#include <cstdio>const int MAXN = 5;
const int INF = 0x3f3f3f3f;
int n, m, maze[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
int maxValue = -INF;const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool isValid(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && !visited[x][y];
}void DFS(int x, int y, int nowValue) {if (x == n - 1 && y == m - 1) {if (nowValue > maxValue) {maxValue = nowValue;}return;}visited[x][y] = true;for (int i = 0; i < MAXD; i++) {int nextX = x + dx[i];int nextY = y + dy[i];if (isValid(nextX, nextY)) {int nextValue = nowValue + maze[nextX][nextY];DFS(nextX, nextY, nextValue);}}visited[x][y] = false;
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}DFS(0, 0, maze[0][0]);printf("%d", maxValue);return 0;
}
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;typedef pair<int, int> Position;const int MAXN = 5;
const int INF = 0x3f;
int n, m, maze[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
int maxValue = -INF;
vector<Position> tempPath, optPath;const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool isValid(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && !visited[x][y];
}void DFS(int x, int y, int nowValue) {if (x == n - 1 && y == m - 1) {if (nowValue > maxValue) {maxValue = nowValue;optPath = tempPath;}return;}visited[x][y] = true;for (int i = 0; i < MAXD; i++) {int nextX = x + dx[i];int nextY = y + dy[i];if (isValid(nextX, nextY)) {int nextValue = nowValue + maze[nextX][nextY];tempPath.push_back(Position(nextX, nextY));DFS(nextX, nextY, nextValue);tempPath.pop_back();}}visited[x][y] = false;
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}tempPath.push_back(Position(0, 0));DFS(0, 0, maze[0][0]);for (int i = 0; i < optPath.size(); i++) {printf("%d %d\n", optPath[i].first + 1, optPath[i].second + 1);}return 0;
}
#include <cstdio>const int MAXN = 5;
const int INF = 0x3f;
int n, m, maze[MAXN][MAXN], isWall[MAXN][MAXN];
bool visited[MAXN][MAXN] = {false};
int maxValue = -INF;const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool isValid(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && !isWall[x][y] && !visited[x][y];
}void DFS(int x, int y, int nowValue) {if (x == n - 1 && y == m - 1) {if (nowValue > maxValue) {maxValue = nowValue;}return;}visited[x][y] = true;for (int i = 0; i < MAXD; i++) {int nextX = x + dx[i];int nextY = y + dy[i];if (isValid(nextX, nextY)) {int nextValue = nowValue + maze[nextX][nextY];DFS(nextX, nextY, nextValue);}}visited[x][y] = false;
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &isWall[i][j]);}}for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}DFS(0, 0, maze[0][0]);printf("%d", maxValue);return 0;
}
8.2广度优先搜索(BFS)
#include <cstdio>
#include <queue>
using namespace std;const int MAXN = 100000;
bool inQueue[MAXN + 1] = {false};int getStep(int n) {int step = 0;queue<int> q;q.push(1);while (true) {int cnt = q.size();for (int i = 0; i < cnt; i++) {int front = q.front();q.pop();if (front == n) {return step;}inQueue[front] = true;if (front * 2 <= n && !inQueue[front * 2]) {q.push(front * 2);}if (front + 1 <= n && !inQueue[front + 1]) {q.push(front + 1);}}step++;}
}int main() {int n, step = 0;scanf("%d", &n);printf("%d", getStep(n));return 0;
}
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;typedef pair<int, int> Position;const int MAXN = 100;
int n, m, matrix[MAXN][MAXN];
bool inQueue[MAXN][MAXN] = {false};const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool canVisit(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && matrix[x][y] == 1 && !inQueue[x][y];
}void BFS(int x, int y) {queue<Position> q;q.push(Position(x, y));inQueue[x][y] = true;while (!q.empty()) {Position front = q.front();q.pop();for (int i = 0; i < MAXD; i++) {int nextX = front.first + dx[i];int nextY = front.second + dy[i];if (canVisit(nextX, nextY)) {inQueue[nextX][nextY] = true;q.push(Position(nextX, nextY));}}}
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &matrix[i][j]);}}int counter = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (matrix[i][j] == 1 && !inQueue[i][j]) {BFS(i, j);counter++;}}}printf("%d", counter);return 0;
}
#include <cstdio>
#include <queue>
#include <utility>
using namespace std;typedef pair<int, int> Position;const int MAXN = 100;
int n, m, maze[MAXN][MAXN];
bool inQueue[MAXN][MAXN] = {false};const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool canVisit(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0 && !inQueue[x][y];
}int BFS(int x, int y) {queue<Position> q;q.push(Position(x, y));inQueue[x][y] = true;int step = 0;while (!q.empty()) {int cnt = q.size();while (cnt--) {Position front = q.front();q.pop();if (front.first == n - 1 && front.second == m - 1) {return step;}for (int i = 0; i < MAXD; i++) {int nextX = front.first + dx[i];int nextY = front.second + dy[i];if (canVisit(nextX, nextY)) {inQueue[nextX][nextY] = true;q.push(Position(nextX, nextY));}}}step++;}return -1;
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}int step = BFS(0, 0);printf("%d", step);return 0;
}
#include <cstdio>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;typedef pair<int, int> Position;const int MAXN = 100;
int n, m, maze[MAXN][MAXN];
bool inQueue[MAXN][MAXN] = {false};
Position pre[MAXN][MAXN];const int MAXD = 4;
int dx[MAXD] = {0, 0, 1, -1};
int dy[MAXD] = {1, -1, 0, 0};bool canVisit(int x, int y) {return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] == 0 && !inQueue[x][y];
}void BFS(int x, int y) {queue<Position> q;q.push(Position(x, y));inQueue[x][y] = true;while (!q.empty()) {Position front = q.front();q.pop();if (front.first == n - 1 && front.second == m - 1) {return;}for (int i = 0; i < MAXD; i++) {int nextX = front.first + dx[i];int nextY = front.second + dy[i];if (canVisit(nextX, nextY)) {pre[nextX][nextY] = Position(front.first, front.second);inQueue[nextX][nextY] = true;q.push(Position(nextX, nextY));}}}
}void printPath(Position p) {Position prePosition = pre[p.first][p.second];if (prePosition == Position(-1, -1)) {printf("%d %d\n", p.first + 1, p.second + 1);return;}printPath(prePosition);printf("%d %d\n", p.first + 1, p.second + 1);
}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &maze[i][j]);}}fill(pre[0], pre[0] + n * m, Position(-1, -1));BFS(0, 0);printPath(Position(n - 1, m - 1));return 0;
}