[CF1070A]Find a Number_bfs
Find a Number
题目链接:http://codeforces.com/problemset/problem/1070/A
数据范围:略。
题解:
因为$d$和$s$比较小可以搜。
这就很$gay$了...我根本没想到可以宽搜。
想到搜索就非常简单了,从小到大枚举就好了。
代码:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define D 510
#define S 5010
using namespace std;
int d, s;
bool vis[D][S];
queue <pair<int, int> >q;
pair<pair<int, int>, int> p[D][S];
void bfs() {
vis[0][0] = true;
q.push(make_pair(0, 0));
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 10; i ++ ) {
int x_ = (x * 10 + i) % d, y_ = y + i;
if (y_ <= s && !vis[x_][y_]) {
vis[x_][y_] = true;
p[x_][y_] = make_pair(make_pair(x, y), i);
q.push(make_pair(x_, y_));
}
}
}
}
void output(int x, int y) {
if (!x && !y) {
return;
}
output(p[x][y].first.first, p[x][y].first.second);
putchar('0' + p[x][y].second);
}
int main() {
cin >> d >> s ;
bfs();
if (!vis[0][s]) {
puts("-1");
}
else {
output(0, s);
}
return 0;
}
| 欢迎来原网站坐坐! >原文链接<

浙公网安备 33010602011771号