USACO 2.2 Runaround Numbers

Straight forward brutal force approach. Mainly test implementation skills.

#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <set>
#include <unordered_map>

using namespace std;

int N;

bool isRunaroundNumber(int n) {
vector<int> num;
unordered_map<int, int> map;
while (n) {
int d = n % 10;
if (map.find(d) != map.end()) {
return false;
}
map[d] = 0;
num.push_back(d);
n /= 10;
}

reverse(num.begin(), num.end());
int size = (int)num.size(), index = 0, next = -1;
map[num[0]] = 1;

while (next != 0) {
int next = (index + num[index]) % size;
if (next == 0) {
for (int i = 0; i < size; ++i) {
if (map[num[i]] != 1)
return false;
}
return true;
}
if (map[num[next]] == 1)
return false;
map[num[next]]++;
index = next;
}
return false;
}

int main() {
ifstream fin("runround.in");
ofstream fout("runround.out");
fin >> N;
++N;
while (!isRunaroundNumber(N++));
fout << --N << endl;
}