Sicily7151. 1758!

題目見:http://soj.sysu.edu.cn/7151


乍一看是搜索,其實仔細看完後就會發現是模擬。

針對不同“智商”,分情況處理即可,對每讀入的字符都進行移動。


源代碼如下:

#include <stdio.h>
void find_position(char dir, int power, char na[]);
int n, m, x, y;
int main() {
    int k, i, j, p;
    char ch;
    char name[300] = {0};
    while (scanf("%d %d %d", &n, &m, &k) && n != 0) {
        for (i = 1; i <= k; i++) {
            scanf("%s", name);
            scanf("%d %d %d", &x, &y, &p);
            getchar();
            for (; ;) {
                ch = getchar();
                find_position(ch, p, name);
                if (ch == '\n') break;
            }
        }
        printf("\n");
    }
    return 0;
}

void find_position(char dir, int power, char na[]) {
    if (dir == '\n') {
        if (x >= n || x < 0 || y >= m || y < 0)
            printf("%s is out of square!\n", na);
        else printf("%d %d\n", x, y);
        return;
    }
    if (power < 0 && (x >= n || x < 0 || y >= m || y < 0)) return;
    switch(dir) {
    case 'N' :
        y++;
        break;
    case 'S' :
        y--;
        break;
    case 'E' :
        x++;
        break;
    case 'W' :
        x--;
        break;
    }
    if (power > 0) {
        if (x >= n) x = 0;
        if (x < 0) x = n - 1;
        if (y >= m) y = 0;
        if (y < 0) y = m - 1;
    } else if (power == 0) {
        if (x >= n) x--;
        if (x < 0) x++;
        if (y >= m) y--;
        if (y < 0) y++;
    }
    return;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章