LA 6802 Turtle Graphics (water question)
Question:
Similar to snake games. Ask the coordinates of the end point after the given path (F-forward, L-left, R-right) and after more than two points.
Solution:
Simulate it.
Reference code:
#include
#include
#include
#include
using namespace std;const int MAXN = 100;const int dx[4] = {0, 1, 0, -1};const int dy[4] = {1, 0, -1, 0};int nCase, cCase, x, y, ans;string str;int visited[MAXN][MAXN];void init() { memset(visited, 0, sizeof(visited)); ans = 0;}void input() { scanf("%d%d", &x, &y); cin >> str;}void solve() { int direct = 0; visited[x][y] = true; for (int i = 0; i < str.length(); i++) { if (str[i] == 'F') { x += dx[direct]; y += dy[direct]; if (visited[x][y] == 1) { ans++; } visited[x][y]++; } if (str[i] == 'L') { direct = (direct - 1 + 4) % 4; } if (str[i] == 'R') { direct = (direct + 1) % 4; } } printf("Case #%d: %d %d %d\n", ++cCase, x, y, ans);}int main() { scanf("%d", &nCase); while (nCase--) { init(); input(); solve(); } return 0;}