AcWing 3152. 兰顿蚂蚁
原题链接
简单
作者:
ycqs
,
2021-04-16 14:35:50
,
所有人可见
,
阅读 444
C++ 代码
#include<iostream>
using namespace std;
char map[101][101];
char dir[4] = {'U','L','D','R'};
int dx[4] = {-1,0,1,0}, dy[4] = {0,-1,0,1};
int m,n,x,y,k;
int i,j;
char s;
int main(){
cin>>m>>n;
for( i=0; i<m; i++){
for( j=0; j<n; j++)cin>>map[i][j];
}
cin>>x>>y>>s>>k;
for( i=0; i<k; i++){
for( j=0; j<4; j++) if(dir[j]==s)break; //找到当前方向
if(map[x][y]=='1'){
map[x][y] = '0';
if(j==0)j = 4;
s = dir[j-1]; //右转
x += dx[j-1];
y += dy[j-1];
}
else (map[x][y]=='0'){
map[x][y] = '1';
s = dir[(j+1)%4]; //左转
x += dx[(j+1)%4];
y += dy[(j+1)%4];
}
}
cout<<x<<" "<<y<<endl;
return 0;
}