10267 - Graphical Editor
Moderator: Board moderators
Try this:
The output should be:
Code: Select all
I 3 3
U F 1 1 P
S test.bmp
X
Code: Select all
OOO
OOO
OOO
10267 Invalid memory reference--headache!!
Dear all,
Pls help to spot the error which cause "invalid memory reference", I have tried everything I can, scratching my head off now....
sry for the length but for debugging....sigh....
[cpp]
#include <iostream.h>
void clearTable (char box[250][250], int x, int y);
void fillRegion (char box[250][250], int x, int y, char colour1, char colour2);
bool checkSize ( int n );
int main(){
int x, y, tempX, tempY, sizeX, sizeY, temp4swap;
char command, box[250][250], filename[1000], colour;
while ( true ){
cin>>command;
if ( command=='X' ) break;
if ( command=='I' ) {
cin>>sizeX>>sizeY;
if ( checkSize(sizeX) && checkSize(sizeY) )
clearTable( box, sizeX, sizeY );
}
if ( command=='C' ) clearTable( box, sizeX, sizeY );
if ( command=='S' ){
cin>>filename;
cout<<filename<<endl;
for (int j=0; j<sizeY; j++){
for (int i=0; i<sizeX; i++)
cout<<box[j];
cout<<endl;
}
}
if ( command=='L' ){
cin>>x>>y>>colour;
if ( !checkSize(x) || !checkSize(y) )
continue;
box[x-1][y-1] = colour;
}
if ( command=='V' ){
cin>>x>>y>>tempY>>colour;
if ( y > tempY ){
temp4swap = y;
y = tempY;
tempY = temp4swap;
}
if ( !checkSize(x) || !checkSize(y) || !checkSize(tempY) )
continue;
for ( int i=y-1; i<tempY; i++ )
box[x-1] = colour;
}
if ( command=='H' ){
cin>>x>>tempX>>y>>colour;
if ( x > tempX ){
temp4swap = x;
x = tempX;
tempX = temp4swap;
}
if ( !checkSize(x) || !checkSize(tempX) || !checkSize(y) )
continue;
for ( int i=x-1; i<tempX; i++ )
box[y-1] = colour;
}
if ( command=='K' ){
cin>>x>>y>>tempX>>tempY>>colour;
if ( !checkSize(x) || !checkSize(y) || !checkSize(tempX) || !checkSize(tempY) )
continue;
for ( int i=x-1; i<tempX; i++ )
for ( int j=y-1; j<tempY; j++ )
box[j] = colour;
}
if ( command=='F' ){
cin>>x>>y>>colour;
if ( !checkSize(x) || !checkSize(y) )
continue;
fillRegion( box, x-1, y-1, box[x-1][y-1], colour );
}
}
return 0;
}
void clearTable (char box[250][250], int x, int y){
for (int i=0; i<x; i++)
for (int j=0; j<y; j++)
box[j] = 'O';
}
void fillRegion (char box[250][250], int x, int y, char colour1, char colour2){
// DFS Recursive way to change colour
//cout<<"now in "<<x<<" "<<y<<endl;
box[x][y] = colour2;
if ( x>0 )
if ( box[x-1][y]==colour1 )
fillRegion( box, x-1, y, colour1, colour2 );
if ( x<249 )
if ( box[x+1][y] == colour1 )
fillRegion( box, x+1, y, colour1, colour2 );
if ( y>0 )
if ( box[x][y-1]==colour1 )
fillRegion( box, x, y-1, colour1, colour2 );
if ( y<249 )
if ( box[x][y+1] == colour1 )
fillRegion( box, x, y+1, colour1, colour2 );
}
bool checkSize ( int n ){
if ( n>250 || n<1 )
return false;
else
return true;
}[/cpp]
Pls help to spot the error which cause "invalid memory reference", I have tried everything I can, scratching my head off now....

