Run ID 作者 问题 语言 测评结果 时间 内存 代码长度 提交时间
80071 sh25_wangtaojie 蓝桥杯赛迷宫 C++ 解答错误 0 MS 264 KB 1585 2026-01-04 14:58:21

Tests(2/3):


#include <iostream> #include <vector> using namespace std; const int MAXN = 100; char grid[MAXN][MAXN]; int n, m; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; bool visited[MAXN][MAXN]; bool dfs(int x, int y, int step) { if (step == 4) return true; visited[x][y] = true; for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny]) { if (step == 0 && grid[nx][ny] == 'Q') { if (dfs(nx, ny, step + 1)) return true; } else if (step == 1 && grid[nx][ny] == 'B') { if (dfs(nx, ny, step + 1)) return true; } else if (step == 2 && grid[nx][ny] == 'S') { if (dfs(nx, ny, step + 1)) return true; } else if (step == 3 && grid[nx][ny] == 'L') { if (dfs(nx, ny, step + 1)) return true; } } } visited[x][y] = false; return false; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> grid[i]; } int max_cycles = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == 'L') { fill(&visited[0][0], &visited[0][0] + MAXN * MAXN, false); if (dfs(i, j, 0)) { max_cycles++; } } } } if (max_cycles == 0) cout << 0 << endl; else if (max_cycles == 1) cout << 1 << endl; else cout << -1 << endl; return 0; }


测评信息: