网址如下:
Patrol Robot - UVA 1600 - Virtual Judge (vjudge.net)
(第三方网站)
写完之后直接WA,给我干傻了
debug了挺久之后,发现是一个地方c写成r了
逆天
代码如下:
#include<cstdio>
#include<queue>
#include<cstring>
struct Node{
int r, c, k, step;
Node(int r, int c, int k, int step): r(r), c(c), k(k), step(step){}
};
const int maxn = 22;
const int dr[4]{0, 1, 0, -1};
const int dc[4]{1, 0, -1, 0};
int m, n, k, map[maxn][maxn], d[maxn][maxn][maxn];
void bfs(void)
{
int step = -1;
std::queue<Node> q; q.push(Node(1, 1, 0, 0));
while(!q.empty()){
Node u = q.front(); q.pop();
if(u.r < 1 || u.r > m || u.c < 1 || u.c > n) continue;
if(map[u.r][u.c]) u.k += 1;
else u.k = 0;
if(u.k > k || d[u.r][u.c][u.k]) continue;
d[u.r][u.c][u.k] = u.step;
if(u.r == m && u.c == n){step = d[u.r][u.c][u.k]; break;}
for(int i = 0; i < 4; i++)
q.push(Node(u.r + dr[i], u.c + dc[i], u.k, u.step + 1));
}
printf("%d\n", step);
}
int main(void)
{
//freopen("input.txt", "r", stdin);
//freopen("D:\\vscode_c\\work\\output.txt", "w", stdout);
int kase; scanf("%d", &kase);
while(kase--){
memset(d, 0, sizeof(d)); memset(map, 0, sizeof(map));
scanf("%d%d%d", &m, &n, &k);
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++)
scanf("%d", &map[i][j]);
bfs();
}
return 0;
}
里面那个注释是debug用的,等会记录以下,作为小技巧
这篇文章介绍了如何使用C++编写一个广度优先搜索算法解决UVA1600题目中的机器人巡逻路径问题,涉及图的遍历和路径计数。

被折叠的 条评论
为什么被折叠?



