-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfroggerhard.cpp
72 lines (66 loc) · 1.64 KB
/
froggerhard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//Kattis Problem 1-D Frogger Hard
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
vector<vector<int>> winPairs; //caching, for speed
vector<vector<int>> losePairs; //caching, for speed
vector<int> board;
int numSpaces;
int total = 0;
void frogger(int start, int magic)
{
int frog = start; //frog will be used to index board
set<int> visited; //visited positions in board
//cout << "Testing " << start << " and " << magic << endl;
while(69)
{
if (board[frog] == magic)
{
total++;
return;
}
int move = frog + board[frog];
if(move < 0 || move > numSpaces || visited.count(move))
{
losePairs[start].push_back(magic); //this (start,magic is a losing pair)
return;
}
else
{
frog = move;
visited.insert(move);
winPairs[start].push_back(magic); //this (start,magic is a winning pair)
//cout << "Inserting: " << move << endl;
}
}
}
int main() {
cin >> numSpaces;
//Read in the board
for(int i = 0; i < numSpaces; i++)
{
int middleMan;
cin >> middleMan;
board.push_back(middleMan);
vector<int> middleVect;
winPairs.push_back(middleVect);
losePairs.push_back(middleVect);
}
//Traverse the board
for(int i = 0; i < numSpaces; i++) //start from each space
{
for(int j = 0; j < numSpaces; j++) //use each possible magic number for that start
{
//if (j == i) continue; //i is a position, j is a number
if(find(winPairs[i].begin(), winPairs[i].end(), j) == winPairs[i].end())
frogger(i, board[j]);
else if(find(losePairs[i].begin(), losePairs[i].end(), j) == losePairs[i].end())
continue;
else total++;
}
}
cout << total;
return 0;
}