hdu 4452简单的模拟

比较简单的模拟题,可是我刚开始读题理解有问题,Tom will change his direction into Jerry's direction, and Jerry also will change his direction into Tom's original direction
这句话,我想多了,以为交换的时候,Jerry得换成Tom最开始的运动方向。改了这个就过了,1Y。

/*
 * hdu4452/win.cpp
 * Created on: 2012-10-29
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
int DIRECTION[4][4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
inline int getDirection(char c) {
    int ret;
    switch(c) {
    case 'E' : ret = 1; break;
    case 'W' : ret = 3; break;
    case 'N' : ret = 0; break;
    case 'S' : ret = 2; break;
    default: ret = 0; break;
    }
    return ret;
}
typedef struct State{
    int x, y;
    int s, t;
    int d;
    State(int xx, int yy, int ss, int tt, char c) {
        x = xx, y = yy;
        s = ss, t = tt;
        d = getDirection(c);
    }
}State;
int N;

inline bool run(State &s) {
    int x = s.x + s.s * DIRECTION[s.d][0];
    int y = s.y + s.s * DIRECTION[s.d][1];
    if(x < 1) {
        x = 2 - x;
        s.d = (s.d + 2) % 4;
    }
    if(x > N) {
        x = N + N - x;
        s.d = (s.d + 2) % 4;
    }
    if(y < 1) {
        y = 2 - y;
        s.d = (s.d + 2) % 4;
    }
    if(y > N) {
        y = N + N - y;
        s.d = (s.d + 2) % 4;
    }
    s.x = x;
    s.y = y;
    return true;
}

inline bool turnleft(State &s) {
    s.d = (s.d - 1 + 4) % 4;
    return true;
}

inline bool atsamepos(State &s1, State &s2) {
    return (s1.x == s2.x) && (s1.y == s2.y);
}

inline bool changedirection(State &tom, State &jerry) {
    int temp = tom.d;
    tom.d = jerry.d;
    jerry.d = temp;
    return true;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    char c;
    int s, t, k;
    while(scanf("%d", &N) == 1 && N > 0) {
        scanf(" %c %d %d", &c, &s, &t);
        State tom(1, 1, s, t, c);
        scanf(" %c %d %d", &c, &s, &t);
        State jerry(N, N, s, t, c);
        scanf("%d", &k);
        int now = 1;
        while(now <= k) {
            run(tom);
            run(jerry);
            if(atsamepos(tom, jerry)) {
                changedirection(tom, jerry);
            }else {
                if(now % tom.t == 0) {
                    turnleft(tom);
                }
                if(now % jerry.t == 0) {
                    turnleft(jerry);
                }
            }
            now++;
        }
        printf("%d %d\n%d %d\n", tom.x, tom.y, jerry.x, jerry.y);
    }
    return 0;
}
posted @ 2012-10-29 22:22  moonbay  阅读(...)  评论(... 编辑 收藏