AOJ 2103 Battle town

http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=2103

方針

書かれている通りに頑張って実装する。

ソース

#include<iostream>
#include<cassert>
using namespace std;

#define REP(i,a,b) for(i=a; i<b; ++i)
#define rep(i,n) REP(i,0,n)

string mp[20];
char tank[4] = {'<', '^', '>', 'v'};
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, -1, 0, +1 };
int w,h;

pair<int,pair<int,int> > getpos() {
    int i,j,k;
    rep(i,h) rep(j,mp[i].length()) rep(k,4)
        if(mp[i][j] == tank[k]) return make_pair(k,make_pair(i,j));
}

void solve(string com) {
    for(int i=0; i<com.length(); ++i) {
        int dir = getpos().first;
        int y = getpos().second.first;
        int x = getpos().second.second;

        if(com[i] == 'S') {
            while((y < h) && (y >= 0) && (x < w) && (x >= 0)) {
                if(mp[y][x] == '*') {
                    mp[y][x] = '.';
                    break;
                }else if(mp[y][x] == '#') {
                    break;
                }
                      y += dy[dir];
                      x += dx[dir];
            }
        }else if(com[i] == 'U') {
            if(y-1 < 0 || mp[y-1][x] != '.') mp[y][x] = '^';
            else {
                mp[y-1][x] = '^';
                mp[y][x] = '.';
            }
        }else if(com[i] == 'R') {
            if(x+1 >= w || mp[y][x+1] != '.') mp[y][x] = '>';
            else {
                mp[y][x+1] = '>';
                mp[y][x] = '.';
            }
        }else if(com[i] == 'D') {
            if(y+1 >= h || mp[y+1][x] != '.') mp[y][x] = 'v';
            else {
                mp[y+1][x] = 'v';
                mp[y][x] = '.';
            }
        }else if(com[i] == 'L') {
            if(x-1 < 0 || mp[y][x-1] != '.') mp[y][x] = '<';
            else {
                mp[y][x-1] = '<';
                mp[y][x] = '.';
            }
        }else assert(false);

    }
}

int main() {
    int t,n;
    string com;
    cin>>t;
    while(t--) {
        cin>>h>>w;
        for(int i=0; i<h; ++i) cin>>mp[i];
        cin>>n;
        cin>>com;

        solve(com);
        for(int i=0; i<h; ++i) cout<<mp[i]<<endl;
        if(t > 0) cout<<endl;
    }
}