题意:给你一个HxW的矩阵,每个点是一个指令,根据指令进行一系列操作。
题解:模拟
#include<cstdio> #include<algorithm> using namespace std;const int maxn = 101; char G[maxn][maxn];int dx[] = {-1,0,1, 0}; int dy[] = { 0,1,0,-1};struct pointer {int r,c;int dir;void Move(){r += dx[dir];c += dy[dir];}char read(){return G[r][c];} }p;int h,w; const int up_bound = 1e5; const int TimeLim = 1e6; const int MaxN = 1e5+5; int readLim; int readList[MaxN];void execute() {p.r = p.c = 0;int Register[26] = {0};int cur = 0;char op = p.read();p.dir = 1;int step = 1;int ReadTimes = 0;while(op!='#'){if('A'<=op&&op<='Z'){swap(Register[op-'A'],cur);}else switch(op){case '^':{p.dir = 0;break;}case '>':{p.dir = 1;break;}case 'v':{p.dir = 2;break;}case '<':{p.dir = 3;break;}case '?':{if(ReadTimes>=readLim){cur = readList[readLim-1];}else {cur = readList[ReadTimes];ReadTimes++;}break;}case '!':{printf("%d\n",cur);cur = 0;break;}case '+':{cur++;if(abs(cur)>up_bound){printf("OVERFLOW ERROR\n");return;}break;}case '-':{cur--;if(abs(cur)>up_bound){printf("OVERFLOW ERROR\n");return;}break;}case '@':{if(cur){ p.dir = (p.dir+1)%4; }else { p.dir = (p.dir+3)%4; }break;}}step++;if(step>TimeLim) {printf("TIME LIMIT EXCEEDED\n");return;}p.Move();if(p.r<0||p.r>=h||p.c<0||p.c>=w) {printf("RUNTIME ERROR\n"); return;}op = p.read();} }int main() {scanf("%d%d",&h,&w);for(int i = 0; i < h; i++)scanf("%s",G[i]);scanf("%d",&readLim);for(int i = 0; i < readLim; i++){scanf("%d",readList+i);}execute();return 0; }