10010 - Where's Waldorf?
Posted: Mon Jul 21, 2003 8:26 pm
Is there something wrong with the judge? This is the second problem in a week that has been spit back Invalid Memory Reference which I can only assume is an out of bounds problem, but I've scoured my code and I can't seem to find anywhere where this is even possible, and EVERYTHING I try when i run the program works beautifully. Here is my code, granted I could probably have written a compact and more efficient program, but I just wrote this out quickly using cut and paste without thinking about it too much. If anybody can point out where an out of bounds or invalid memory reference could possibly happen, please let me know because I am stumped.
[cpp]
#include <iostream>
#include <vector>
#include<string>
using namespace std;
void findit(vector< vector<char> > &grid, string s)
{
int z;
for (int y=0; y<grid.size(); y++)
{
for (int x=0; x<grid[0].size(); x++)
{
if (grid[y][x]!=s[0])
continue;
// forward
if (x<=grid[0].size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y][x+z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// backward
if (x>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y][x-z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// down
if (y<=grid.size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y+z][x]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// up
if (y>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y-z][x]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal down to right
if (x<=grid[0].size()-s.length() && y<=grid.size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y+z][x+z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal down to left
if (x>=s.length()-1 && y<=grid.size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y+z][x-z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal up to left
if (x>=s.length()-1 && y>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y-z][x-z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal up to right
if (x<=grid[0].size()-s.length() && y>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y-z][x+z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
}
}
}
void allLower(string &s)
{
for (int x=0; x<s.length(); x++)
s[x]=tolower(s[x]);
}
int main()
{
vector<int> vec(8,0)
cout<<vec[10];
int x,y,z;
string buff;
int cases;
cin>>cases;
getline(cin,buff);
cin.ignore(255,'\n');
for (x=0; x<cases; x++)
{
int height, width;
cin>>height>>width;
vector<char> vec;
vector< vector<char> > grid;
for (y=0; y<height; y++)
{
vec.clear();
for (z=0; z<width; z++)
{
char c;
cin>>c;
vec.push_back(tolower(c));
}
grid.push_back(vec);
}
int num;
cin>>num;
for (y=0; y<num; y++)
{
string word;
cin>>word;
allLower(word);
findit(grid,word);
}
if (x!=cases-1)
{
cout<<endl;
getline(cin,buff);
cin.ignore(255,'\n');
}
}
return 0;
}
[/cpp]
[cpp]
#include <iostream>
#include <vector>
#include<string>
using namespace std;
void findit(vector< vector<char> > &grid, string s)
{
int z;
for (int y=0; y<grid.size(); y++)
{
for (int x=0; x<grid[0].size(); x++)
{
if (grid[y][x]!=s[0])
continue;
// forward
if (x<=grid[0].size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y][x+z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// backward
if (x>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y][x-z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// down
if (y<=grid.size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y+z][x]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// up
if (y>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y-z][x]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal down to right
if (x<=grid[0].size()-s.length() && y<=grid.size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y+z][x+z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal down to left
if (x>=s.length()-1 && y<=grid.size()-s.length())
{
for (z=0; z<s.length(); z++)
{
if (grid[y+z][x-z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal up to left
if (x>=s.length()-1 && y>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y-z][x-z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
// diagonal up to right
if (x<=grid[0].size()-s.length() && y>=s.length()-1)
{
for (z=0; z<s.length(); z++)
{
if (grid[y-z][x+z]!=s[z])
break;
if (z==s.length()-1)
{
cout<<y+1<<" "<<x+1<<endl; return;
}
}
}
}
}
}
void allLower(string &s)
{
for (int x=0; x<s.length(); x++)
s[x]=tolower(s[x]);
}
int main()
{
vector<int> vec(8,0)
cout<<vec[10];
int x,y,z;
string buff;
int cases;
cin>>cases;
getline(cin,buff);
cin.ignore(255,'\n');
for (x=0; x<cases; x++)
{
int height, width;
cin>>height>>width;
vector<char> vec;
vector< vector<char> > grid;
for (y=0; y<height; y++)
{
vec.clear();
for (z=0; z<width; z++)
{
char c;
cin>>c;
vec.push_back(tolower(c));
}
grid.push_back(vec);
}
int num;
cin>>num;
for (y=0; y<num; y++)
{
string word;
cin>>word;
allLower(word);
findit(grid,word);
}
if (x!=cases-1)
{
cout<<endl;
getline(cin,buff);
cin.ignore(255,'\n');
}
}
return 0;
}
[/cpp]