题解:P14866 [ICPC 2020 Yokohama R] Short Coding
MaiJingYao666 · · 题解
\texttt{Solution of P14866 [ICPC 2020 Yokohama R] Short Coding}
感觉没有什么很好的靠谱做法,值得注意的点有几个,一个是 IF-OPEN 操作,还有一个是起点和终点都在边上 (别说你没看见)。
我们发扬观察力,通过以前玩迷宫的方法使用右手原则,即贴着右边的墙走,5 行能解决:
RIGHT
IF-OPEN 5
LEFT
GOTO 2
FORWARD
可以发现右手原则会让鸡器人贴着地图的外边走,而起点和终点都在外边,在保证有可达路线的情况下一定可以到达。
小于四步的再写个搜就可以了,
AC code
#include<bits/stdc++.h>
using namespace std;
int n,m;
string s[110];
int sx,sy,gx,gy;
string cmd[20]={"","FORWARD","LEFT","RIGHT","IF-OPEN 1","IF-OPEN 2","IF-OPEN 3","IF-OPEN 4","GOTO 1","GOTO 2","GOTO 3","GOTO 4"};
int ans[10];
inline void print(int len){
cout<<len<<'\n';
for(int i=1;i<=len;i++){
cout<<cmd[ans[i]]<<'\n';
}
}
bool vis[110][110][4][5];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
inline bool safe(int x,int y,int to){
int xx=x+dx[to],yy=y+dy[to];
if(xx<1 || yy<1 || xx>n || yy>m) return false;
if(s[xx][yy]=='#') return false;
return true;
}
inline bool check(int len){
int x=sx,y=sy,to=3,ti=1;
memset(vis,0,sizeof vis);
while(x!=gx || y!=gy){
if(vis[x][y][to][ti]) return false;
vis[x][y][to][ti]=1;
if(ans[ti]>=8) ti=ans[ti]-7;
else if(ans[ti]>=4){
if(safe(x,y,to)) ti=ans[ti]-3;
else ti=ti%len+1;
}
else{
if(ans[ti]==1){
if(safe(x,y,to)){
x+=dx[to];
y+=dy[to];
}
}
else if(ans[ti]==2) to=(to+4-1)%4;
else to=(to+1)%4;
ti=ti%len+1;
}
}
return true;
}
bool dfs(int p,int len){
if(p>len) return check(len);
for(int j=1;j<=11;j++){
if(j-3==p || j-7==p) continue;
if(j>7+len) continue;
if(j<=7 && j>3+len) continue;
ans[p]=j;
if(dfs(p+1,len)) return true;
ans[p]=0;
}
return false;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
s[i]=" "+s[i];
for(int j=1;j<=m;j++){
if(s[i][j]=='S'){
sx=i,sy=j;
}
if(s[i][j]=='G'){
gx=i,gy=j;
}
}
}
for(int i=1;i<=4;i++){
if(dfs(1,i)){
print(i);
return 0;
}
}
cout<<"5\n"<<"RIGHT\n"<<"IF-OPEN 5\n"<<"LEFT\n"<<"GOTO 2\n"<<"FORWARD\n";
}