회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
시간 초과..
4963번 - 섬의 개수
coltonoppa
6년 전
1
어떤것 때문에 시간초과가 나오는지 궁금합니다.
50 50 사이즈로 돌려도 빠르게 나오던데 이유가 뭘까요!!?
import java.util.*; public class Main { public static class Cell { public boolean isLand; public boolean visited = false; public int i, j; public Cell(boolean isLand, int i, int j) { this.isLand = isLand; this.i = i; this.j = j; } } public static String[] width_height; public static String[][] inputMap; public static Cell[][] map; public static int width = -1; public static int height = -1; public static List<Integer> landCounts = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); int counter = 0; do { width_height = in.nextLine().split(" "); width = Integer.parseInt(width_height[0]); height = Integer.parseInt(width_height[1]); if (width == 0 && height == 0) break; inputMap = new String[height][width]; map = new Cell[height][width]; for (int i = 0; i < height; i++) inputMap[i] = in.nextLine().split(" "); drawMap(); landCounts.add(countLands()); counter++; } while (true); // Pirnt results for (int i = 0; i < counter; i++) System.out.println(landCounts.get(i)); } public static int countLands() { int result = 0; int tempi = 0; int tempj = 0; Cell currentCell; Queue<Cell> queue = new ArrayDeque<>(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (!map[i][j].visited && map[i][j].isLand) { result++; tempi = i; tempj = j; currentCell = map[tempi][tempj]; queue.offer(currentCell); while(!queue.isEmpty()) { currentCell = queue.poll(); currentCell.visited = true; checkSurrounding(queue, currentCell.i, currentCell.j); } } else map[i][j].visited = true; } } return result; } public static void checkSurrounding(Queue<Cell> queue, int i, int j) { boolean skipLeft = false; boolean skipRight = false; boolean skipBottom = false; boolean skipTop = false; if (j == 0) skipLeft = true; if (j == width-1) skipRight = true; if (i == height-1) skipBottom = true; if (i == 0) skipTop = true; if (!skipBottom) { if (map[i+1][j].isLand && !map[i+1][j].visited) { queue.add(map[i+1][j]); } } if (!skipTop) { if (map[i-1][j].isLand && !map[i-1][j].visited) { queue.add(map[i-1][j]); } } if (!skipLeft) { if (map[i][j-1].isLand && !map[i][j-1].visited) { queue.add(map[i][j-1]); } } if (!skipRight) { if (map[i][j+1].isLand && !map[i][j+1].visited) { queue.add(map[i][j+1]); } } if (!skipLeft && !skipTop) { if (map[i-1][j-1].isLand && !map[i-1][j-1].visited) { queue.add(map[i-1][j-1]); } } if (!skipLeft && !skipBottom) { if (map[i+1][j-1].isLand && !map[i+1][j-1].visited) { queue.add(map[i+1][j-1]); } } if (!skipRight && !skipBottom) { if (map[i+1][j+1].isLand && !map[i+1][j+1].visited) { queue.add(map[i+1][j+1]); } } if (!skipRight && !skipTop) { if (map[i-1][j+1].isLand && !map[i-1][j+1].visited) { queue.add(map[i-1][j+1]); } } } public static void drawMap() { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if(inputMap[i][j].equals("1")) map[i][j] = new Cell(true, i, j); else map[i][j] = new Cell(false, i, j); } } } }
댓글을 작성하려면
로그인
해야 합니다.
coltonoppa 6년 전 1
어떤것 때문에 시간초과가 나오는지 궁금합니다.
50 50 사이즈로 돌려도 빠르게 나오던데 이유가 뭘까요!!?