brucehb


私信TA

用户名:brucehb

访问量:2964

签 名:

等  级
排  名 1439
经  验 2785
参赛次数 0
文章发表 27
年  龄 0
在职情况 学生
学  校 北航
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

#include<bits/stdc++.h>
using namespace std;

const int N = 101;
bool vis[N][N] = {false};
char a[N][N];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

struct Node {
    int x, y;
    int step;
    Node(int a, int b, int s) {
        x = a;
        y = b;
        step = s;
    }
};

int main()
{
    int n;
	queue<Node> s;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a[i][j];
            if (a[i][j] == 'A') {
                s.push(Node(i, j, 0));
            }
        }
    }

    while (s.size() > 0) {
        Node front = s.front();
        s.pop();
        if (a[front.x][front.y] == 'B') {
            cout << front.step << endl;
            break;
        }
       
        for (int i = 0; i < 4; i++) {
            int tx = front.x + dx[i];
            int ty = front.y + dy[i];
            if (tx < 0 || tx >= n || ty < 0 || ty >= n || vis[tx][ty]
                || (a[front.x][front.y] == a[tx][ty])) {
                continue;
            }
            vis[tx][ty] = true;
            s.push(Node(tx, ty, front.step + 1));
        }
    }
    
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区