티스토리 뷰
섬나라 아일랜드
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <queue>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int map[30][30];
struct Loc {
int x;
int y;
Loc(int a, int b){
x = a;
y = b;
}
};
queue<Loc> Q;
int main() {
int n, k, cnt=0;
//freopen("input.txt", "rt", stdin);
scanf("%d", &n);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
scanf("%d", &map[i][j]);
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(map[i][j]==1){
Q.push(Loc(i, j));
map[i][j]=0;
while(!Q.empty()){
Loc loc =Q.front();
Q.pop();
for(k=0; k<8; k++){
int xx = loc.x + dx[k];
int yy = loc.y + dy[k];
if(map[xx][yy]==1){
Q.push(Loc(xx, yy));
map[xx][yy]=0;
}
}
}
cnt++;
}
}
}
printf("%d\n", cnt);
return 0;
}
미로의 최단거리통로
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <queue>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int map[30][30], min_val=2147000000;
struct Loc {
int x;
int y;
Loc(int a, int b){
x = a;
y = b;
}
};
queue<Loc> Q;
int main() {
int n, k, cnt=0, res;
freopen("input.txt", "rt", stdin);
for(int i=1; i<=7; i++){
for(int j=1; j<=7; j++){
scanf("%d", &map[i][j]);
}
}
Q.push(Loc(1, 1));
map[1][1]=0;
cnt=0;
while(!Q.empty()){
Loc loc =Q.front();
Q.pop();
for(k=0; k<4; k++){
int xx = loc.x + dx[k];
int yy = loc.y + dy[k];
if(map[xx][yy]==0 && xx<=7 && xx>=1 && yy<=7 && yy>=1){
//printf("xx: %d, yy: %d\n", xx, yy);
Q.push(Loc(xx, yy));
map[xx][yy]=map[loc.x][loc.y]+1;
}
}
}
printf("%d\n", map[7][7]);
return 0;
}
'lecture > algorithm - c++' 카테고리의 다른 글
[c++] 라이언킹 심바(BFS) (0) | 2021.01.30 |
---|---|
[c++] 토마토(BFS) (0) | 2021.01.28 |
[c++] 수식만들기(DFS) (0) | 2021.01.24 |
[c++] 피자배달거리(DFS활용) (0) | 2021.01.24 |
[c++] 복면산 SEND+MORE=MONEY (0) | 2021.01.24 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- client side rendering
- 병행프로세스
- 동적프로그래밍
- 구조체
- 인접리스트
- 스텍
- 배열
- 운영체제
- javascript
- dfs
- 세마포어
- 재귀함수
- react
- Stack
- 퀵정렬
- stackframe
- Java
- 입출력장치
- 자료구조
- 소프트웨어
- 이진탐색
- 최단경로
- 인접행렬
- BFS
- 알고리즘
- 교착상태
- 클래스
- server side rendering
- C++
- C
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함