[cpp]
#include <iostream.h>
void clearTable (char box[250][250], int x, int y);
void fillRegion (char box[250][250], int x, int y, char colour1, char colour2);
bool checkSize ( int n );
int main(){
int x, y, tempX, tempY, sizeX, sizeY, temp4swap;
char command, box[250][250], filename[1000], colour;
while ( true ){
cin>>command;
if ( command=='X' ) break;
if ( command=='I' ) {
cin>>sizeX>>sizeY;
if ( checkSize(sizeX) && checkSize(sizeY) )
clearTable( box, sizeX, sizeY );
}
if ( command=='C' ) clearTable( box, sizeX, sizeY );
if ( command=='S' ){
cin>>filename;
cout<<filename<<endl;
for (int j=0; j<sizeY; j++){
for (int i=0; i<sizeX; i++)
cout<<box[j];
cout<<endl;
}
}
if ( command=='L' ){
cin>>x>>y>>colour;
if ( !checkSize(x) || !checkSize(y) )
continue;
box[x-1][y-1] = colour;
}
if ( command=='V' ){
cin>>x>>y>>tempY>>colour;
if ( y > tempY ){
temp4swap = y;
y = tempY;
tempY = temp4swap;
}
if ( !checkSize(x) || !checkSize(y) || !checkSize(tempY) )
continue;
for ( int i=y-1; i<tempY; i++ )
box[x-1] = colour;
}
if ( command=='H' ){
cin>>x>>tempX>>y>>colour;
if ( x > tempX ){
temp4swap = x;
x = tempX;
tempX = temp4swap;
}
if ( !checkSize(x) || !checkSize(tempX) || !checkSize(y) )
continue;
for ( int i=x-1; i<tempX; i++ )
box[y-1] = colour;
}
if ( command=='K' ){
cin>>x>>y>>tempX>>tempY>>colour;
if ( !checkSize(x) || !checkSize(y) || !checkSize(tempX) || !checkSize(tempY) )
continue;
for ( int i=x-1; i<tempX; i++ )
for ( int j=y-1; j<tempY; j++ )
box[j] = colour;
}
if ( command=='F' ){
cin>>x>>y>>colour;
if ( !checkSize(x) || !checkSize(y) )
continue;
fillRegion( box, x-1, y-1, box[x-1][y-1], colour );
}
}
return 0;
}
void clearTable (char box[250][250], int x, int y){
for (int i=0; i<x; i++)
for (int j=0; j<y; j++)
box[j] = 'O';
}
void fillRegion (char box[250][250], int x, int y, char colour1, char colour2){
// DFS Recursive way to change colour
//cout<<"now in "<<x<<" "<<y<<endl;
box[x][y] = colour2;
if ( x>0 )
if ( box[x-1][y]==colour1 )
fillRegion( box, x-1, y, colour1, colour2 );
if ( x<249 )
if ( box[x+1][y] == colour1 )
fillRegion( box, x+1, y, colour1, colour2 );
if ( y>0 )
if ( box[x][y-1]==colour1 )
fillRegion( box, x, y-1, colour1, colour2 );
if ( y<249 )
if ( box[x][y+1] == colour1 )
fillRegion( box, x, y+1, colour1, colour2 );
}
bool checkSize ( int n ){
if ( n>250 || n<1 )
return false;
else
return true;
}[/cpp]
If I understand the problem statement correctly, there can be errors
in the commands given, such as for example the user entering the
'C' command before the 'I' command. In your code, sizex and sizey
would not yet be set if the first command was 'C', and thus could be
some arbitrary value. Thus when you go through the box array in your
clearTable routine, you may be accessing very large indices in the box
array and that may cause an invalid memory reference.
This is just my guess..but try setting sizex=sizey=0 before the
while loop starts and see if that helps.
There may be some other problems, but that was just the first
thing that caught my attention.
in the commands given, such as for example the user entering the
'C' command before the 'I' command. In your code, sizex and sizey
would not yet be set if the first command was 'C', and thus could be
some arbitrary value. Thus when you go through the box array in your
clearTable routine, you may be accessing very large indices in the box
array and that may cause an invalid memory reference.
This is just my guess..but try setting sizex=sizey=0 before the
while loop starts and see if that helps.
There may be some other problems, but that was just the first
thing that caught my attention.
I have tried initiating all values to 0 at the beginning, however the problem still exist.
Another interesting thing I find out abt this code:
when I tried the following commands:
I 150 150
K 1 75 150 150 B
F 1 1 J
it crashes somewhere, but then when I tried another set of commands:
I 150 150
K 1 60 150 150 B
F 1 1 J
(it works fine!!)
K 1 1 75 75 O
F 1 1 J
( more amazingly, it works fine now!! same space to fill as last set of commands)
anyone has any clue?
Another interesting thing I find out abt this code:
when I tried the following commands:
I 150 150
K 1 75 150 150 B
F 1 1 J
it crashes somewhere, but then when I tried another set of commands:
I 150 150
K 1 60 150 150 B
F 1 1 J
(it works fine!!)
K 1 1 75 75 O
F 1 1 J
( more amazingly, it works fine now!! same space to fill as last set of commands)
anyone has any clue?
-
- New poster
- Posts: 28
- Joined: Mon Mar 01, 2004 11:29 pm
10267
Hi,
I tried to solve the 10267 but I got wrong, I tested with all cases that have in board and OK.
The read of input that be special?
This is my code
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 300
int m, n;
char matriz[MAX][MAX];
int max(int a, int b) {
if (a > b) return a;
return b;
}
int min(int a, int b) {
if (a < b) return a;
return b;
}
void fImprimir() {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
printf("%c", matriz[j]);
}
printf("\n");
}
}
void fC() {
int i, j;
for (i = 1; i < MAX; i++)
for (j = 1; j <= MAX; j++)
matriz[j] = 'O';
}
void fL(int x, int y, char c) {
matriz[y][x] = c;
}
void fV(int x, int y1, int y2, char c) {
int i, min_y, max_y;
min_y = min(y1, y2);
max_y = max(y1, y2);
for (i = min_y; i <= max_y && i <= n; i++)
matriz[x] = c;
}
void fH(int x1, int x2, int y, char c) {
int i, min_x, max_x;
min_x = min(x1, x2);
max_x = max(x1, x2);
for (i = min_x; i <= max_x && i <= m; i++)
matriz[y] = c;
}
void fK(int x1, int y1, int x2, int y2, char c) {
int i, j;
for (i = y1; i <= y2 && i <= n; i++) {
for (j = x1; j <= x2 && j <= m; j++) {
matriz[j] = c;
}
}
}
void fF(int x, int y, char c, char f) {
if (x < 1 || x >= m+1) return;
if (y < 1 || y >= n+1) return;
if (matriz[y][x] != f || matriz[y][x] == c) {
return;
}
matriz[y][x] = c;
fF(x, y+1, c, f);
fF(x, y-1, c, f);
fF(x+1, y, c, f);
fF(x-1, y, c, f);
/*fF(x+1, y+1, c, f);
fF(x+1, y-1, c, f);
fF(x-1, y+1, c, f);
fF(x-1, y-1, c, f);*/
}
int main() {
#ifndef ONLINE_JUDGE
freopen("10267.in", "r", stdin);
freopen("10267.out", "w", stdout);
#endif
char ca, c;
char line[256], filename[20];
int x1, x2, y1, y2, x, y;
while (true) {
//gets(line);
scanf(" %c ", &ca);
//printf("%c\n", ca);
if (ca == 'I') {
scanf(" %d %d", &m, &n);
//printf("I: %d %d\n", m, n);
fC();
}
else if (ca == 'C') {
fC();
}
else if (ca == 'L') {
scanf(" %d %d %c", &x, &y, &c);
//printf("%d %d %c", x, y, c);
fL(x, y, c);
//fImprimir();
}
else if (ca == 'V') {
scanf(" %d %d %d %c", &x, &y1, &y2, &c);
//printf("%d %d %d %c", x, y1, y2, c);
fV(x, y1, y2, c);
}
else if (ca == 'H') {
scanf(" %d %d %d %c", &x1, &x2, &y, &c);
//printf("%d %d %d %c", x1, x2, y, c);
fH(x1, x2, y, c);
}
else if (ca == 'K') {
scanf(" %d %d %d %d %c", &x1, &y1, &x2, &y2, &c);
//printf("%d %d %d %d %c", x1, y1, x2, y2, c);
fK(x1, y1, x2, y2, c);
}
else if (ca == 'F') {
scanf(" %d %d %c", &x, &y, &c);
//printf("%d %d %c", x, y, c);
if (matriz[x][y] != c) {
fF(x, y, c, matriz[x][y]);
}
}
else if (ca == 'S') {
//scanf("%s", &filename);
gets(filename);
printf("%s\n", filename);
fImprimir();
}
else if (ca == 'X') {
return 0;
}
else {
gets(line);
/* comando desconhecido */
}
}
return 0;
}
[/cpp]
Thanks for all
Wanderley
I tried to solve the 10267 but I got wrong, I tested with all cases that have in board and OK.
The read of input that be special?
This is my code

