회원가입
로그인
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
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
백준 4963번 섬의 개수
4963번 - 섬의 개수
hermosa65
3년 전
0
자꾸만 런타임 에러가 납니다
도와주세요 ㅜㅜ
import java.io.*; import java.util.*; public class Main{ static int w, h; static int arr[][]; static boolean visit[][]; static int count=0; static int dx[] = {0, 0, -1 ,1, -1, 1, -1, 1}; static int dy[] = {-1, 1, 0, 0, 1, 1, -1, -1}; public static void main(String[] args)throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; st=new StringTokenizer(br.readLine()); w =Integer.parseInt(st.nextToken()); h =Integer.parseInt(st.nextToken()); while(true){ if(w==0 && h==0) break; arr= new int[w][h]; visit= new boolean[w][h]; for(int i =0; i<w; i++){ st = new StringTokenizer(br.readLine()); for(int j=0; j<h; j++){ arr[i][j]= Integer.parseInt(st.nextToken()); } } for(int i =1; i<w; i++){ for(int j=0; j<h; j++){ if(visit[i][j]==false && arr[i][j]==1){ bfs(i,j); count ++; } } } } System.out.println(count+'\n'); } public static void bfs(int x, int y){ Queue<node> q = new LinkedList<>(); q.add(new node(x,y)); visit[x][y]=true; while(!q.isEmpty()){ node p = q.poll(); for(int i=0;i<8;i++){ int nx = p.x + dx[i]; int ny = p.y + dy[i]; if(nx >=0 && ny >=0 && nx <w && ny < h){ if(!visit[nx][ny] && arr[nx][ny] == 1){ visit[nx][ny] = true; q.add(new node(nx,ny)); } } } } } } class node{ int x, y; public node(int x, int y){ this.x=x; this.y=y; } }
댓글을 작성하려면
로그인
해야 합니다.
hermosa65 3년 전
자꾸만 런타임 에러가 납니다
도와주세요 ㅜㅜ