#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int m, n;
vector<vector<bool>> visited;
vector<vector<char>> square;
bool legal(int i, int j, char a, char b) {
if (visited[i][j]) return false;
if (i < 0 || i >= m || j < 0 || j >= n) return false;
if ((a == 'r' && b == 'd') || (a == 'e' && b == 'r') || (a == 'd' && b == 'e')) return false;
return true;
}
int main() {
cin>>m,cin>>n;
visited.resize(m,vector<bool>(n));
square.resize(m, vector<char>(n));
vector<int> dx = { 0,0,-1,1 };
vector<int> dy = { -1,1,0,0 };
queue<pair<int, int>> q;
q.push({ 0,0 });
visited[0][0] = true;
int res = 0;
while (!q.empty()) {
int N = q.size();
res++;
for (int k = 0; k < N; k++) {
pair<int,int> pa = q.front();
int i = pa.first,j = pa.second;
q.pop();
if (i == m - 1 && j == n - 1) {
cout << res - 1;
system("pause");
return 0;
}
for (int p = 0; p < 4; p++) {
int newi = i + dy[p], newj = j + dx[p];
if (legal(newi, newj, square[i][j], square[newi][newj])) {
q.push(make_pair(newi, newj));
visited[newi][newj] = 1;
}
}
}
}
cout << -1;
system("pause");
return 0;
}
请大佬们指点一二。
#百度笔试#