[cpp]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 300
int m, n;
char matriz[MAX][MAX];
int max(int a, int b) {
if (a > b) return a;
return b;
}
int min(int a, int b) {
if (a < b) return a;
return b;
}
void fImprimir() {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
printf("%c", matriz[j]);
}
printf("\n");
}
}
void fC() {
int i, j;
for (i = 1; i < MAX; i++)
for (j = 1; j <= MAX; j++)
matriz[j] = 'O';
}
void fL(int x, int y, char c) {
matriz[y][x] = c;
}
void fV(int x, int y1, int y2, char c) {
int i, min_y, max_y;
min_y = min(y1, y2);
max_y = max(y1, y2);
for (i = min_y; i <= max_y && i <= n; i++)
matriz[x] = c;
}
void fH(int x1, int x2, int y, char c) {
int i, min_x, max_x;
min_x = min(x1, x2);
max_x = max(x1, x2);
for (i = min_x; i <= max_x && i <= m; i++)
matriz[y] = c;
}
void fK(int x1, int y1, int x2, int y2, char c) {
int i, j;
for (i = y1; i <= y2 && i <= n; i++) {
for (j = x1; j <= x2 && j <= m; j++) {
matriz[j] = c;
}
}
}
void fF(int x, int y, char c, char f) {
if (x < 1 || x >= m+1) return;
if (y < 1 || y >= n+1) return;
if (matriz[y][x] != f || matriz[y][x] == c) {
return;
}
matriz[y][x] = c;
fF(x, y+1, c, f);
fF(x, y-1, c, f);
fF(x+1, y, c, f);
fF(x-1, y, c, f);
/*fF(x+1, y+1, c, f);
fF(x+1, y-1, c, f);
fF(x-1, y+1, c, f);
fF(x-1, y-1, c, f);*/
}
int main() {
#ifndef ONLINE_JUDGE
freopen("10267.in", "r", stdin);
freopen("10267.out", "w", stdout);
#endif
char ca, c;
char line[256], filename[20];
int x1, x2, y1, y2, x, y;
while (true) {
//gets(line);
scanf(" %c ", &ca);
//printf("%c\n", ca);
if (ca == 'I') {
scanf(" %d %d", &m, &n);
//printf("I: %d %d\n", m, n);
fC();
}
else if (ca == 'C') {
fC();
}
else if (ca == 'L') {
scanf(" %d %d %c", &x, &y, &c);
//printf("%d %d %c", x, y, c);
fL(x, y, c);
//fImprimir();
}
else if (ca == 'V') {
scanf(" %d %d %d %c", &x, &y1, &y2, &c);
//printf("%d %d %d %c", x, y1, y2, c);
fV(x, y1, y2, c);
}
else if (ca == 'H') {
scanf(" %d %d %d %c", &x1, &x2, &y, &c);
//printf("%d %d %d %c", x1, x2, y, c);
fH(x1, x2, y, c);
}
else if (ca == 'K') {
scanf(" %d %d %d %d %c", &x1, &y1, &x2, &y2, &c);
//printf("%d %d %d %d %c", x1, y1, x2, y2, c);
fK(x1, y1, x2, y2, c);
}
else if (ca == 'F') {
scanf(" %d %d %c", &x, &y, &c);
//printf("%d %d %c", x, y, c);
if (matriz[x][y] != c) {
fF(x, y, c, matriz[x][y]);
}
}
else if (ca == 'S') {
//scanf("%s", &filename);
gets(filename);
printf("%s\n", filename);
fImprimir();
}
else if (ca == 'X') {
return 0;
}
else {
gets(line);
/* comando desconhecido */
}
}
return 0;
}
[/cpp]
Thanks for all
Wanderley
-
- New poster
- Posts: 18
- Joined: Fri Oct 10, 2003 8:46 am
- Location: Airway Heights
-
- Experienced poster
- Posts: 183
- Joined: Thu Nov 11, 2004 12:35 pm
- Location: AIUB, Bangladesh

