解题思路:
注意事项:
参考代码:
#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 人评分
C语言程序设计教程(第三版)课后习题8.1 (C语言代码)浏览:722 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:559 |
永远的丰碑 (C语言代码)浏览:656 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:516 |
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:608 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:638 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:1007 |
WU-图形输出 (C++代码)浏览:802 |
WU-输入输出格式练习 (C++代码)浏览:1082 |
三角形 (C++代码)递推浏览:760 |