https://www.acmicpc.net/problem/16441
16441번: 아기돼지와 늑대
첫 번째 줄에는 격자의 행의 수를 나타내는 N (3 ≤ N ≤ 100) 과 격자의 열의 수를 나타내는 M (3 ≤ M ≤ 100) 이 주어집니다. 두 번째 줄부터 N개의 줄에 지도의 정보를 나타내는 길이가 M인 문자열
www.acmicpc.net
전형적인 그래프 구현 문제이되, 빙판 구현을 신경써야 한다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
int N, M;
char board[101][101];
vector<pii> wolf;
bool vis[101][101];
int dir[2][4] = {{-1, 1, 0, 0}, {0, 0, -1, 1}};
void DFS(int x, int y)
{
vis[x][y] = true;
for (int i{0}; i < 4; i++)
{
int nx = x, ny = y;
while (true)
{
nx += dir[0][i];
ny += dir[1][i];
if (nx >= 1 && nx <= N && ny >= 1 && ny <= M)
{
if (board[nx][ny] == '+')
continue;
else if (board[nx][ny] == '.' && !vis[nx][ny])
DFS(nx, ny);
else if (board[nx][ny] == '#' && !vis[nx - dir[0][i]][ny - dir[1][i]])
DFS(nx - dir[0][i], ny - dir[1][i]);
break;
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N >> M;
for (int i{1}; i <= N; i++)
{
for (int j{1}; j <= M; j++)
{
cin >> board[i][j];
if (board[i][j] == 'W')
{
wolf.push_back({i, j});
board[i][j] = '.';
}
}
}
for (auto [x, y] : wolf)
DFS(x, y);
for (auto [x, y] : wolf)
board[x][y] = 'W';
for (int i{1}; i <= N; i++)
{
for (int j{1}; j <= M; j++)
{
if (board[i][j] == '.' && !vis[i][j])
cout << 'P';
else
cout << board[i][j];
}
cout << "\n";
}
}
24551번: 일이 너무 많아... - 조합론(포함-배제 정리) (0) | 2023.05.27 |
---|---|
2179번: 비슷한 단어 - 자료 구조(unordered_map), 정렬 (1) | 2023.05.27 |
2805번: 나무 자르기 - 이분 탐색(매개 변수 탐색) (2) | 2023.05.26 |
18111번: 마인크래프트 - 완전 탐색 (0) | 2023.05.26 |
2108번: 통계학 - 정렬 (0) | 2023.05.26 |
댓글 영역