회원가입
로그인
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
문제집
대회
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
시간초과 질문드립니다..
1520번 - 내리막 길
leeking91
6년 전
0
처음에 답은 제대로 나와서 , 돌렸는데 시간초과가 나왔습니다
그래서 visit[][] 추가했는데 이번엔 2가 나오더군요 ;
답은 : 3<<
메모제이션으로 바꿔야될것같은데 어느 부분에서 어떻게 바꾸는 것이 좋을까요 ? ..
감이 안오네요 ..
import java.util.Scanner; import java.util.LinkedList; import java.util.Queue; public class Boj1520dp { public static int n, m, cnt = 0; public static int[][] map; public static boolean[][] visit; public static int[] dx = { 0, 0, 1, -1 }; public static int[] dy = { 1, -1, 0, 0 }; public static Scanner in = new Scanner(System.in); public static void main(String args[]) { n = in.nextInt(); m = in.nextInt(); map = new int[n][m]; visit = new boolean[n][m]; System.out.println(dp()); } public static boolean isRange(int nx, int ny) { return (nx < n && ny < m && nx >= 0 && ny >= 0); } public static boolean canMove(int nx, int ny, int x, int y) { return (map[nx][ny] < map[x][y]); } public static int dp() { init(); Queue<Node> q = new LinkedList<>(); q.add(new Node(0, 0)); while (!q.isEmpty()) { Node node = q.poll(); visit[node.x][node.y] = true; for (int i = 0; i < 4; i++) { int nx = node.x + dx[i]; int ny = node.y + dy[i]; System.out.println(nx + " : " + ny); if (isRange(nx, ny) && canMove(nx, ny, node.x, node.y)&&visit[nx][ny]==false) { if (nx == n-1 && ny == m-1) { cnt++; } q.add(new Node(nx, ny)); } } } return cnt; } public static void init() { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { map[i][j] = in.nextInt(); visit[i][j] = false; } } } static class Node { int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } } }
댓글을 작성하려면
로그인
해야 합니다.
leeking91 6년 전