【ACWing】188. 武士风度的牛

题目地址:

https://www.acwing.com/problem/content/190/

农民John有很多牛,他想交易其中一头被称为The Knight的牛。这头牛有一个独一无二的超能力,在农场里像Knight一样地跳(就是我们熟悉的象棋中马的走法)。虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个 ( x , y ) (x,y) (x,y)的坐标图来表示。这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了The Knight的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。现在你的任务是,确定The Knight要想吃到草,至少需要跳多少次。The Knight的位置用 K K K来标记,障碍的位置用*来标记,草的位置用H来标记。这里有一个地图的例子:

             11 | . . . . . . . . . .
             10 | . . . . * . . . . . 
              9 | . . . . . . . . . . 
              8 | . . . * . * . . . . 
              7 | . . . . . . . * . . 
              6 | . . * . . * . . . H 
              5 | * . . . . . . . . . 
              4 | . . . * . . . * . . 
              3 | . K . . . . . . . . 
              2 | . . . * . . . . . * 
              1 | . . * . . . . * . . 
              0 ----------------------
                                    1 
                0 1 2 3 4 5 6 7 8 9 0 

The Knight 可以按照下图中的 A , B , C , D … A,B,C,D… A,B,C,D这条路径用 5 5 5次跳到草的地方(有可能其它路线的长度也是 5 5 5):

             11 | . . . . . . . . . .
             10 | . . . . * . . . . .
              9 | . . . . . . . . . .
              8 | . . . * . * . . . .
              7 | . . . . . . . * . .
              6 | . . * . . * . . . F<
              5 | * . B . . . . . . .
              4 | . . . * C . . * E .
              3 | .>A . . . . D . . .
              2 | . . . * . . . . . *
              1 | . . * . . . . * . .
              0 ----------------------
                                    1
                0 1 2 3 4 5 6 7 8 9 0

注意:数据保证一定有解。

输入格式:
1 1 1行:两个数,表示农场的列数 C C C和行数 R R R。第 2... R + 1 2...R+1 2...R+1行:每行一个由 C C C个字符组成的字符串,共同描绘出牧场地图。

输出格式:
一个整数,表示跳跃的最小次数。

数据范围:
1 ≤ R , C ≤ 150 1≤R,C≤150 1R,C150

思路是BFS。由于要记录步数,所以在BFS的时候要分层遍历。代码如下:

#include <iostream>
#include <queue>

#define x first
#define y second
using namespace std;

typedef pair<int, int> PII;

const int N = 160;
int C, R;
char a[N][N];
queue<PII> q;
bool st[N][N];

int bfs(PII start, PII end) {
    int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
    int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
    q.push(start);
	
	// 存步数
    int res = 0;
    while (!q.empty()) {
        res++;
        // 分层遍历需要先存一下队列的size
        int size = q.size();
        for (int i = 0; i < size; i++) {
            auto t = q.front();
            q.pop();
            for (int j = 0; j < 8; j++) {
                int nx = t.x + dx[j], ny = t.y + dy[j];
                if (0 <= nx && nx < R && 0 <= ny && ny < C && !st[nx][ny] && a[nx][ny] != '*') {
                	// 走到终点了,直接返回步数
                    if (nx == end.x && ny == end.y) return res;

                    st[nx][ny] = true;
                    q.push({nx, ny});
                }
            }
        }
    }
    
    // 表示走不到。题目保证有解,所以这句不会执行
    return -1;
}

int main() {
    cin >> C >> R;
    PII start, end;
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++) {
            cin >> a[i][j];
            if (a[i][j] == 'K') start = {i, j};
            else if (a[i][j] == 'H') end = {i, j};
        }

    cout << bfs(start, end) << endl;

    return 0;
}

时空复杂度 O ( R C ) O(RC) O(RC)

也可以双向BFS来做。代码如下:

#include <iostream>
#include <queue>
using namespace std;

using PII = pair<int, int>;

const int N = 160;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};
int dy[] = {2, 1, -1, -2, 2, 1, -1, -2};
int m, n;
char g[N][N];
bool vis[2][N][N];
int sx, sy, ex, ey;

bool one_step(queue<PII> &q, int beg) {
    for (int i = q.size(); i; i--) {
        auto t = q.front(); q.pop();
        int x = t.first, y = t.second;
        for (int j = 0; j < 8; j++) {
            int nx = x + dx[j], ny = y + dy[j];
            if (1 <= nx && nx <= m && 1 <= ny && ny <= n && !vis[beg][nx][ny] && g[nx][ny] != '*') {
                if (vis[beg ^ 1][nx][ny]) return true;
                vis[beg][nx][ny] = true;
                q.push({nx, ny});
            }
        }
    }
    
    return false;
}

int bfs() {
    queue<PII> qs, qe;
    qs.push({sx, sy}); 
    qe.push({ex, ey});
    vis[0][sx][sy] = vis[1][ex][ey] = true;
    
    int res = 0;
    while (qs.size() && qe.size()) {
        res++;
        if (qs.size() <= qe.size()) {
            if (one_step(qs, 0)) return res;
        } else {
            if (one_step(qe, 1)) return res;
        }
    }
    
    return -1;
}

int main() {
    scanf("%d%d", &n, &m);
    
    for (int i = 1; i <= m; i++) {
        scanf("%s", g[i] + 1);
        for (int j = 1; j <= n; j++)
            if (g[i][j] == 'K') sx = i, sy = j;
            else if (g[i][j] == 'H') ex = i, ey = j;
    }
        
    printf("%d\n", bfs());
    
    return 0;
}

时空复杂度一样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
武士数独问题是一个特殊的9x9数独问题。可以将其视为按照左上、右上、中间、左下、右下的顺序,在有解的前提下,能够求解出一个有效解。在Python中,可以使用回溯算法来解决这个问题。回溯算法通过尝试每个可能的数字,并逐步填充数独格子,直到找到一个有效解为止。 以下是一个可能的Python解决方案: ```python def solve_samurai_sudoku(board): if is_complete(board): return board row, col = find_empty_cell(board) for num in range(1, 10): if is_valid(board, row, col, num): board[row][col] = num if solve_samurai_sudoku(board): return board board[row][col] = 0 return None def is_complete(board): for row in range(9): for col in range(9): if board[row][col] == 0: return False return True def find_empty_cell(board): for row in range(9): for col in range(9): if board[row][col] == 0: return row, col return None def is_valid(board, row, col, num): for i in range(9): if board[row][i] == num or board[i][col] == num: return False start_row = 3 * (row // 3) start_col = 3 * (col // 3) for i in range(start_row, start_row + 3): for j in range(start_col, start_col + 3): if board[i][j] == num: return False return True # 以下是一个示例数独问题的输入和调用示例 sudoku_board = [ [0, 0, 0, 2, 5, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 7, 0, 0], [0, 0, 0, 0, 0, 0, 0, 5, 4], [0, 0, 0, 8, 0, 0, 0, 3, 0], [0, 0, 0, 0, 0, 0, 5, 0, 0], [0, 0, 2, 0, 0, 6, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0] ] solution = solve_samurai_sudoku(sudoku_board) if solution: for row in solution: print(row) else: print("No solution found.") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值