Репост из: TCS CodeVita Season 12 Solutions
Desert Queen
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
#include
using namespace std;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool isValid(int x, int y, int n, const vector& grid) {
return x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'M';
}
// @PLACEMENTLELO
int fMW(int n, const vector& grid, pair start, pair end) {
vector placementlelo(n, vector(n, INT_MAX));
queue q;
https://telegram.me/+_hn3cBQVbGliYTI9
q.push(start);
placementlelo[start.first][start.second] = 0;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
https://telegram.me/+_hn3cBQVbGliYTI9
for (int d = 0; d < 4; d++) {
int nx = x + dir[d][0];
int ny = y + dir[d][1];
if (isValid(nx, ny, n, grid)) {
int cost = (grid[x][y] == 'T' && grid[nx][ny] == 'T') ? 0 : 1;
if (placementlelo[x][y] + cost < placementlelo[nx][ny]) {
placementlelo[nx][ny] = placementlelo[x][y] + cost;
q.push({nx, ny});
}
}
}
}
https://telegram.me/+_hn3cBQVbGliYTI9
return placementlelo[end.first][end.second];
}
@PLACEMENTLELO
int main() {
int n;
cin >> n;
https://telegram.me/PLACEMENTLELO
vector grid(n, vector(n));
pair start, end;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'S') start = {i, j};
if (grid[i][j] == 'E') end = {i, j};
}
}
int result = fMW(n, grid, start, end);
cout
C++
TCS CodeVita Zone 2
https://telegram.me/+_hn3cBQVbGliYTI9
#include
using namespace std;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool isValid(int x, int y, int n, const vector& grid) {
return x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'M';
}
// @PLACEMENTLELO
int fMW(int n, const vector& grid, pair start, pair end) {
vector placementlelo(n, vector(n, INT_MAX));
queue q;
https://telegram.me/+_hn3cBQVbGliYTI9
q.push(start);
placementlelo[start.first][start.second] = 0;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
https://telegram.me/+_hn3cBQVbGliYTI9
for (int d = 0; d < 4; d++) {
int nx = x + dir[d][0];
int ny = y + dir[d][1];
if (isValid(nx, ny, n, grid)) {
int cost = (grid[x][y] == 'T' && grid[nx][ny] == 'T') ? 0 : 1;
if (placementlelo[x][y] + cost < placementlelo[nx][ny]) {
placementlelo[nx][ny] = placementlelo[x][y] + cost;
q.push({nx, ny});
}
}
}
}
https://telegram.me/+_hn3cBQVbGliYTI9
return placementlelo[end.first][end.second];
}
@PLACEMENTLELO
int main() {
int n;
cin >> n;
https://telegram.me/PLACEMENTLELO
vector grid(n, vector(n));
pair start, end;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'S') start = {i, j};
if (grid[i][j] == 'E') end = {i, j};
}
}
int result = fMW(n, grid, start, end);
cout