题解:P17558 「FAOI-R15」A Gleam for the Sightless
Subway_hub · · 题解
比较好玩的构造题目,如何一步步分析这种题目我觉得是比较有价值的。
::::info[Hint1]{} 我们可以查询可以确定 clue 位置范围,而且极小。 ::::
::::info[Hint2]{} 选什么点可以做到? ::::
::::success[正解]{}
我们可以通过查询得出
- 开始时,我们查
(1,1) ,根据曼哈顿距离公式,x + y = d + 2 ,其中d 为返回值。第一次我们随便问一个即可。 - 第二步非常关键,我们查
(1,m) ,此时因为 clue 会随机移动到上下左右4 格的位置,但你会发现x' + y' \in [x + y - 1, x + y + 1] ,又因为 clue 不可能不动,所以只有两种可能(有时会只有一种,因为另外一种不合法,所以答案是唯一的)!我们随机猜一个,如果对了就结束,如果错了另外一个值就一定为 clue 当前值! - 第三到七步,我们每次查
(1,1) ,根据上面的铺垫,根据已知求未知,即可。
::::info[正确性证明]{}
我们后面猜位置每次有
| 所以每次都猜错的概率: |
|---|
| ::::success[Code]{} |
| constexpr int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; |
| int n, m; |
| int ask(int a, int b){ |
| cout << a << ' ' << b << endl; |
| int d; cin >> d; |
| return d; |
| } |
| bool query(int a, int b){ |
| cout << a << ' ' << b << endl; |
| int f; cin >> f; |
| return f; |
| } |
| bool in(int x, int y){ |
| return x >= 1 && x <= n && y >= 1 && y <= m; |
| } |
| vec<pair<int, int>> get(int x, int y){ |
| vec<pair<int, int>> res; |
| for (int i = 0; i < 4; ++i){ |
| int tx = x + dx[i], ty = y + dy[i]; |
| if (in(tx, ty)){ |
| res.eb(tx, ty); |
| } |
| } |
| return res; |
| } |
| void Main() { |
| cin >> n >> m; |
| int sum1 = ask(1, 1) + 2; |
| bool find = false; |
| find = query(max(1, sum1 - m), sum1 - max(1, sum1 - m)); |
| if (find){ |
| return ; |
| } |
| int sum2 = ask(1, m) - m + 1; |
| vec<pair<int, int>> cur; |
| for (int i : {sum1 - 1, sum1 + 1}){ |
| if ((i + sum2) % 2 == 0){ |
| int x = (i + sum2) / 2, y = (i - sum2) / 2; |
| if (in(x, y)){ |
| bool f = false; |
| for (const auto& [i, j] : get(x, y)){ |
| if (i + j == sum1){ |
| f = true; |
| break; |
| } |
| } |
| if (f){ |
| cur.eb(x, y); |
| } |
| } |
| } |
| } |
| find = query(cur[0].first, cur[0].second); |
| if (find) return ; |
| auto pos = cur[1]; |
| for (int i = 3; i <= 7; ++i){ |
| auto tmp = get(pos.first, pos.second); |
| sum2 = ask(1, 1) + 2; |
| vec<pair<int, int>> tmp2; |
| for (const auto& [xx, yy] : tmp){ |
| if (xx + yy == sum2){ |
| tmp2.eb(xx, yy); |
| } |
| } |
| find = query(tmp2[0].first, tmp2[0].second); |
| if (find) return ; |
| if (i == 7) return ; |
| pos = tmp2[1]; |
| } |
| } |
| :::: |