티스토리 뷰

4963번: 섬의 개수
입력은 여러 개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스의 첫째 줄에는 지도의 너비 w와 높이 h가 주어진다. w와 h는 50보다 작거나 같은 양의 정수이다. 둘째 줄부터 h개 줄에는 지도
www.acmicpc.net
stack or queue를 통해 문제를 풀 수 있고 이번에는 stack을 통해 c++ 공부중 배운 개념을 활용해 문제를 풀어보았다.
1. stack사용
- <stack>을 include해서 사용.
- 재귀를 사용.
- stack에 담을 정보는 x와 y 좌표를 담아줘야하기에 pair사용
2. pair의 사용
-utility를 include
- stack과 함께 사용할 때 -> pair를 자료형처럼 사용해주면 됨
=> stack <pair <type,type> stack이름
3. 이차원 vector를 사용해 위치정보를 담아주며 중복체크를 할 수 있음
check 배열은 0으로 초기화해서 사용해야함(아래의 예문에서 사용법을 확인가능)
#include <iostream>
#include <stack>
#include <vector>
#include <utility>
using namespace std;
vector <vector <int> > check(50, vector<int>(50, 0));
vector <vector <int> > v(50, vector<int>(50));
stack <pair<int, int> > st;
void stk(int n, int m)
{
int x = st.top().first;
int y = st.top().second;
st.pop();
if (x-1 >= 0 && v[x-1][y]==1&&check[x-1][y] != 1)
{
check[x - 1][y] = 1;
st.push(make_pair(x - 1, y));
stk(n,m);
}
if (x+1 <n && v[x+1][y] == 1&& check[x+1][y] != 1)
{
check[x + 1][y] = 1;
st.push(make_pair(x+1, y));
stk(n,m);
}
if (y-1 >= 0 && v[x][y-1] == 1 && check[x][y-1] != 1)
{
check[x][y-1] = 1;
st.push(make_pair(x, y-1));
stk(n,m);
}
if (y +1 < m && v[x][y+1] == 1 && check[x][y+1] != 1)
{
check[x][y+1] = 1;
st.push(make_pair(x, y+1));
stk(n,m);
}//대각선
if ((x - 1 >= 0 &&y-1>=0) && v[x - 1][y-1] == 1 && check[x - 1][y-1] != 1)
{
check[x - 1][y-1] = 1;
st.push(make_pair(x - 1, y-1));
stk(n, m);
}
if ((x + 1 < n && y-1>=0) && v[x + 1][y-1] == 1 && check[x + 1][y-1] != 1)
{
check[x + 1][y-1] = 1;
st.push(make_pair(x + 1, y-1));
stk(n, m);
}
if ((y + 1 < m && x-1>=0) && v[x-1][y+1] == 1 && check[x-1][y+1] != 1)
{
check[x-1][y+1] = 1;
st.push(make_pair(x-1, y+1));
stk(n, m);
}
if ((y + 1 < m &&x+1<n) && v[x+1][y + 1] == 1 && check[x+1][y + 1] != 1)
{
check[x+1][y + 1] = 1;
st.push(make_pair(x+1, y + 1));
stk(n, m);
}
}
int main()
{
int w, h;
int c;
while (1)
{
cin >> w >> h;
swap(w, h);
if (w == 0 && h == 0)
{
break;
}
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
cin >> c;
v[i][j] = c;
}
}
int cnt = 0;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
if (v[i][j] == 1&& check[i][j]==0)
{
check[i][j] = 1;
cnt++;
st.push(make_pair(i, j));
stk(w,h);
}
}
}
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
check[i][j] = 0;
}
}
st = stack < pair <int, int> > ();
cout << cnt << '\n';
}
}
'c++ > 문제' 카테고리의 다른 글
16948 - 데스나이트 (0) | 2021.03.21 |
---|
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 시뮬레이션 c
- c++덱
- 핀테크 트렌드
- 딥러닝입문
- 백트래킹(1)
- mm1queue
- 백준 4963
- 기사작성 대외활동
- 파이썬 알아두면 유용
- DRF 회원관리
- 영화 리뷰 긍정 부정 분류
- 기본 텍스트 분류
- 13886
- 코딩월드뉴스
- 4963 섬의개수
- 11053 백준
- 백준 10866
- CREATE ASSERTION
- LAMBDA
- 백준 15650 파이썬
- 소프트웨어공학설계
- 10866 백준
- 효율적인방법찾기
- 모듈 사용법
- CSMA/CD란?
- stack 컨테이너
- 온라인프로필 만들기
- 백준 11053 파이썬
- 스택 파이썬
- 백준 숫자놀이
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함