题解:P13919 [PO Final 2024] 黑暗密室 / The Dark Chambers of Chalmers
第一次过黑题,必须写篇题解庆祝一下。
38pts
::::info[Hint 1-1]
假设样例为满二叉树,一个点在最左端,另一个点在最右端,那么 app 将会检测到 -1,那么要先怎么操作来避免这种情况?
::::
::::info[Hint 1-2] 尝试移动到最上面。 ::::
::::info[Hint 1-3] 可以尝试每层通过一次询问来找到目标房间所在的子树。 ::::
::::success[38pts 解法]
首先移动到最上面,然后每层通过一次询问来确定位置,假设上一次询问的结果为
-
- 否则就是走对了,直接进入下一次循环。
当询问结果为 here 即可。 |
|---|
::::success[38pts 代码]
#include<iostream>
#include<string>
using namespace std;
const int N = 1010;
string s, s2; int x, y;
int main(){
cin >> s;
while(s[0] == '1'){
cout << "up" << endl;
cin >> s;
}
cout << "app" << endl;
cin >> x;
while(x){
if(s[2] == '1'){
cout << "downright" << endl;
cin >> s2;
}
else{
cout << "downleft" << endl;
cin >> s2;
}
cout << "app" << endl;
cin >> y;
if(y == x){
if(s[2] == '1'){
cout << "left" << endl;
cin >> s;
}
else{
cout << "right" << endl;
cin >> s;
}
x = y-1;
}
else x = y, s = s2;
}
cout << "here" << endl;
return 0;
}
::::
59pts
::::info[Hint 2-1]
既然询问次数被限制在
::::info[Hint 2-2]
考虑这种情况(
| 此时若询问结果 |
|---|
::::info[Hint 2-3] 考虑避免上一个提示的情况。 ::::
::::success[59pts 解法] 考虑每两层进行一次询问,先走向一边,根据不同的地形进行不同的讨论:
注意需要提前判定是否要换一边,如果要换的话是否存在另一边,最后每两层问一次即可。
那么这个为什么不是正解呢?
| 首先,我们在开头即需要一次询问,然后依次在第 |
|---|
::::success[59pts 代码]
#include<iostream>
#include<string>
using namespace std;
const int N = 1010;
string s, s2; int x, y;
int main(){
cin >> s;
while(s[0] == '1'){
cout << "up" << endl;
cin >> s;
}
cout << "app" << endl;
cin >> x;
while(x){
if(x == 1){
if(s[2] == '1'){
cout << "downright" << endl;
cin >> s2;
cout << "app" << endl;
cin >> x;
if(x == 0) break;
cout << "up" << endl;
cin >> s2;
}
if(s[3] == '1'){
cout << "downleft" << endl;
cin >> s2;
break;
}
}
if(s[2] == '0'){
cout << "downleft" << endl;
cin >> s; x--;
continue;
}
if(s[3] == '0'){
cout << "downright" << endl;
cin >> s; x--;
continue;
}
cout << "downright" << endl;
cin >> s;
if(s[2] == '0' && s[3] == '0'){
cout << "left" << endl;
cin >> s; x--;
}
else if(s[2] == '0' && s[3] == '1'){
cout << "downleft" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s2;
cout << "downleft" << endl;
cin >> s;
}
else if(y == x){
cout << "left" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s;
}
else if(y == x-1){
cout << "left" << endl;
cin >> s;
}
x -= 2;
}
else if(s[2] == '1' && s[3] == '0'){
if(s[4] == '0'){
cout << "downright" << endl;
cin >> s;
}
else{
cout << "left" << endl;
cin >> s;
if(s[2] == '0'){
cout << "downleft" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "right" << endl;
cin >> s2;
cout << "downright" << endl;
cin >> s;
}
}
else{
cout << "downright" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x-1){
cout << "left" << endl;
cin >> s;
}
else if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "right" << endl;
cin >> s2;
cout << "downright" << endl;
cin >> s;
}
}
}
x -= 2;
}
else{
cout << "downright" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s2;
cout << "downleft" << endl;
cin >> s;
}
else if(y == x){
cout << "left" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s;
}
else if(y == x-1){
cout << "left" << endl;
cin >> s;
}
x -= 2;
}
}
cout << "here" << endl;
return 0;
}
::::
100pts
::::info[Hint 3-1] 我们仅仅是因为多了一次 app 查询而出错,考虑在前四层进行两次询问来避免这种情况。 ::::
::::info[Hint 3-2] 我们有没有利用题目中所有的询问? ::::
::::info[Hint 3-3] 在 59 分做法中我们还有很多的移动操作没有使用,能否利用它们? ::::
::::success[100pts 解法] 可以用多的移动操作来摸清楚前四层的地形。
然后暴力枚举出所有前四层的情况,你会发现一定存在两个点,使得询问后可以区分出所有子树,于是我们用 floyd 算法求出全源最短路,然后暴力枚举两个节点,看询问这两个点是否可以区分出所有子树。
| 具体的写法在代码注释中有写。 |
|---|
::::success[100pts 代码]
#include<iostream>
#include<string>
#include<vector>
#include<set>
using namespace std;
const int N = 20;
string s, s2; int x, y, nn, w[N], rt[4], rtn[N];
vector<int> g[N], q;
int dfs(int dep, string s, int x, int bs){
// dep 表示到达的层数,3 表示第一层,2 表示第二层,以此类推。
// s 表示该位置的询问结果。
// x 表示每一个节点需要的步数的三进制表示,1 为 downright,2 为 downleft
// bs 为当前节点的位数。
// g 为地形。
// q 为第四层节点。
// w 为到达该位置的步数的三进制表示。
if(dep == 0){
q.push_back(++nn);
w[nn] = x; rtn[nn] = rt[dep]; rt[dep] = nn;
return nn;
}
int idx = ++nn; w[idx] = x;
rtn[idx] = rt[dep]; rt[dep] = idx;
if(s[2] == '1'){
cout << "downright" << endl;
string tmp; cin >> tmp;
g[idx].push_back(dfs(dep-1, tmp, x + 1 * bs, bs*3));
cout << "up" << endl;
cin >> tmp;
// 走完还原。
}
else{
for(int i = dep-1; i >= 0; i--) rt[i] = -1;
}
if(s[3] == '1'){
cout << "downleft" << endl;
string tmp; cin >> tmp;
g[idx].push_back(dfs(dep-1, tmp, x + 2 * bs, bs*3));
cout << "up" << endl;
cin >> tmp;
// 走完还原。
}
else{
for(int i = dep-1; i >= 0; i--) rt[i] = -1;
}
return idx;
}
struct pii{
int val1, val2;
bool operator < (const pii & o) const{
if(val1 == o.val1) return val2 < o.val2;
return val1 < o.val1;
}
pii operator + (const pii & o) const{
return {val1 + o.val1, val2 + o.val2};
}
int s(){
return val1 + val2;
}
} f[N][N];
void moving(){
set<int> st;
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++) f[i][j] = {(int)1e9, (int)1e9};
f[i][i] = {0, 0};
}
for(int i = 1; i <= nn; i++){
for(int j: g[i]) f[i][j] = f[j][i] = {1, 0};
}
for(int i = 1; i <= nn; i++){
if(rtn[i] != -1) f[i][rtn[i]] = f[rtn[i]][i] = {0, 1};
}
// floyd
for(int k = 1; k <= nn; k++){
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++){
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
}
// 查找两个位置的询问能否区分出所有第 4 层的位置。
int d1 = -1, d2;
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++){
st.clear();
for(int k: q){
st.insert(f[i][k].s() - f[j][k].s());
}
if(st.size() == q.size()){
d1 = i, d2 = j;
break;
}
}
if(d1 != -1) break;
}
int dep = 0, q1, q2, w1, w2;
w1 = w[d1], w2 = w[d2];
// 询问节点 1。
while(w1){
if(w1 % 3 == 2){
cout << "downleft" << endl;
cin >> s;
}
else{
cout << "downright" << endl;
cin >> s;
}
w1 /= 3; dep++;
}
cout << "app" << endl;
cin >> q1;
// 还原。
while(dep--){
cout << "up" << endl;
cin >> s;
}
dep = 0;
// 询问节点 2。
while(w2){
if(w2 % 3 == 2){
cout << "downleft" << endl;
cin >> s;
}
else{
cout << "downright" << endl;
cin >> s;
}
w2 /= 3; dep++;
}
cout << "app" << endl;
cin >> q2;
// 还原。
while(dep--){
cout << "up" << endl;
cin >> s;
}
// 如果距离小于 10,说明目标位置可能在前 4 层,直接返回按 59 分做法。
if(q1 < 10){
cout << "app" << endl;
cin >> x;
return;
}
// 走到查找的位置。
for(int k: q){
if(f[d1][k].s() - f[d2][k].s() == q1 - q2){
while(w[k]){
if(w[k] % 3 == 2){
cout << "downleft" << endl;
cin >> s;
}
else{
cout << "downright" << endl;
cin >> s;
}
w[k] /= 3; dep++;
}
x = q1 - f[d1][k].s();
if(q1 - f[d1][k].s() != q2 - f[d2][k].s()){
cout << "114514" << endl;
}
break;
}
}
}
int main(){
cin >> s;
while(s[0] == '1'){
cout << "up" << endl;
cin >> s;
}
for(int i = 0; i < 4; i++) rt[i] = -1;
dfs(3, s, 0, 1); // 探索前 4 层的地形。
if(q.size() == 0){
cout << "app" << endl;
cin >> x;
}
else if(q.size() == 1){
while(w[q[0]]){
if(w[q[0]] % 3 == 2){
cout << "downleft" << endl;
cin >> s;
}
else{
cout << "downright" << endl;
cin >> s;
}
w[q[0]] /= 3;
}
cout << "app" << endl;
cin >> x;
}
else{
moving();
}
// 这下面的代码已经足够得到 59 分。
while(x){
if(x == 1){
if(s[2] == '1'){
cout << "downright" << endl;
cin >> s2;
cout << "app" << endl;
cin >> x;
if(x == 0) break;
cout << "up" << endl;
cin >> s2;
}
if(s[3] == '1'){
cout << "downleft" << endl;
cin >> s2;
break;
}
}
if(s[2] == '0'){
cout << "downleft" << endl;
cin >> s; x--;
continue;
}
if(s[3] == '0'){
cout << "downright" << endl;
cin >> s; x--;
continue;
}
cout << "downright" << endl;
cin >> s;
if(s[2] == '0' && s[3] == '0'){
cout << "left" << endl;
cin >> s; x--;
}
else if(s[2] == '0' && s[3] == '1'){
cout << "downleft" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s2;
cout << "downleft" << endl;
cin >> s;
}
else if(y == x){
cout << "left" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s;
}
else if(y == x-1){
cout << "left" << endl;
cin >> s;
}
x -= 2;
}
else if(s[2] == '1' && s[3] == '0'){
if(s[4] == '0'){
cout << "downright" << endl;
cin >> s;
}
else{
cout << "left" << endl;
cin >> s;
if(s[2] == '0'){
cout << "downleft" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "right" << endl;
cin >> s2;
cout << "downright" << endl;
cin >> s;
}
}
else{
cout << "downright" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x-1){
cout << "left" << endl;
cin >> s;
}
else if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "right" << endl;
cin >> s2;
cout << "downright" << endl;
cin >> s;
}
}
}
x -= 2;
}
else{
cout << "downright" << endl;
cin >> s;
cout << "app" << endl;
cin >> y;
if(y == x+1){
cout << "up" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s2;
cout << "downleft" << endl;
cin >> s;
}
else if(y == x){
cout << "left" << endl;
cin >> s2;
cout << "left" << endl;
cin >> s;
}
else if(y == x-1){
cout << "left" << endl;
cin >> s;
}
x -= 2;
}
}
cout << "here" << endl;
return 0;
}
::::