6802 - Turtle Graphics
題目大意:
類似貪吃蛇游戲。問經過給定路徑(F-向前,L-左拐,R-右拐)後的終點坐標,和經過兩次以上的點數。
解題思路:
模擬一下即可。
參考代碼:
#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; }