一道ZZ结论题,主要是来写一写交互题的。
我们要先知道一句话:
扶着墙是肯定可以走出简单迷宫的。
然后我们冷静分析问题。若这个迷宫是(n imes m)的,那么最多有(2mn+n+m)个墙壁。
由于题目中提到方格之间都联通且形成一棵树,那么我们删去(nm-1)条边。
由于边界其中至多一半会经过一次,其余则不会经过,内部边可能经过两次,因此摸着墙壁前进的步数上限为 (2(nm+n+m+1)-3(n+m)=2nm-n-m-2)。我们在观察一下数据范围,发现:
(l>2nmge 2nm-n-m-2)。因此我们摸着墙壁走是肯定可以在规定步数内走到终点的。
然后初始时我们面向右侧,因此左侧一定有墙,所以我们只需要一直沿着左侧墙壁走即可。
由于这是交互题,因此我们可以弄出一个非常精简的核心算法:
while (!reach_dest()) move_left();
以上参考于官方题解。
然后注意一下交互题的事项即可。千万要注意请缓存,并且读进来不要连换行一起,会出锅的
CODE
#include<bits/stdc++.h>
using namespace std;
int n,m,l,d;
inline int reach_dest(void)
{
puts("reach_dest"); fflush(stdout);
int x; scanf("%d",&x); return x;
}
inline void move_left(void)
{
puts("move_left"); fflush(stdout);
int x; scanf("%d",&x);
}
int main()
{
scanf("%d%d%d%d",&n,&m,&l,&d);
while (!reach_dest()) move_left();
return 0;
}