-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8111.cpp
50 lines (45 loc) · 898 Bytes
/
8111.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
typedef pair<int, string>pi;
typedef pair<int, pair<int, int>>pii;
int t, n;
string bfs() {
bool visit[20005][101] = { 0, };
queue<pi>q;
q.push({ n,"" });
while (!q.empty()) {
int bfr = q.front().first;
int len = q.front().second.length();
string str = q.front().second;
q.pop();
if (len > 100) continue;
if (bfr == 0 && len ) {
string tmp;
for (int i = len - 1; i >= 0; i--)
{
tmp += str[i];
}
return tmp;
}
visit[bfr][len] = 1;
for (int i = 0; i < 10; i++)
{
int x = bfr + (n*i);
if (x % 10 == 1 || x % 10 == 0) {
if (visit[x / 10][len + 1]) continue;
visit[x / 10][len + 1] = 1;
q.push({ x / 10, str + to_string(x % 10)});
}
}
}
return "BRAK";
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
string ans = bfs();
cout << ans << "\n";
}
}