ghost in the shellcode edgy
编程题
题意:一张图,一个起点,一堆障碍,从起点出发,给出一条线路,给定限制的重复次数,他会按照重复的方式走,请问怎么走?我们可以枚举位移,然后根据其暴力询问下一个位置是否合法,从而降低复杂度。
具体参见
https://wiki.mma.club.uec.ac.jp/CTF/Writeup/Ghost in the Shellcode 2015
https://www.robertxiao.ca/hacking/ctf-writeup/gits2015-edgy/
这里只提供我写的代码
#include <bits/stdc++.h>
using namespace std;
int row,col,limit;
int start_x,start_y;
char mymap[2555][2555];
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
char haha[]="AWDS";
bool vis[1500][1500];
char ask(int x,int y){
if(x < 0 || x >= row || y < 0 || y >= col){
return ' ';
}
return mymap[x][y];
}
bool valid(int x,int y){
return x>=0&&x<row&&y>=0&&y<col;
}
bool ok = false;
string ans;
#define MAX 2333
void dfs(int mx,int my,int deltx,int delty,int now){
if(now==0){
vis[start_x][start_y] = true;
}
if(now>limit){
return;
}
if(mx==deltx&&my==delty){
cout<<ans<<endl;
exit(0);
}
for(int i=0;i<4;++i){
int fx = dx[i] + mx;
int fy = dy[i] + my;
int tx = start_x + fx;
int ty = start_y + fy;
bool flag = true;
int cnt = 0;
while(true){
if(ask(tx,ty)=='x'){
flag = false;
break;
}
if(!valid(tx,ty)&&!valid(tx+deltx,ty+delty)) break;//说明整个图已经跑到外面去了
tx += deltx;
ty += delty;
}
if(!flag) continue;
if(!vis[start_x+fx][start_y+fy]){
vis[start_x+fx][start_y+fy] = 1;
ans += haha[i];
dfs(fx,fy,deltx,delty,now+1);
vis[start_x+fx][start_y+fy] = 0;
ans.pop_back();
}
}
}
int main(){
cin>>row>>col>>limit;
getchar();
for(int i=0;i<row;++i){
for(int j=0;j<col;++j){
scanf("%1c",&mymap[i][j]);
if(mymap[i][j]=='@'){
start_x = i;
start_y = j;
}
}
getchar();
}
for(int i=0;i<=limit;i++){
for(int j=0;j+i<=limit;++j){
if(i+j){
dfs(0,0,i,j,0);
if(i)dfs(0,0,-i,j,0);
if(j)dfs(0,0,i,-j,0);
if(i&&j)dfs(0,0,-i,-j,0);
}
}
}
return 0;
}