강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

Park님의 프로필 이미지
Park

작성한 질문수

10주완성 C++ 코딩테스트 | 알고리즘 코딩테스트

2-Q 질문

작성

·

216

0

http://boj.kr/26099a5957134d3c8954c7971fc64fbb

질문게시판에 있는 모든 반례를 다 넣어봤는데도 16%에서 계속 실패하네요

 

접근 방법은 동 서 남 북 에서 바라봤을때 1을 만나기 전 까지 모든 0을 queue에 집어넣어 bfs를 돌리는 방법으로 했습니다.

 

반례 리스트

10 12
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 0 0 1 0 0 0 1 1 0
0 1 0 1 0 1 0 1 1 0 1 0
0 1 0 1 0 1 0 0 0 0 0 0
0 1 0 1 0 0 1 1 1 0 1 0
0 1 0 1 1 1 1 1 1 0 1 0
0 1 0 0 0 0 0 0 0 0 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0

answer: 1 50

5 5
0 0 0 0 0
0 1 1 0 0
0 1 0 1 0
0 1 1 1 0
0 0 0 0 0
answer 1,7

4 6
0 0 0 0 0 0
0 0 0 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0
answer 1,6

6 4
0 0 0 0
0 1 1 0
0 0 1 0
0 1 0 0
0 1 0 0
0 0 0 0
answer 1,5


6 6
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 1 0 0
0 0 0 0 0 0
answer 1,3

3 3
0 0 0
0 1 0
0 0 0
answer 1,1

3 3
0 0 0
0 0 0
0 0 0
answer 0,0

답변 1

0

큰돌님의 프로필 이미지
큰돌
지식공유자

안녕하세요 ㅎㅎ 주석 달았는데 참고 부탁드립니다.

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int n, m;
int arr[101][101];
bool ok[101][101];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
set<pair<int, int>> S;

void solve(int i, int j, set<pair<int, int>> &S) {
  queue<pair<int, int>> Q;
  Q.push({i, j});
  ok[i][j] = true;
  while (!Q.empty()) {
    auto cur = Q.front();
    Q.pop();
    for (int t = 0; t < 4; t++) {
      int nx = cur.first + dx[t];
      int ny = cur.second + dy[t];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || ok[nx][ny])
        continue;
      if (arr[nx][ny] == 1) {
        S.insert({nx, ny});
        continue;
      }
      ok[nx][ny] = true;
      Q.push({nx, ny});
    }
  }
}

int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(NULL);

//GOOD
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> arr[i][j];
    }
  }

  int cnt = 0;
  int last_cnt = 0;

  while (true) {
    S.clear(); 
    bool ok[101][101];
    // 이런식으로 초기화 해주는 것이 좋습니다.  
	memset(ok, sizeof(ok), 0);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        ok[i][j] = false;

// 왜 이렇게 하시죠? 이 문제는 가장자리가 모두 0이기 때문에 가장자리에서 한번만 solve를 작동하면 
// 됩니다. 
		// 서 -> 동
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (arr[i][j] == 1 || ok[i][j]) {
          break;
        }
        solve(i, j, S);
      }
    }

		// 북 -> 서
    for (int j = 0; j < m; j++) {
      for (int i = 0; i < n; i++) {
        if (arr[i][j] == 1 || ok[i][j]) {
          break;
        }
        solve(i, j, S);
      }
    }

		// 남 -> 북
    for (int i = 0; i < n; i++) {
      for (int j = m - 1; j >= 0; j--) {
        if (arr[i][j] == 1 || ok[i][j]) {
          break;
        }
        solve(i, j, S);
      }
    }

		// 동 -> 서
    for (int j = 0; j < m; j++) {
      for (int i = n - 1; i >= 0; i--) {
        if (arr[i][j] == 1 || ok[i][j]) {
          break;
        }
        solve(i, j, S);
      }
    }
    if (S.size() == 0)
      break;
    cnt++;
    last_cnt = S.size();
    for (auto a : S)
      arr[a.first][a.second] = 0;
  }
  cout << cnt << "\n";
  cout << last_cnt;
  return 0;
}
Park님의 프로필 이미지
Park
질문자

가장자리가 0이라는 조건을 못봤네요 답변 감사합니다! 문제 꼼꼼히 봐야겠네요!!

Park님의 프로필 이미지
Park
질문자

http://boj.kr/9bdace1ae4124ba696f31c202e8b1776
다시 제출하고 통과했습니다 감사합니다!

Park님의 프로필 이미지
Park

작성한 질문수

질문하기