UVaLive/LA 6802 Turtle Graphics(水題)

FILE 6802 - Turtle Graphics


題目大意:

類似貪吃蛇遊戲。問經過給定路徑(F-向前,L-左拐,R-右拐)後的終點座標,和經過兩次以上的點數。


解題思路:

模擬一下即可。


參考代碼:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
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;
}


發佈了60 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章