uva : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1091

 

doublets 의 의미는 한 쌍의 단어를 비교했을 떄 알파벳이 1개만 다른 단어의 쌍을 말한다

[aaa, aab] 같은 경우가 됨

 

문제풀이는 다음과 같다

1. 단어의 사전이 개행이 나오기 전까지 주어진다

2. 단어의 한 쌍이 주어진다

3. 주어진 한 쌍 중에서 첫번째 단어에서 두번째 단어로 가는 가장 짧은 경로를 찾는다

4. EOF가 아니면 2번부터 다시 반복한다

 

일단 최단경로에 관한 문제이기 때문에 bfs로 접근했다

탐색과정은 다음과 같다

1. bfs탐색은 현재 단어를 기준으로 방문하지 않은 doublets string을 찾았다.

2. end string을 발견하면 bfs 종료 후 탐색해왔던 string을 찾는다. 

 

아 참고로 문제 인풋이 좀 더러운 문제였다

사전이 주어질 때 단어 뒤에 ' ' 형태의 공백이 같이 딸려오는 인풋이 있어서 pop_back() 을 통해 정확한 단어를 삽입했다

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <string>
#include <cstring>
using namespace std;
 
struct pii {
    string my_str;
    int my_cnt;
    int my_num;
};
 
vector<string> dict;
int arr[30000];
 
