Programming Problems/Graph

배열 스왑해서 정렬하기 (BFS 응용)

fw93 2018. 4. 17. 17:59
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
125
126
127
#include "stdafx.h"
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <map>
 
using namespace std;
 
void makeBFS(int n,map<vector<int>,int> & data) {
    // make buffere queue and starting point
    vector<int> perm(n);
    for (int i = 0; i < n; i++) {
        perm[i] = i;
    }
    int flag = 0;
    long long count = 0;
    queue<pair<vector<int>,int>> buffer;
    buffer.push(pair<vector<int>,int>(perm,0));
    data.insert(pair<vector<int>int>(perm, 0));
    while (buffer.size() != 0) {
        count++;
        if (count % 500 == 0cout << "running at " << count << endl;
        vector<int> curr = buffer.front().first;
        int dis = buffer.front().second;
        buffer.pop();
        // make all combinations and put in map
        vector<int> tmp;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                tmp = curr;
                reverse(tmp.begin() + i, tmp.begin() + j + 1);
                if (data.find(tmp) == data.end()) {
                    if (flag < dis) {
                        cout << "current at " << dis << endl;
                        flag++;
                    }
                    data.insert(pair<vector<int>int>(tmp, dis + 1));
                    buffer.push(pair<vector<int>int>(tmp, dis + 1));
                }
            }
        }
    }
}
 
void swap(int & a, int & b) {
    int tmp = a;
    a = b;
    b = tmp;
}
 
int findminswaps(vector<int> & data) {
    map<vector<int>int> buf;
    // rescale data to 0 ... n-1
    for (int i = 0; i < data.size(); i++) {
        int smaller = 0;
        for (int j = 0; j < data.size(); j++) {
            if (data[i] > data[j]) smaller++;
        }
        data[i] = smaller;
    }
    for (int x : data) cout << x << " ";
    cout << endl;
    // make BFS
    makeBFS(data.size(), buf);
    // find min distance
    auto it = buf.find(data);
    if (it == buf.end()) return -9999;
    return it->second;
}
 
map<vector<int>int> toSort;
 
void precalc(int n) {
    vector<int> perm(n);
    for (int i = 0; i < n; ++i) {
        perm[i];
    }
    queue<vector<int>> q;
    q.push(perm);
    toSort[perm] = 0;
    int count = 0;
    while (!q.empty()) {
        count++;
        if (count % 500 == 0cout << "count at : " << count << endl;
        vector<int> here = q.front();
        q.pop();
        int cost = toSort[here];
        for (int i = 0; i < n; ++i) {
            for (int j = i + 2; j <= n; j++) {
                reverse(here.begin() + i, here.begin() + j);
                if (toSort.count(here)==0) {
                    toSort[here] = cost + 1;
                    q.push(here);
                }
                reverse(here.begin() + i, here.begin() + j);
            }
        }
    }
}
 
int solve(const vector<int> & perm) {
    int n = perm.size();
    vector<int> fixed(n);
    for (int i = 0; i < n; ++i) {
        int smaller = 0;
        for (int j = 0; j < n; ++j) {
            if (perm[j] < perm[i])
                ++smaller;
        }
        fixed[i] = smaller;
    }
    return toSort[fixed];
}
 
int main() {
    map<vector<int>int> tmp;
    vector<int> v1 = { 1,2,3,4,8,7,6,5 };
    precalc(8);
    cout << solve(v1) << endl;
    //cout << findminswaps(v1) << endl;
    while(1){}
    return 0;
}
cs