题解
2023-08-01 20:36:45
发布于:上海
80阅读
0回复
0点赞
#include <iostream>
#include <vector>
using namespace std;
int n, m, c;
vector<vector<char>> grid;
bool dfs(int i, int j, int steps) {
if (i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == 'X') {
return false;
}
if (grid[i][j] == 'T' && steps == c) {
return true;
}
if (steps >= c) {
return false;
}
char temp = grid[i][j];
grid[i][j] = 'X';
bool found = false;
found |= dfs(i + 1, j, steps + 1);
found |= dfs(i - 1, j, steps + 1);
found |= dfs(i, j + 1, steps + 1);
found |= dfs(i, j - 1, steps + 1);
grid[i][j] = temp;
return found;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> m >> c;
grid.resize(n, vector<char>(m));
int start_i, start_j;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'S') {
start_i = i;
start_j = j;
}
}
}
if (dfs(start_i, start_j, 0)) {
cout << "YES" <<"\n";
} else cout<<"NO"<<"\n";
}
return 0;
}
全部评论 3
你的洛谷号多少啊,法姐
2023-08-14 来自 上海
0法姐最近怎么不发题解了
2023-08-09 来自 浙江
0不咋在这刷题
2023-08-09 来自 上海
0呜呜呜
2023-08-10 来自 浙江
0为啥啊?
2023-08-10 来自 浙江
0
道歉信看一下
2023-08-06 来自 浙江
0嗯嗯
2023-08-06 来自 上海
0
有帮助,赞一个