充分な回数繰り返す。
class SequenceOfCommands {
public:
string whatHappens(vector <string> cs)
{
string c;
for (int i = 0; i < cs.size(); ++i) {
c += cs[i];
}
set< pair<int, int> > vis;
int x = 0, y = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int dir = 0;
vis.insert(make_pair(x, y));
for (int loop = 10000; loop--; ) {
for (int i = 0; i < c.size(); ++i) {
if (c[i] == 'S') {
x += dx[dir];
y += dy[dir];
} else if (c[i] == 'R') {
dir = (dir + 1) % 4;
} else if (c[i] == 'L') {
dir = (dir - 1 + 4) % 4;
}
}
pair<int, int> p = make_pair(x, y);
if (vis.count(p)) return "bounded";
vis.insert(p);
}
return "unbounded";
}
};
0 件のコメント :
コメントを投稿