bool check(string a, string b) {
    if (a.size() != b.size()) return false;
    int cnt = 0;
    for (int i = 0; i < a.size(); i++) {
        if (a[i] != b[i]) cnt++;
        if (cnt > 1return false;
    }
    if (cnt == 1return true;
    return false;
}
 
vector<string> solve(string start, string endint pin) {
    set<string> visit;
    queue<pii> q;
    vector<pair<string,int> > btr;
    vector<string> answer;
    int last_pin;
    bool sol = false;
 
    q.push({ start,0,pin});
    while (!q.empty()) {
        string str = q.front().my_str;
        int cnt = q.front().my_cnt;
        int my_num = q.front().my_num;
        q.pop();
 
        if (str.compare(end== 0) {
            sol = true;
            last_pin = my_num;
            break;
        }
 
        int visit_size = visit.size();
        visit.insert(str);
        if (visit_size == visit.size()) continue;
 
        for (int i = 0; i < dict.size(); i++) {
            if (check(str, dict[i])) {
                visit_size = visit.size();
                visit.insert(dict[i]);
                if (visit_size != visit.size()) {
                    visit.erase(dict[i]);
                    q.push({ dict[i], cnt + 1, i});
                    if (arr[i] == -1) arr[i] = my_num;
                }
            }
        }
    }
 
    if (!sol) return answer;
    else {
        answer.push_back(end);
        while (1) {
            string str = dict[arr[last_pin]];
            answer.push_back(str);
            if (str.compare(start) == 0break;
            last_pin = arr[last_pin];
        }
        return answer;
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    string str, start, end;
    while (getline(cin, str), str != "") {
        while (1) {
            if (str[str.size() - 1== ' ') str.pop_back();
            else break;
        }
        dict.push_back(str);
    }
 
    while (cin >> start >> end) {
        memset(arr, -1sizeof(arr));
 
        if (start.size() != end.size()) {
            cout << "No solution.\n\n";
            continue;
        }
        if (start.compare(end== 0) {
            cout << end << "\n" << end << "\n\n";
            continue;
        }
 
        int pin;
        for (int i = 0; i < dict.size(); i++) {
            if (start.compare(dict[i]) == 0) {
                pin = i;
                break;
            }
        }
        vector<string> answer = solve(start, end, pin);
 
        if (answer.size() == 0cout << "No solution.\n";
        else for (int i = answer.size() - 1; i >= 0; i--cout << answer[i] << "\n";
        cout << "\n";
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > uva' 카테고리의 다른 글

uva 11994번 Happy Painting!  (0) 2020.04.06
uva 384번 Slurpys, BOJ 14906번 스러피  (0) 2020.03.31
uva 10469번 To Carry or not to Carry  (0) 2020.03.23
uva 10010번 Where's Waldorf?  (0) 2020.03.23
uva 679 - Dropping Balls  (0) 2020.03.19

uva : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&page=show_problem&problem=1410

 

10진수 -> 2진수로 변경 후

carry 값을 0으로 만들어주면 끝나는 매우 쉬운 문제

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
#include <string>
using namespace std;
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    long long n1, n2;
    int a[40], b[40];
    while (1) {
        cin >> n1 >> n2;
        if (cin.eof()) break;
 
        memset(a, 0sizeof(a));
        memset(b, 0sizeof(b));
 
        // 10진수 -> 2진수
        string astr = "", bstr = "";
        while (n1) {
            if (n1 == 1) {
                astr += '1';
                break;
            }
            astr += (n1 % 2+ '0';
            n1 /= 2;
        }
        while (n2) {
            if (n2 == 1) {
                bstr += '1';
                break;
            }
            bstr += (n2 % 2+ '0';
            n2 /= 2;
        }
        for (int i = astr.size() - 1; i >= 0; i--) a[i] = astr[i] == '1' ? 1 : 0;
        for (int i = bstr.size() - 1; i >= 0; i--) b[i] = bstr[i] == '1' ? 1 : 0;
 
        int len = max(astr.size(), bstr.size());
        long long ans = 0;
        for (int i = 0; i < len; i++) {
            if (a[i] && b[i]) continue;
            if (a[i] || b[i]) ans += pow(2, i);
        }
        cout << ans << "\n";
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > uva' 카테고리의 다른 글

uva 384번 Slurpys, BOJ 14906번 스러피  (0) 2020.03.31
uva 10150번 Doublets  (0) 2020.03.27
uva 10010번 Where's Waldorf?  (0) 2020.03.23
uva 679 - Dropping Balls  (0) 2020.03.19
uva 10137 - the trip, BOJ 4411번  (0) 2020.03.19

uva : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=951

 

간단한 dfs 문제

 

* 같은 방향으로 원하는 문자열을 찾으면 되는 문제이다

* 반드시 문자열을 찾을 수 있다.

* 답이 여러개인 경우 가장 왼쪽부근에 위치한 답을 출력하라고 하는데,

  그냥 행->열 순서로 탐색하면 해결된다.

 

alpha 함수는 대소문자가 같은 경우를 리턴하는 함수

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
#include <string>
using namespace std;
 
string arr[51];
bool visit[52][52];
int row, col;
int mx[8= { 0,1,1,1,0,-1,-1,-1 };
int my[8= { -1,-1,0,1,1,1,0,-1 };
 
bool alpha(char now_al, char stand) {
    if (now_al == stand || now_al - '0' + 32 == stand - '0' || now_al - '0' - 32 == stand - '0'return true;
    else return false;
}
 
bool dfs(int y, int x, int cnt, string str, int direct) {
    if (cnt == str.size()) return true;
 
    for (int i = 0; i < 8; i++) {
        if (direct == -1 || direct == i) {
            int yy = y + my[i];
            int xx = x + mx[i];
            if (yy >= 0 && yy < row && xx >= 0 && xx < col && !visit[yy][xx]) {
                if (alpha(arr[yy][xx], str[cnt])) {
                    visit[yy][xx] = true;
                    if (dfs(yy, xx, cnt + 1, str, i)) return true;
                    visit[yy][xx] = false;
                }
            }
        }
    }
 
    return false;
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    int testcase;
   cin >> testcase;
 
    while (testcase--) {
       cin >> row >> col;
        for (int i = 0; i < row; i++)
           cin >> arr[i];
        
        vector<pair<intpair<intint> > > v;
        int test;
       cin >> test;
        while (test--) {
            string str;
           cin >> str;
            
            bool check = false;
 
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (alpha(str[0], arr[i][j])) {
                        if (dfs(i, j, 1, str, -1)) {
                           cout << i + 1 << " " << j + 1 << "\n";
                            memset(visit, 0sizeof(visit));
                            check = true;
                            break;
                        }
                    }
                }
                if (check) break;
            }
        }
       cout << "\n";
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > uva' 카테고리의 다른 글

uva 10150번 Doublets  (0) 2020.03.27
uva 10469번 To Carry or not to Carry  (0) 2020.03.23
uva 679 - Dropping Balls  (0) 2020.03.19
uva 10137 - the trip, BOJ 4411번  (0) 2020.03.19
uva 100 - 3n+1 problem  (0) 2020.03.18

uva : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=620

 

FBT 는 배열로 쉽게 표현가능합니다.

 

현재 위치를 flag 로 표현할 때, 

좌 -> 우 로 자식노드를 확인해주면서 마지막 리프노드에 도달하는 순서를 확인하는 문제입니다.

좌 가 false 이면 방문 true 이면 우 를 확인

우 가 false 이면 방문 true 이면 둘다 false 갱신 후 좌 부터 진입

 

재귀를 이용했습니다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <cmath>
#include <limits>
#include <cstring>
using namespace std;
 
int deep;
int visit[1 << 20];
 
void dfs(int node, int now_cnt, int cnt) {
    if (node >= (1 << (deep - 1)) && now_cnt == cnt) {
        cout << node << "\n";
        return;
    }
 
    if (node >= (1 << (deep - 1))) {
        visit[node] = 1;
        return;
    }
 
    visit[node] = 1;
 
    if (!visit[node * 2]) dfs(node * 2, now_cnt, cnt);
    else if (!visit[node * 2 + 1]) dfs(node * 2 + 1, now_cnt, cnt);
    else {
        visit[node * 2= visit[node * 2 + 1= 0;
        dfs(node * 2, now_cnt, cnt);
    }
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    int T; cin >> T;
    while (T--) {
        int cnt;
        cin >> deep >> cnt;
 
        for (int i = 1; i <= cnt; i++)
            dfs(1, i, cnt);
 
        memset(visit, 0sizeof(visit));
    }
    int num = -1;
    cin >> num;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > uva' 카테고리의 다른 글

uva 10150번 Doublets  (0) 2020.03.27
uva 10469번 To Carry or not to Carry  (0) 2020.03.23
uva 10010번 Where's Waldorf?  (0) 2020.03.23
uva 10137 - the trip, BOJ 4411번  (0) 2020.03.19
uva 100 - 3n+1 problem  (0) 2020.03.18

+ Recent posts