아직 완벽한 결과가 안나오는데요 계속해서 수정하겠읍니다.
아래 코드는 중간 중간에 값을 체크하는 문구들은 뺀거구요.
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #include <iostream> #include <cstdio> #include <fstream> #include <stack> using namespace std; int** adjacent_arr; char buf[64]; int node_count; int* color_arr; int node = 0; stack<int> s; int search_adjacent_color(int node); void solve(int node); int main(void) { ifstream ifs("./input/graph10_yes.dat", ios::binary); if (ifs.is_open() == false) cout << "File Open Failed" << endl; ifs.getline(buf, 64); node_count = atoi(buf); adjacent_arr = new int*[node_count]; for (int i = 0; i < node_count; i++) adjacent_arr[i] = new int[node_count]; for (int i = 0; i < node_count; i++) for (int j = 0; j < node_count; j++) adjacent_arr[i][j] = -1; color_arr = new int[node_count]; for (int i = 0; i < node_count; i++) color_arr[i] = -1; while (ifs.getline(buf, 64)) { char* pch = strtok(buf, "<"); int node = atoi(pch) - 1; while (true) { pch = strtok(NULL, "> "); if (pch == nullptr) break; int adjacent_node = atoi(pch) - 1; adjacent_arr[node][adjacent_node] = 1; adjacent_arr[adjacent_node][node] = 1; } } for (int node = 0; node < node_count; node++) { for (int i = 0; i < node_count; i++) { if (adjacent_arr[node][i] == 1) { adjacent_arr[node][i] = 2; adjacent_arr[i][node] = 2; int color = search_adjacent_color(node); if (color == -1) { cout << "Failed Case" << endl; abort(); return 0; } color_arr[node] = color; for (int k = 0; k < node_count; k++) { switch (color_arr[k]) { case 1: cout << "R"; break; case 2: cout << "G"; break; case 3: cout << "B"; break; } cout << endl; } break; } } } return 0; } int search_adjacent_color(int node) { int rgb[3] = { 1,2,3 }; for (int i = 0; i < node_count; i++) { if (adjacent_arr[node][i] == 1 || adjacent_arr[node][i] == 2) { int color = color_arr[i]; if (color != -1) { rgb[color - 1] = -1; } } } for (int i = 0; i < 3; i++) { if (rgb[i] != -1) return rgb[i]; } return -1; } | cs |
그리고 이건 결과인데요.
위에 결과는 yes10을 넣어본것인데...
인접색이 정해지지 않았으면 -1이고 정해지면 rgb(1,2,3)으로 해놨는뎅 여기서 인접색이 1,2,3즉 rgb가 모두 나와버려서 fail했다고 했는데... 힝..
문제점을 찾는데로 수정하겠읍니다.
문제를 찾긴 했는데 어떻게 고쳐야할지 몰겟시영. 예를들어 A의 인접한 노드가 R,-1,-1 이라고 한다면 A가 될 수 있는건 G 또는 B인데 제가 쓴 코드는 무조건 G로만 가기 때문에 B로 가서 생기는 경우를 신경쓰지 못해서 에러가 나는듯해여
머리가 안돌아가염... 이곳은 새벽4시... 낼봐염
건투를빕니다