2-P 왜 예제에서 0이 나올까요..?
236
投稿した質問数 13
코드가 아래와 같은데 뭐가 잘못된 걸까요..?ㅜ
#include <iostream>
#include <string>
#include <vector>
#include <queue>
int calcArea(const std::vector<std::vector<int>> &map)
{
int result = 0;
for(int i = 0; i < map.size(); ++i)
{
for (int j = 0; j < map[i].size(); ++j)
{
if (map[i][j] == 0) result += 1;
}
}
return result;
}
void bfs(std::vector<std::vector<int>> &map, std::vector<std::vector<bool>> &visited, const int yy, const int xx)
{
std::queue<std::pair<int, int>> que;
visited[yy][xx] = true;
que.push({yy, xx});
int y, x;
int moveX[4] = {0, 0, -1, 1};
int moveY[4] = {1, -1, 0, 0};
while(!que.empty())
{
y = que.front().first;
x = que.front().second;
que.pop();
for(int i = 0; i < 4; ++i)
{
int newX = x + moveX[i];
int newY = y + moveY[i];
if (newX >= 0 && newX < map[0].size() && newY >= 0 && newY < map.size() && !visited[newY][newX] && map[newY][newX] != 1)
{
que.push({newY, newX});
visited[newY][newX] = true;
map[newY][newX] = 2;
}
}
}
}
int solve(std::vector<std::vector<int>> map, const std::vector<std::pair<int, int>>& virusList)
{
std::vector<std::vector<bool>> visited(map.size());
std::vector<bool> temp(map[0].size());
temp.assign(temp.size(), false);
visited.assign(visited.size(), temp);
for(int i = 0; i < virusList.size(); ++i)
{
bfs(map, visited, virusList[i].first, virusList[i].second);
}
return calcArea(map);
}
int solution(std::vector<std::vector<int>> map, const std::vector<std::pair<int, int>>& wallList, const std::vector<std::pair<int, int>>& virusList)
{
int result = 0;
for(int i = 0; i < wallList.size(); ++i)
{
for (int j = 0; j < i; ++j)
{
for (int k = 0; k < j; ++k)
{
map[wallList[i].first][wallList[i].second] = 1;
map[wallList[j].first][wallList[j].second] = 1;
map[wallList[k].first][wallList[k].second] = 1;
result = std::max(result, solve(map, virusList));
map[wallList[i].first][wallList[i].second] = 0;
map[wallList[j].first][wallList[j].second] = 0;
map[wallList[k].first][wallList[k].second] = 0;
}
}
}
return result;
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int N, M;
std::cin >> N >> M;
std::string str;
std::vector<std::vector<int>> map;
int temp;
std::vector<std::pair<int, int>> wallList, virusList;
for (int i = 0; i < N; ++i)
{
std::vector<int> tempVec;
for(int j = 0; j < M; ++j)
{
std::cin >> temp;
if (temp == 1) wallList.push_back({i, j});
else if (temp == 2) virusList.push_back({i, j});
tempVec.push_back(temp);
}
map.push_back(tempVec);
}
int result = 0;
result = solution(map, wallList, virusList);
std::cout << result << std::endl;
return 0;
}
回答 3
1
초기화부분, 기존에 벽을 기반으로 하는게 아니라 -> 벽이 없는 곳에 있는 지역에 벽을 세움.
이 로직 등을 다듬어 봤습니다. 참고부탁드립니다.
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
int calcArea(const std::vector<std::vector<int>>& map)
{
int result = 0;
for (int i = 0; i < map.size(); ++i)
{
for (int j = 0; j < map[i].size(); ++j)
{
if (map[i][j] == 0)
result += 1;
}
}
return result;
}
void bfs(std::vector<std::vector<int>>& map, std::vector<std::vector<bool>>& visited, const int yy, const int xx)
{
std::queue<std::pair<int, int>> que;
visited[yy][xx] = true;
que.push({yy, xx});
int y, x;
int moveX[4] = {0, 0, -1, 1};
int moveY[4] = {1, -1, 0, 0};
while (!que.empty())
{
y = que.front().first;
x = que.front().second;
que.pop();
for (int i = 0; i < 4; ++i)
{
int newX = x + moveX[i];
int newY = y + moveY[i];
if (newX >= 0 && newX < map[0].size() && newY >= 0 && newY < map.size() && !visited[newY][newX] && map[newY][newX] != 1)
{
que.push({newY, newX});
visited[newY][newX] = true;
map[newY][newX] = 2;
}
}
}
}
int solve(std::vector<std::vector<int>> map, const std::vector<std::pair<int, int>>& virusList)
{
std::vector<std::vector<bool>> visited(map.size(), std::vector<bool>(map[0].size(), false));
for (int i = 0; i < virusList.size(); ++i)
{
bfs(map, visited, virusList[i].first, virusList[i].second);
}
return calcArea(map);
}
int solution(std::vector<std::vector<int>> map, const std::vector<std::pair<int, int>>& emptySpaces, const std::vector<std::pair<int, int>>& virusList)
{
int result = 0;
for (int i = 0; i < emptySpaces.size(); ++i)
{
for (int j = i + 1; j < emptySpaces.size(); ++j)
{
for (int k = j + 1; k < emptySpaces.size(); ++k)
{
map[emptySpaces[i].first][emptySpaces[i].second] = 1;
map[emptySpaces[j].first][emptySpaces[j].second] = 1;
map[emptySpaces[k].first][emptySpaces[k].second] = 1;
result = std::max(result, solve(map, virusList));
map[emptySpaces[i].first][emptySpaces[i].second] = 0;
map[emptySpaces[j].first][emptySpaces[j].second] = 0;
map[emptySpaces[k].first][emptySpaces[k].second] = 0;
}
}
}
return result;
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int N, M;
std::cin >> N >> M;
std::vector<std::vector<int>> map(N, std::vector<int>(M));
std::vector<std::pair<int, int>> emptySpaces, virusList;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
std::cin >> map[i][j];
if (map[i][j] == 0)
emptySpaces.push_back({i, j});
else if (map[i][j] == 2)
virusList.push_back({i, j});
}
}
int result = solution(map, emptySpaces, virusList);
std::cout << result << std::endl;
return 0;
}
0
안녕하세요 hello님 ㅎㅎ
이렇게 질문주시면 제가 디버깅하기가 너무 힘듭니다. (들여쓰기가 안되어있어욤 ㅠ)
0주차 - 질문하는 방법 참고해서 질문 부탁드립니다.
감사합니다.
0
아 제가 코드블럭이 따로 있는 지 몰랐습니다.. 재첨부 드려요!#include <iostream>
#include <string>
#include <vector>
#include <queue>
int calcArea(const std::vector<std::vector<int>>& map)
{
int result = 0;
for (int i = 0; i < map.size(); ++i)
{
for (int j = 0; j < map[i].size(); ++j)
{
if (map[i][j] == 0)
result += 1;
}
}
return result;
}
void bfs(std::vector<std::vector<int>>& map, std::vector<std::vector<bool>>& visited, const int yy, const int xx)
{
std::queue<std::pair<int, int>> que;
visited[yy][xx] = true;
que.push({yy, xx});
int y, x;
int moveX[4] = {0, 0, -1, 1};
int moveY[4] = {1, -1, 0, 0};
while (!que.empty())
{
y = que.front().first;
x = que.front().second;
que.pop();
for (int i = 0; i < 4; ++i)
{
int newX = x + moveX[i];
int newY = y + moveY[i];
if (newX >= 0 && newX < map[0].size() && newY >= 0 && newY < map.size() && !visited[newY][newX] && map[newY][newX] != 1)
{
que.push({newY, newX});
visited[newY][newX] = true;
map[newY][newX] = 2;
}
}
}
}
int solve(std::vector<std::vector<int>> map, const std::vector<std::pair<int, int>>& virusList)
{
std::vector<std::vector<bool>> visited(map.size());
std::vector<bool> temp(map[0].size());
temp.assign(temp.size(), false);
visited.assign(visited.size(), temp);
for (int i = 0; i < virusList.size(); ++i)
{
bfs(map, visited, virusList[i].first, virusList[i].second);
}
return calcArea(map);
}
int solution(std::vector<std::vector<int>> map, const std::vector<std::pair<int, int>>& wallList, const std::vector<std::pair<int, int>>& virusList)
{
int result = 0;
for (int i = 0; i < wallList.size(); ++i)
{
for (int j = 0; j < i; ++j)
{
for (int k = 0; k < j; ++k)
{
map[wallList[i].first][wallList[i].second] = 1;
map[wallList[j].first][wallList[j].second] = 1;
map[wallList[k].first][wallList[k].second] = 1;
result = std::max(result, solve(map, virusList));
map[wallList[i].first][wallList[i].second] = 0;
map[wallList[j].first][wallList[j].second] = 0;
map[wallList[k].first][wallList[k].second] = 0;
}
}
}
return result;
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int N, M;
std::cin >> N >> M;
std::string str;
std::vector<std::vector<int>> map;
int temp;
std::vector<std::pair<int, int>> wallList, virusList;
for (int i = 0; i < N; ++i)
{
std::vector<int> tempVec;
for (int j = 0; j < M; ++j)
{
std::cin >> temp;
if (temp == 1)
wallList.push_back({i, j});
else if (temp == 2)
virusList.push_back({i, j});
tempVec.push_back(temp);
}
map.push_back(tempVec);
}
int result = 0;
result = solution(map, wallList, virusList);
std::cout << result << std::endl;
return 0;
}
코딩살구클럽 가입부탁드립니다
0
22
2
코딩살구클럽 가입 요청 확인부탁드립니다
0
29
2
5-S 테스트 케이스 질문
0
34
2
코살 문제풀이 환경
0
46
2
2 - T 오큰수 문제가 있는 것 같습니다.
0
42
1
추천 추가문제들
0
42
2
프로그래머스 코테 환경 관련해서 질문드립니다.
0
43
2
해당 문제에 대한 채점이 코딩살구클럽에서 올바르게 처리되지 않습니다.
0
40
2
균형 이진 트리 설명 시 높이 숫자
0
30
2
4-H 질문드립니다.
0
35
2
1-K 질문드립니다.
0
41
2
대기업 인적성 시험 질문
0
41
2
4-C 질문드립니다
0
41
2
[수학숙제 / BOJ 2870] 채점 서버 오작동
0
37
1
코테 준비 질문
0
50
1
살구클럽가입 요청드려요
0
38
2
1-I 문제 질문
0
39
2
코딩살구클럽 가입
0
57
2
AI 코딩 도구 사용 시 학습 방법 조언
0
51
2
코딩살구클럽 오류
0
61
2
코살클 [3-F 괄호 추가하기] 프라이빗 9번 제보
0
46
1
코딩살구클럽 테스트 케이스 오류 제보
0
52
2
삼성 코딩테스트
0
62
2
틀린 이유를 못찾겠습니다
0
48
2

