题意:有一个ROW*COL的矩形,其中每一个1*1的小块上有NSWE四个方向,一个机器人站在这上面,按照指定方向前进,问最后小机器人能不能出去或者在里面怎么循环。
想法:建一个矩形,然后按照提示每一步走就行了。。这几天做的模拟题都好水。。。
代码如下:
#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<cstring>
#include<sstream>
#include<set>
#include<string>
#include<iterator>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int mmap[15][15];
char m[15][15];
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int row, col,step,x,y;
char sign;
string str[15];
while (cin >> col >> row >> x, col || row || x) {
memset(mmap, 0, sizeof(mmap));
for (int i = col; i >= 1; --i)
for (int j = 1; j <= row; ++j)
cin >> m[j][i];
step = 1;
y = col;
while (x > 0 && x <= row&&y > 0 && y <= col&&(!mmap[x][y])) {
mmap[x][y] = step;
sign = m[x][y];
if (sign == 'N') y++;
else if (sign == 'S')y--;
else if (sign == 'W') x--;
else if (sign == 'E') x++;
step++;
}
if (mmap[x][y])
cout << mmap[x][y] - 1 << " step(s) before a loop of " << step - mmap[x][y] << " step(s)" << endl;
else cout << step-1<<" step(s) to exit" << endl;
}
return 0;
}