회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
어떤 반례가 있는지 모르겠습니다.
2931번 - 가스관
ghjo92
4년 전
0
문제예제랑 질문들에 있는것들 봤는데 뭐가 틀렸는지 잘 모르겠네요 ㅠㅠ..
반례들어주시면 감사하겠습니다.
#include <iostream> using namespace std; int R, C; char board[26][26]; int startr, startc; int endr, endc; int dr[] = { -1,0,1,0 }; int dc[] = { 0,1,0,-1 }; struct pos { int r; int c; int d; //0↑ 1→ 2↓ 3← }; // 파이프 + | - => 0,5,6 bool pipe[7][4] = { {true,true,true,true}, {false, true, true, false}, {true, true, false, false}, {true, false, false, true}, {false, false, true, true}, {true, false, true, false}, {false, true, false, true} }; char findpipe(bool pipeDir[]) { char c = '?'; for (int i = 0; i < 7; i++) { for (int d = 0; d < 4; d++) { if (pipe[i][d] != pipeDir[d]) break; if (d == 3) c = i; } } if (c == 0) c = '+'; else if (c == 5) c = '|'; else if (c == 6) c = '-'; else if (c != '?') c = c + '0'; return c; } void findDir(int r, int c, bool pipeDir[]) //해당 칸의 파이프를 알아내기위해, 4방향의 블록들 검사하기 { for (int d = 0; d < 4; d++) { int x = r + dr[d]; int y = c + dc[d]; char p = board[x][y]; switch (d) { case 0: if (p == '|' || p == '+' || p == '1' || p == '4') pipeDir[d] = true; break; case 1: if (p == '-' || p == '+' || p == '3' || p == '4') pipeDir[d] = true; break; case 2: if (p == '|' || p == '+' || p == '2' || p == '3') pipeDir[d] = true; break; case 3: if (p == '-' || p == '+' || p == '1' || p == '2') pipeDir[d] = true; break; default: break; } } } int findStartDir(pos nextp) //첫방향 넣기 { for (int d =0; d<4; d++)//M에서 가는 경우 4가지 { bool startPipeDir[4] = {false, }; startPipeDir[(d + 2) % 4] = true; int nr = nextp.r + dr[d]; int nc = nextp.c + dc[d]; if (board[nr][nc] == 0) continue; //M바로옆에 빈칸이 아닌 갈곳이 있는 경우 if (board[nr][nc] != '.' && board[nr][nc] != 'Z') { char tmp = board[nr][nc]; int pi = board[nr][nc] - '0'; if (tmp == '+') pi = 0; else if (tmp == '|') pi = 5; else if (tmp == '-') pi = 6; if( pipe[pi][(d + 2) % 4] == true) return d; } else { //M바로옆의 빈칸으로 가는경우 findDir(nr, nc, startPipeDir); char mpipe = findpipe(startPipeDir); if (mpipe != '?') //출발지점이 있는경우 return d; } } return -1; } int moveGas(int r, int c, int d) { char x = board[r][c]; if (x == '1') { if (d == 3) d = 2; else if (d == 0) d = 1; } else if (x == '2') { if (d == 2) d = 1; else if (d == 3) d = 0; } else if (x == '3') { if (d == 2) d = 3; else if (d == 1) d = 0; } else if (x == '4') { if (d == 1) d = 2; else if (d == 0) d = 3; } // - | 블록은 방향 그대로 +는 따로처리 return d; } int main() { cin >> R >> C; for (int i=1; i<=R; i++) { for (int j = 1; j <= C; j++) { cin >> board[i][j]; if (board[i][j] == 'M') { startr = i; startc = j; } else if (board[i][j] == 'Z') { endr = i; endc = j; } } } pos nextp; nextp.r = startr; nextp.c = startc; nextp.d = findStartDir(nextp); //길따라가기 출발 while (board[nextp.r][nextp.c] != '.') { int nowr = nextp.r + dr[nextp.d]; int nowc = nextp.c + dc[nextp.d]; int nowd = moveGas(nowr,nowc,nextp.d); nextp.r = nowr; nextp.c = nowc; nextp.d = nowd; } int ansr = nextp.r; int ansc = nextp.c; int ansd = nextp.d; cout << ansr << " " << ansc << " "; bool pipeDir[4] = { false, }; pipeDir[(ansd + 2) % 4] = true;//M에서 갈 경우. findDir(ansr, ansc, pipeDir); char ans = findpipe(pipeDir); //Z로 갈 경우 if (ans == '?') { bool zpipe[4] = { false, }; for (int d=0; d<4; d++) { int nowr = ansr + dr[d]; int nowc = ansc + dc[d]; if (board[nowr][nowc] == 'Z') zpipe[d] = true; } findDir(ansr, ansc, zpipe); ans = findpipe(zpipe); } cout << ans; return 0; }
댓글을 작성하려면
로그인
해야 합니다.
ghjo92 4년 전
문제예제랑 질문들에 있는것들 봤는데 뭐가 틀렸는지 잘 모르겠네요 ㅠㅠ..
반례들어주시면 감사하겠습니다.