-
[코테 준비] reminders공부/etc 2023. 10. 7. 14:59
cmp 함수 만들때는, 문제에 있는 모든 조건을 활용하여 만들기!!!!
특히 대소 비교할때 주의. 모든 경우를 고려할 것
// 구조체 형식 struct square { int r, c, len; // 위 꼭짓점(r,c), 한 변 길이: len }; // cmp 함수 bool cmp(square& candi1, square& candi2) { if (candi1.len < candi2.len) return true; else if (candi1.len == candi2.len) { if (candi1.r < candi2.r) return true; else if (candi1.r == candi2.r) { if (candi1.c < candi2.c) return true; else return false; } else return false; } else return false; } // cmp 함수 호출하는 부분 vector <square> candi; // 정사각형 후보 sort(candi.begin(), candi.end(), cmp);
주의할 것:
문제 조건에서 "벽"으로만 이동할 수 있다고 할 때, 사실 "벽"과 "출구"로도 이동가능하다는 사실을 명심할 것!!!!
BFS로 시작에서 도착까지 최단경로로 이동하며 이동 경로도 아는 방법
😲 역추적 테크닉!!
코드트리 [포탑 부수기] 참고
pair<int, int> come[20][20] = {}; // 역추적 배열. come[x][y] = {a,b} : (x,y)는 (a,b)에서 온 것! while (!q.empty()) { int cur_r = q.front().first; int cur_c = q.front().second; q.pop(); for (int k = 0; k < 4; k++) { // 가장자리 확인 -> 반대편으로 나옴 int nr = (cur_r + dr_four[k] + N) % N; int nc = (cur_c + dc_four[k] + M) % M; if (board[nr][nc] == 0 || visited[nr][nc]) continue; come[nr][nc] = { cur_r, cur_c }; // 어디에서 왔는지 기록하기 visited[nr][nc] = 1; q.push({ nr,nc }); printf(">> q.push(%d, %d)\n", nr, nc); } }
문제 조건에 직접적으로 안써있는 경우도 고려하기.
ex) 공격 조건 - 공격 반경에 공격자가 있는 경우, 공격자는 공격당하지 않아야함
사실 당연한거 ㅋㅋㅋㅋㅋ
크기가 선언되지 않은 vector에 push_back을 하지 않으면 값이 없는 상태임.
값이 없는 상태에서 v[0] = 10; 과 같이 인덱스에 직접 값을 할당할 수 없음
'공부 > etc' 카테고리의 다른 글
[Spring Boot] Controller vs RestController (0) 2024.03.02 Adafruit ESP32 Feather V2 CH2102F 사용해보기 (1) - 환경설정부터 예제 코드 실행까지 (0) 2022.06.22 [git] 로컬 저장소에서 변경한 내역을 원격 저장소에 반영하기 (0) 2022.01.10 [라즈베리파이] USB 마이크를 이용해서 음성데이터를 녹음하기 (0) 2021.11.08 [git] warning: LF will be replaced by CRLF (0) 2021.11.06