I'm getting some strange errors on a USACO training problem (clocks). I suspect that I'm accessing memory out of bounds somewhere in my code, but I can't figure out where...
Errors
Compiling...
Compile: OK
Executing...
Test 1: TEST OK [0.000 secs, 3356 KB]
Test 2: TEST OK [0.000 secs, 3356 KB]
Test 3: TEST OK [0.281 secs, 9032 KB]
Test 4: TEST OK [0.659 secs, 14708 KB]
> Run 5: Execution error: Your program had this runtime error:
Illegal file open (/sys/devices/system/cpu/online). The program
ran for 0.767 CPU seconds before the error. It used 16292 KB of
memory.
------ Data for Run 5 [length=26 bytes] ------
6 12 12
12 12 12
12 12 12
----------------------------
Test 5: SIGNAL 50 (0.767 secs, 16292 KB)
My Code:
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <map>
#define ord(ch) ((int)(ch - 'A'))
using namespace std;
vector<string> moves;
vector<int> ideal;
void print(vector<int>);
vector<int> find(vector<int>);
vector<int> move(vector<int>, int);
void turn(vector<int> &, int);
int main(void)
{
moves.resize(9);
ideal.resize(9);
moves[0] = "ABDE";
moves[1] = "ABC";
moves[2] = "BCEF";
moves[3] = "ADG";
moves[4] = "BDEFH";
moves[5] = "CFI";
moves[6] = "DEGH";
moves[7] = "GHI";
moves[8] = "EFHI";
for (int i = 0; i < 9; i++)
ideal[i] = 12;
vector<int> v(9);
ifstream f("
clocks.in");
ofstream g("clocks.out");
if (f.fail())
{
cerr << "FILE ERROR" << endl;
return 1;
}
for (int i = 0; i < 9; i++)
f >> v[i];
vector<int> sol = find(v);
for (vector<int>::iterator it = sol.begin(); it < sol.end(); ++it)
g << *it + 1 << (it < sol.end() - 1 ? " " : "");
g << endl;
}
vector<int> find(vector<int> v)
{
queue< vector<int> > open_list;
open_list.push(v);
map< vector<int>, vector<int> > path;
path[v] = vector<int>();
while (open_list.size() > 0)
{
vector<int> curr = open_list.front();
open_list.pop();
for (int i = 0; i < 9; i++)
{
vector<int> next = move(curr, i);
if (path.find(next) == path.end())
{
path[next] = path[curr];
path[next].push_back(i);
open_list.push(next);
}
}
if (path.find(ideal) != path.end())
return path[ideal];
}
return vector<int>();
}
vector<int> move(vector<int> v, int n)
{
for (string::iterator it = moves[n].begin(); it < moves[n].end(); ++it)
turn(v, ord(*it));
return v;
}
void turn(vector<int> &v, int n)
{
v[n] = (v[n] == 12 ? 3 : v[n] == 3 ? 6 : v[n] == 6 ? 9 : 12);
}
By the way the code functions fine (gets the right answers), but seems to cause memory errors. Thanks for your time!
--Kuroneko