【ACWing】188. The cow of samurai style

Subject address:

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

Farmer John has a lot of cows, and he wants to trade one of them called The Knight. This cow has a unique superpower, jumping like a Knight on the farm (which is what we are familiar with as a horse in chess). Although this magical cow can't jump on trees and rocks, it can jump on the pasture at will. We use one for the pasture (x, y) (x,y)(x,y ) . This magical cow likes to eat grass like other cows. Give you a map with the starting position of The Knight, the location of trees, bushes, stones and other obstacles, in addition to a bale of grass. Now your task is to determine how many times the Knight needs to jump at least to eat the grass. The position of The Knight usesKKK is used to mark, the position of the obstacle is marked with *, and the position of grass is marked with H. Here is an example of a map:

             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 can follow the A, B, C, D… A, B, C, D…A,B,C,D This path uses5 5Jump to the grass 5 times (it is possible that the length of other routes is also5 55):

             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

Note: The data guarantee must have a solution.

Input format:
No. 1 11 row: two numbers, indicating the number of columnsCC of thefarmC and number of rowsRRR . Chapter2 ... R + 1 2 ... R2...R+1 line: one for each line byCCA string of C characters, which together describe the map of the ranch.

Output format:
an integer representing the minimum number of jumps.

Data range:
1 ≤ R, C ≤ 150 1≤R, C≤1501R,C150

The idea is BFS. Since the number of steps needs to be recorded, it is necessary to traverse hierarchically during BFS. code show as below:

#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;
}

Time and space complexity O (RC) O (RC)O ( R C ) .

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/114561585