acgo题库
  • 首页
  • 题库
  • 学习
  • 天梯
  • 备赛

    竞赛

    • CSP-J/S
    • 蓝桥杯

    考级

    • GESP
    • CPA
    • 电子学会考级
  • 资讯
  • 竞赛
  • 讨论
  • 团队
  • 商城
登录
注册
题目详情提交记录(0)
  • 题解

    #include <bits/stdc++.h> using namespace std; const int N = 1005; int n, m, k,sx,sy,ex,ey; char a[N][N]; int vis[N][N]; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; struct node { int x, y; }; queue<node> q; void bfs(){ memset(vis, -1, sizeof vis); q.push({sx,sy}); vis[sx][sy] = 0; while(!q.empty()){ node cur = q.front(); q.pop(); int x = cur.x, y = cur.y; for (int i = 0; i < 4; i++) { for(int d=1; d<=k; d++) { int nx = x + dx[i]*d, ny = y + dy[i]*d; if(nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == '#' ) break; if(vis[nx][ny]!=-1){ //已走过 if(vis[nx][ny] <= vis[x][y]) break; //同方向后续不会产生更优解 }else{ //没走过,正常入队 q.push({nx,ny}); vis[nx][ny] = vis[x][y] + 1; } } } } } int main() { cin >> n >> m >> k; for (int i =1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } cin >> sx >> sy >> ex >> ey; bfs(); cout << vis[ex][ey]; return 0; }

    userId_undefined
    未知
    模拟·模拟练习生倔强青铜冒泡宗师→排序元老
    0阅读
    0回复
    0点赞
暂无数据

提交答案之后,这里将显示提交结果~

首页