알고리즘 문제/AlgoSpot
쿼드 트리 뒤집기(QUADTREE)
parkit
2019. 1. 7. 14:55
728x90
반응형
https://algospot.com/judge/problem/read/QUADTREE#
엄청 복잡하게 생각했었는데,
책에서는 아주 간단한 코드로 구현했다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> using namespace std; string s; // 책 194p string Reverse(string::iterator &itr) // string::iterator itr 가 아니다. s 전체에 대한 영향을 주어야 한다. { char head = *(itr); ++itr; /* ++itr이 아래의 if문 보다 위에 있어야 한다. itr은 전역 인수 개념으로, return을 하기 전에 증가시켜야 아래의 4개에 대한 연산에 영향을 줄 수 있다. */ // 첫 글자가 b 또는 w인 경우. head를 비교하고 있다. if (head == 'b' || head == 'w') return string(1, head); string upperleft = Reverse(itr); string upperright = Reverse(itr); string lowerleft = Reverse(itr); string lowerright = Reverse(itr); return string("x") + lowerleft + lowerright + upperleft + upperright; } int main(void) { int T = 0; scanf("%d", &T); while (T--) { cin >> s; string::iterator itr = s.begin(); cout << Reverse(itr) << '\n'; /* 알고스팟에서는 아래와 같이 쓰고, 제출하면 컴파일 에러가 뜬다. cout << Reverse(s.begint()) << '\n'; */ } return 0; } | cs |
728x90
반응형