백준

백준 2667 c++ 런타임 에러 (OutOfBounds)

solfa 2023. 8. 25. 01:41
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

int N;
int arr[26][26]={0};
int visit[26][26]={0};
int dy[4] = {-1, 0, 1, 0}; // 좌표 이동 값 (상, 우, 하, 좌)
int dx[4] = {0, 1, 0, -1};
vector<int> ans;

void BFS(int v, int w)
{
    int cnt = 0;
    visit[v][w] = 1;
    queue<pair<int, int>> q;
    q.push(make_pair(v, w));
    cnt++;

    while (!q.empty())
    {
        int x = q.front().first;
        int y = q.front().second;
        //cnt++;
        q.pop();

        for (int i = 0; i < N; i++)
        {
            int nx = x + dx[i];
            int ny = y + dy[i];

            if (0 <= nx && 0 <= ny && nx < N || ny < N && arr[nx][ny] == 1 && visit[nx][ny] == 0)
            {
                visit[nx][ny] = 1;
                q.push(make_pair(nx, ny));
                cnt++;
            }
        }
    }
    ans.push_back(cnt);
}

int main()
{
    cin>>N;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            scanf("%1d", &arr[i][j]);
        }
    }
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (visit[i][j] != 1 && arr[i][j] == 1)
                BFS(i, j);
        }
    }
    sort(ans.begin(), ans.end());
    
    cout<<ans.size()<<endl;
    for(int i= 0 ; i< ans.size() ; i++)
    {
        cout<<ans[i]<<endl;
    }
}

뭐가 틀렸는지 모르겠다

자고 일어나서 내일 풀어야겠다

ㅗㅗㅗ

728x90

'백준' 카테고리의 다른 글

백준 1874번 C++  (1) 2023.11.15
백준 2805번 C++  (0) 2023.11.15
백준 2644번 c++  (0) 2023.08.09
백준 2178번 C++  (0) 2023.07.18
백준 1260번 C++ 배열 사용  (0) 2023.07.06