can u tell me something....suppose I somehow know if the line has valid command or not.....can I use sscanf the to get the value from the buffer array or there i have to check for more validity?
I mean:
A B C D
if A is a valid command then will B, C,D all r seperated by only space or I have to check it manualy?
Jalal : AIUB SPARKS
-
- Experienced poster
- Posts: 183
- Joined: Thu Nov 11, 2004 12:35 pm
- Location: AIUB, Bangladesh

can u tell me something....suppose I somehow know if the line has valid command or not.....can I use sscanf then to get the values from the buffer array or there i have to check for more validity?
I mean:
A B C D
if A is a valid command (not necessarily at buffer[0]) then will B, C,D all r seperated by only space or I have to check it manualy?
anyone who got Acc can help with some I/Os I think....
Jalal : AIUB SPARKS
-
- Experienced poster
- Posts: 183
- Joined: Thu Nov 11, 2004 12:35 pm
- Location: AIUB, Bangladesh


can u tell me something....suppose I somehow know if the line has valid command or not.....can I use sscanf then to get the values from the buffer array or there i have to check for more validity?
I mean:
A B C D
if A is a valid command (not necessarily at buffer[0]) then will B, C,D all r seperated by only space or I have to check it manualy?
anyone who got Acc can help with some I/Os I think....
Jalal : AIUB SPARKS
What is wrong with my code? i keep getting WA...
[c]#include <stdio.h>
#include <string.h>
int i, j, m, n, x, x1, x2, y, y1, y2;
char c, com, dummy, name[13], pic[250][250];
void ffill (int x, int y, char c, char o){
if (c==o){
return;
}
if (pic[y][x]==o){
pic[y][x]=c;
if (y!=0){
ffill(x, y-1, c, o);
}
if (x!=0){
ffill(x-1, y, c, o);
}
if (x!=m){
ffill(x+1, y, c, o);
}
if (y!=n){
ffill(x, y+1, c, o);
}
}
}
int main(){
while(1){
scanf("%c", &com);
if (com=='X'){
break;
}
switch(com){
case 'I':
scanf("%d %d", &m, &n);
case 'C':
memset(pic, 'O', sizeof(pic));
break;
case 'L':
scanf("%d %d %c", &x, &y, &c);
pic[y-1][x-1]=c;
break;
case 'V':
scanf("%d %d %d %c", &x, &y1, &y2, &c);
if (y1>y2){
y1^=y2;
y2^=y1;
y1^=y2;
}
for (i=y1; i<=y2; i++){
pic[i-1][x-1]=c;
}
break;
case 'H':
scanf("%d %d %d %c", &x1, &x2, &y, &c);
if (x1>x2){
x1^=x2;
x2^=x1;
x1^=x2;
}
for (i=x1; i<=x2; i++){
pic[y-1][i-1]=c;
}
break;
case 'K':
scanf("%d %d %d %d %c", &x1, &y1, &x2, &y2, &c);
if (x1>x2){
x1^=x2;
x2^=x1;
x1^=x2;
}
if (y1>y2){
y1^=y2;
y2^=y1;
y1^=y2;
}
for (i=x1; i<=x2; i++){
for (j=y1; j<=y2; j++){
pic[j-1][i-1]=c;
}
}
break;
case 'F':
scanf("%d %d %c", &x, &y, &c);
ffill(x, y, c, pic[y-1][x-1]);
break;
case 'S':
scanf("%s", name);
printf("%s\n", name);
for (j=0; j<n; j++){
for (i=0; i<m; i++){
printf("%c", pic[j]);
}
printf("\n");
}
break;
case '\n':
break;
default:
while(1){
dummy=getchar();
if (dummy=='\n'){
break;
}
}
}
}
return 0;
}[/c]
[c]#include <stdio.h>
#include <string.h>
int i, j, m, n, x, x1, x2, y, y1, y2;
char c, com, dummy, name[13], pic[250][250];
void ffill (int x, int y, char c, char o){
if (c==o){
return;
}
if (pic[y][x]==o){
pic[y][x]=c;
if (y!=0){
ffill(x, y-1, c, o);
}
if (x!=0){
ffill(x-1, y, c, o);
}
if (x!=m){
ffill(x+1, y, c, o);
}
if (y!=n){
ffill(x, y+1, c, o);
}
}
}
int main(){
while(1){
scanf("%c", &com);
if (com=='X'){
break;
}
switch(com){
case 'I':
scanf("%d %d", &m, &n);
case 'C':
memset(pic, 'O', sizeof(pic));
break;
case 'L':
scanf("%d %d %c", &x, &y, &c);
pic[y-1][x-1]=c;
break;
case 'V':
scanf("%d %d %d %c", &x, &y1, &y2, &c);
if (y1>y2){
y1^=y2;
y2^=y1;
y1^=y2;
}
for (i=y1; i<=y2; i++){
pic[i-1][x-1]=c;
}
break;
case 'H':
scanf("%d %d %d %c", &x1, &x2, &y, &c);
if (x1>x2){
x1^=x2;
x2^=x1;
x1^=x2;
}
for (i=x1; i<=x2; i++){
pic[y-1][i-1]=c;
}
break;
case 'K':
scanf("%d %d %d %d %c", &x1, &y1, &x2, &y2, &c);
if (x1>x2){
x1^=x2;
x2^=x1;
x1^=x2;
}
if (y1>y2){
y1^=y2;
y2^=y1;
y1^=y2;
}
for (i=x1; i<=x2; i++){
for (j=y1; j<=y2; j++){
pic[j-1][i-1]=c;
}
}
break;
case 'F':
scanf("%d %d %c", &x, &y, &c);
ffill(x, y, c, pic[y-1][x-1]);
break;
case 'S':
scanf("%s", name);
printf("%s\n", name);
for (j=0; j<n; j++){
for (i=0; i<m; i++){
printf("%c", pic[j]);
}
printf("\n");
}
break;
case '\n':
break;
default:
while(1){
dummy=getchar();
if (dummy=='\n'){
break;
}
}
}
}
return 0;
}[/c]