회원가입
로그인
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
문제집
대회
1
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
bfs 문제 질문 있습니다(1600:말이 되고픈 원숭이).. 도와주세요 ㅠㅠ
1600번 - 말이 되고픈 원숭이
yunsubaek
7년 전
0
말처럼 이동할 수 있는 횟수와 가로 세로값을 받고 시작위치에서 도착위치까지 bfs로 탐색했습니다.
질문검색에 나온 테스트케이스는 전부 통과하는데 9퍼에서 걸립니다.. 어떤 예외가 있을까요? 고수분들의 도움 부탁드립니다ㅠㅠ
#include<iostream> #include<queue> using namespace std; const int size_ = 202; const int inf_ = 987654321; int hcnt; int r, c; int result = inf_; int sx, sy; int fx, fy; int dx[12] = { 0,0, 1,-1,-2,-1,1,2, 2, 1,-1,-2 }; int dy[12] = { 1,-1,0, 0, 1, 2,2,1,-1,-2,-2,-1 }; int arr[size_][size_]; //0:monkey,1:horse int visit[size_][size_][2]; struct pos { int x, y, cnt, hc; }; int main() { //init scanf("%d%d%d", &hcnt, &c, &r); sx = 1, sy = 1; fx = r, fy = c; for (int i = 1; i <= r; ++i) for (int j = 1; j <= c; ++j) { scanf("%d", &arr[i][j]); for (int k = 0; k < 2; ++k) visit[i][j][k] = inf_; } //bfs queue<pos> q; q.push({ sx,sy,0,hcnt }); visit[sx][sy][0] = 0; while (!q.empty()) { pos now = q.front(); q.pop(); if (now.x == fx && now.y == fy) { if (result > now.cnt)result = now.cnt; } //monkey for (int i = 0; i < 4; ++i) { int nx = now.x + dx[i]; int ny = now.y + dy[i]; int ncnt = now.cnt + 1; int nhcnt = now.hc; if (nx<1 || ny<1 || nx>r || ny>c)continue; if (arr[nx][ny] == 1)continue; if (visit[nx][ny][0] <= ncnt)continue; visit[nx][ny][0] = ncnt; q.push({ nx,ny,ncnt,nhcnt }); } if (now.hc > 0) { //horse for (int i = 4; i < 12; ++i) { int nx = now.x + dx[i]; int ny = now.y + dy[i]; int ncnt = now.cnt + 1; int nhcnt = now.hc - 1; if (nx<1 || ny<1 || nx>r || ny>c)continue; if (arr[nx][ny] == 1)continue; if (visit[nx][ny][1] <= ncnt)continue; visit[nx][ny][1] = ncnt; q.push({ nx,ny,ncnt,nhcnt }); } } } //output printf("%d\n", result == inf_ ? -1 : result); return 0; }
yunsubaek
7년 전
0
visit부분에서 원숭이, 말 방문 2가지로 체크하지 않고 말처럼 이동한 횟수를 메모이제이션 해서 해결했습니다!
댓글을 작성하려면
로그인
해야 합니다.
yunsubaek 7년 전
말처럼 이동할 수 있는 횟수와 가로 세로값을 받고 시작위치에서 도착위치까지 bfs로 탐색했습니다.