회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
BFS로 구현중인데 ㅠㅠ
1012번 - 유기농 배추
ktj7916
4년 전
0
이런형식으로 DFS로 구현해서맞긴했는데 BFS는 왜틀리는지 모르겠습니다 ㅠ
import java.util.*; public class OrganicVegetable { public static Scanner sc = new Scanner(System.in); public static int[] dp_x={-1,0,1,0}; public static int[] dp_y= {0,1,0,-1}; public static void main(String[] args) { int t_case=sc.nextInt( ),i=0,j=0,k=0,count=0,x=0,y=0; for(i=0;i<t_case;i++) { int N=sc.nextInt( ),M=sc.nextInt(),vegetable=sc.nextInt( ); int[][] arr =new int[N][M]; for(j=0;j<vegetable;j++) { x=sc.nextInt( ); y=sc.nextInt( ); arr[x][y]=1; } for(j=0;j<N;j++) { for(k=0;k<M;k++) { if(arr[j][k] == 1) { BFS(arr,j,k); count++; } } } System.out.println(count); count=0; } } public static void DFS(int[][] arr,int x,int y) { int next_x=0,next_y=0,i=0; for(i=0;i<4;i++) { next_x=x+dp_x[i]; next_y=y+dp_y[i]; if((next_x < 0) ||(next_x>arr.length-1) ||(next_y<0) ||(next_y>arr[0].length-1)) continue; else { if(arr[next_x][next_y] == 1) { arr[next_x][next_y]=0; DFS(arr,next_x,next_y); } } } } public static void BFS(int[][] arr, int x,int y) { int next_x=0,next_y=0,i=0; Queue q_x = new LinkedList( ); Queue q_y = new LinkedList( ); q_x.add(x); q_x.add(y); arr[x][y]=0; while(q_x.isEmpty() ==false && q_y.isEmpty() == false) { int ret_x = (int)q_x.poll( ); int ret_y = (int)q_y.poll( ); for(i=0;i<4;i++) { next_x = ret_x+dp_x[i]; next_y = ret_y+dp_y[i]; if((next_x < 0) ||(next_x>arr.length-1) ||(next_y<0) ||(next_y>arr[0].length-1)) continue; else { if(arr[next_x][next_y] == 1) { q_x.add(next_x); q_y.add(next_y); arr[next_x][next_y]=0; } } } } } }
댓글을 작성하려면
로그인
해야 합니다.
ktj7916 4년 전
이런형식으로 DFS로 구현해서맞긴했는데 BFS는 왜틀리는지 모르겠습니다 ㅠ