Reputation: 29
I tried to do a little something today and i got a very weird behaviour from my code. When i leave #include in the code the result differs from when i make it a comment. Maybe somebody could take a look and tell me why, i would appreciate.
Behaviour mentioned above works in certain data sets. Here is the code and my data set.
26 13
X X
XXXXXXX X X
X X X
X X X X
X X RX X
X XXXXXXX X
X X
X XX XXXXX X
XXXX XX X X
X X X
X X X X
X X X X
X XXXXXXX X
X X
X X XXXXXX X
XXX XXX X X
X X X
X X X X
X X X X
X XXXXXXX X
X X
X X XXX XX X
X X XX XXXX X
X X XX X X
X XX X
J XXX XXXX
#include <cstdio>
#include <iostream>
#define MAX 101
using namespace std;
struct leeR
{
int x, y;
}c[MAX*MAX], ir, ij;
int R[MAX][MAX], J[MAX][MAX];
int n, m, dx[] = { 0, 1, 1, 1, 0, -1, -1, -1 }, dy[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
void read()
{
freopen ( "rj.in", "r", stdin );
scanf ( "%d%d%c", &n, &m, &R[1][1] );
char w;
for ( int i = 1; i <= n; ++i )
for ( int j = 1; j <= m + 1; ++j )
{
scanf ( "%c", &w );
if ( w == 'R' )
ir.x = i, ir.y = j;
else if ( w == 'J' )
ij.x = i, ij.y = j;
else if ( w == 'X' )
R[i][j] = -1;
J[i][j] = R[i][j];
}
fclose(stdin);
}
void dump()
{
for ( int i = 1; i <= n; ++i )
{
for ( int j = 1; j <= m; ++j )
printf ( "%d ", R[i][j] );
printf ( "\n\n" );
}
printf ( "\n\n" );
for ( int i = 1; i <= n; ++i )
{
for ( int j = 1; j <= m; ++j )
printf ( "%d ", J[i][j] );
printf ( "\n\n" );
}
}
void leeR()
{
int p(0), f(1);
c[1] = ir;
R[ir.x][ir.y] = 1;
do
{
++p;
for ( int i = 0; i <= 7; ++i )
if ( c[p].x + dx[i] > 0 && c[p].y + dy[i] > 0 && !R[c[p].x + dx[i]][c[p].y + dy[i]] )
R[ c[p].x + dx[i] ][ c[p].y + dy[i] ] = R[c[p].x][c[p].y] + 1,
c[++f].x = c[p].x + dx[i],
c[f].y = c[p].y + dy[i];
if ( p == f )
break;
}while ( f != p + 1 );
}
void leeJ()
{
int p(0), f(1);
c[1] = ij;
J[ij.x][ij.y] = 1;
do
{
++p;
for ( int i = 0; i <= 7; ++i )
if ( c[p].x + dx[i] <= n && c[p].y + dy[i] <= m && c[p].x + dx[i] > 0 && c[p].y + dy[i] > 0 && !J[c[p].x + dx[i]][c[p].y + dy[i]] )
J[ c[p].x + dx[i] ][ c[p].y + dy[i] ] = J[c[p].x][c[p].y] + 1,
c[++f].x = c[p].x + dx[i],
c[f].y = c[p].y + dy[i];
if ( p == f )
break;
}while ( f != p + 1 );
}
void check()
{
freopen ( "rj.out", "w", stdout );
int X, Y, M(200000);
for ( int i = 1; i <= n; ++i )
for ( int j = 1; j <= m; ++j )
if ( R[i][j] == J[i][j] && R[i][j] < M && R[i][j] > 0 )
M = R[i][j], X = i, Y = j;
printf ( "%d %d %d\n", X, Y, M );
fclose(stdout);
}
int main()
{
read();
leeR();
leeJ();
check();
//dump();
return 0;
}
Upvotes: 0
Views: 80
Reputation: 4439
I suggest you try:
g++ test.cpp -fsanitize=address -g -O0 -std=c++11
This allows you to receive an error when "something bad" happens.
Then, load the program up in GDB:
gdb ./a.out
break __asan_report_error
run
backtrace
At this point, I see that on (or about) line 62, you have a buffer overflow:
if ( c[p].x + dx[i] > 0 && c[p].y + dy[i] > 0 && !R[c[p].x + dx[i]][c[p].y + dy[i]] )
At this point, you can do info locals
. For me, it prints:
(gdb) info locals
i = 3
p = 7937
f = 8119
Now, I'm not about to debug this for you but this should be enough for you to figure out where your logic is going wrong.
Upvotes: 1