题目链接:传送门
【题意】:
一个打扫机器人(说到机器人,恭喜LDU在2019-04-21 Robocup中获得全国冠军,为自己的学校及飞哥感到骄傲)。
然后执行指令,在一个指令条中根据当前格子的4联通即当前格子,用3进制来运算,得到的数字进行执行指令。
所以这个题目就是一个大模拟,上下左右的走(U,D,L,R),同时有一个不工作偷懒(I),还有捡起垃圾(P)。
但是捡起垃圾后会清空当前的格子,对于下一个指令也会改变。
【题解】:
因为捡起垃圾会改变状态,所以把所有标记的位置全部清空。
因为可能会走了一个圈绕回来,如果有标记位会阻碍机器人的行走。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <math.h>
#include <set>
#include <stack>
#include <bitset>
typedef long long ll;
const int N = 2e5+100;
using namespace std;
int Dir[5][2]={
{0,1}, //R
{0,-1}, //L
{1,0}, //D
{-1,0}, //U
{0,0}, //yuan
};
int dir[4][2]={
{-1,0},//U
{0,-1}, {0,1},//L.R
{1,0}//D
};
char Ins[500];
int G[2015][2015];
int G_[2015][2015];
int vis[2015][2015];
int T,n,m;
int Getx(int x,int y){
int res = 0 ;
int tmp = 1;
for(int i=0;i<5;i++){
int tx = x+Dir[i][0];
int ty = y+Dir[i][1];
res = res + G[tx][ty]*tmp;
tmp = tmp *3;
}
return res%243+1;
}
bool check(int x,int y){
return ( 2<=x&&x<n && 2<=y && y<m );
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
ll x,y,k;
scanf("%lld%lld%lld",&x,&y,&k);
scanf("%s",Ins+1);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%1d",&G[i][j]);
vis[i][j] = 0 ;
}
}
int ans = 0;
while(k--){
int ch = Getx(x,y);
if( Ins[ch]=='U'){
int tx = x +dir[0][0];
int ty = y +dir[0][1];
if( G[tx][ty] ==1 || vis[tx][ty]==3 )
break;
else {
vis[x][y] ++;
x = tx; y = ty;
}
}else if( Ins[ch] == 'L'){
int tx = x +dir[1][0];
int ty = y +dir[1][1];
if( G[tx][ty] ==1 || vis[tx][ty]==3 )
break;
else {
vis[x][y] ++;
x = tx; y = ty;
}
}else if( Ins[ch] == 'R'){
int tx = x +dir[2][0];
int ty = y +dir[2][1];
if( G[tx][ty] ==1 || vis[tx][ty]==3 )
break;
else {
vis[x][y] ++;
x = tx; y = ty;
}
}else if( Ins[ch] == 'D'){
int tx = x +dir[3][0];
int ty = y +dir[3][1];
if( G[tx][ty] ==1 || vis[tx][ty]==3 )
break;
else {
vis[x][y] ++;
x = tx; y = ty;
}
}else if( Ins[ch] == 'I'){
break;
}else if( Ins[ch] == 'P'){
if( G[x][y]!=2 ){
break;
}else{
ans++;
G[x][y] = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
vis[i][j]=0;
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}