結果
| 問題 |
No.1576 織姫と彦星
|
| ユーザー |
y05h1k1ng
|
| 提出日時 | 2021-07-01 13:28:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 5 ms / 2,000 ms |
| コード長 | 1,339 bytes |
| コンパイル時間 | 2,137 ms |
| コンパイル使用メモリ | 185,764 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-28 02:25:13 |
| 合計ジャッジ時間 | 3,843 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 54 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int hammingDistance(int a, int b) {
int x = a ^ b;
int res = 0;
while (x > 0) {
res += x & 1;
x >>= 1;
}
return res;
}
int main() {
int n, start, end;
cin >> n >> start >> end;
vector<int> stones(n);
vector<bool> visited(n);
for (int i=0; i<n; ++i) {
cin >> stones[i];
visited[i] = false;
}
map<int, vector<int>> weights;
for (int i=0; i<n; ++i) {
int weight = hammingDistance(0, stones[i]);
weights[weight].push_back(i);
}
queue<P> que;
que.push(P(0, start));
while (!que.empty()) {
P p = que.front();
que.pop();
if (hammingDistance(p.second, end) == 1) {
cout << p.first << endl;
return 0;
}
int weight = hammingDistance(0, p.second);
// weight-1
for (int i: weights[weight-1]) {
if (visited[i]) {
continue;
}
if (hammingDistance(p.second, stones[i]) == 1) {
visited[i] = true;
que.push(P(p.first+1, stones[i]));
}
}
// weight+1
for (int i: weights[weight+1]) {
if (visited[i]) {
continue;
}
if (hammingDistance(p.second, stones[i]) == 1) {
visited[i] = true;
que.push(P(p.first+1, stones[i]));
}
}
}
cout << "-1" << endl;
return 0;
}
y05h1k1ng