10267 - Graphical Editor

All about problems in Volume 102. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

alirezanoori
New poster
Posts: 26
Joined: Fri Jan 02, 2009 12:41 am

10267 - Graphical Editor

Post by alirezanoori » Fri Jan 09, 2009 9:28 pm

WA! WA! WA!
I'm gonna kill myself! OMG! Please take a look at my program. I can't find ANYTHING wrong with it.
-I use iterative and VERY FAST method for flood fill.
-I tested my program with HUGE inputs and it worked excellent.
-I tested it with any kind of test data.
-I used my debug skills.
So it leaves me with this idea that I've done something silly again! :( I do this a lot and I hate myself for this. I wrote lots of programs on lots of online judges and every time I get WA I realize that something (for example a blank line) killed my code!!!! :((
So here's my code.
Thank you guys for helping me out.

Code: Select all

#include <iostream>
#include <cmath>
#include <memory>
#include <queue>
using std::cout;
using std::endl;
using std::cin;
using std::queue;

char image[252][252];
int n, m;
char name[12];

enum Shape {HLINE, VLINE, RECT, REG};

#define ISGOOD(i,j) ((i > 0 && i <= m) && (j > 0 && j <= n) ? true : false)

void floodFillScanlineStack(int x, int y, char newColor, char oldColor)
{
    if(oldColor == newColor) return;
    queue< std::pair<int, int> > q;
    
    int y1; 
    bool spanLeft, spanRight;

    std::pair<int, int> p;
    p.first = x;
    p.second = y;

    q.push(p);
    
    while(!q.empty())
    {
        p = q.front();
        x = p.first;
        y = p.second;
        q.pop();
        y1 = y;
        while(y1 >= 0 && image[x][y1] == oldColor) y1--;
        y1++;
        spanLeft = spanRight = false;
        while(y1 <= n && image[x][y1] == oldColor )
        {
            image[x][y1] = newColor;
            if(!spanLeft && x > 0 && image[x - 1][y1] == oldColor) 
            {
                p.first = x - 1;
                p.second = y1;
                q.push(p);
                spanLeft = true;
            }
            else if(spanLeft && x > 0 && image[x - 1][y1] != oldColor)
            {
                spanLeft = false;
            }
            if(!spanRight && x <= m - 1 && image[x + 1][y1] == oldColor) 
            {
                p.first = x + 1;
                p.second = y1;
                q.push(p);
                spanRight = true;
            }
            else if(spanRight && x <= m - 1 && image[x + 1][y1] != oldColor)
            {
                spanRight = false;
            } 
            y1++;
        }
    }
}

void AddShape(int x1, int y1, int x2, int y2, char color, Shape sh)
{
    int i, j;
    char col;
    
    switch(sh)
    {
    case VLINE:
        if((y1 > y2) && (y1 != -1) && (y2 != -1))
            std::swap(y1, y2);
        for(i = y1; i <= y2; i++)
            image[i][x1] = color;
        break;
    case HLINE:
        if((x1 > x2) && (x1 != -1) && (x2 != -1))
            std::swap(x1, x2);
        for(i = x1; i <= x2; i++)
            image[y1][i] = color;
        break;
    case RECT:
        for(i = x1; i <= x2; i++)
        {
            for(j = y1; j <= y2; j++)
                image[j][i] = color;
        }
        break;
    case REG:
        col = image[y1][x1];
        floodFillScanlineStack(y1, x1, color, col);
        break;
    }
}

void printImage()
{
    cout << name << endl;
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
            cout << image[i][j];
        cout << endl;
    }
}

void IgnoreLine()
{
    while(cin.get() != '\n');
}

int main()
{
    char command, c;
    int x1, x2, y1, y2;
    while(cin >> command)
    {
        if(command == 'X')
            break;

        switch(command)
        {
        case 'I': memset(image, 'O', sizeof image); cin >> n >> m; break;
        case 'C': memset(image, 'O', sizeof image); break;
        case 'L': cin >> x1 >> y1 >> c; image[y1][x1] = c; break;
        case 'V': cin >> x1 >> y1 >> y2 >> c; AddShape(x1, y1, -1, y2, c, VLINE); break;
        case 'H': cin >> x1 >> x2 >> y1 >> c; AddShape(x1, y1, x2, -1, c, HLINE); break;
        case 'K': cin >> x1 >> y1 >> x2 >> y2 >> c; AddShape(x1, y1, x2, y2, c, RECT); break;
        case 'F': cin >> x1 >> y1 >> c; AddShape(x1, y1, -1, -1, c, REG); break;
        case 'S': cin >> name; printImage(); break;
        default: IgnoreLine(); break;
        }

        while(iswspace(cin.peek()))
            cin.ignore();
    }
    return 0;
}

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 10267 - Graphical Editor

Post by vahid sanei » Tue Feb 17, 2009 11:50 am

Code: Select all

REMOVED
Last edited by vahid sanei on Mon Mar 16, 2009 6:55 am, edited 1 time in total.
Impossible says I`m possible

gaby
New poster
Posts: 1
Joined: Sun Mar 15, 2009 9:17 am

Re: 10267 - Graphical Editor

Post by gaby » Sun Mar 15, 2009 9:26 am

AC after 10 WA :D

As to this problem, there are really something that we may ignored. As to me, I ignored that in the parameters of H and V command, x1 <= x2 and y <= y2 is not always satisfy. so we should exchange them if they do not satisfy the relation.


If the following code would do some help for you, I will be very glad, maybe you also ignored some details. :D
/*
* Author: Gaby
* Lang: C
*/

#include <stdio.h>

typedef struct{
char info[251][251];
int w; /* width */
int h; /* height */
}Img;

Img img;

void cmdI(int m, int n){
int i, j;

img.w = m; img.h = n;

for (i = 0; i < n; i++){
for (j = 0; j < m; j++){
img.info[j] = 'O';
}
}
}/* cmdI */

void cmdC(){
int i, j;

for (i = 0; i < img.h; i++){
for (j = 0; j < img.w; j++){
img.info[j] = 'O';
}
}
}/* cmdC */

void cmdL(int x, int y, char c){
img.info[y-1][x-1] = c;
}/* cmdL */

void cmdV(int x, int y1, int y2, char c){
int i;
int temp;

if (y1 > y2){
temp = y1; y1 = y2; y2 = temp;
}

for (i = y1-1; i < y2; i++){
img.info[x-1] = c;
}
}/* cmdV */

void cmdH(int x1, int x2, int y, char c){
int i;
int temp;

if (x1 > x2){
temp = x1; x1 = x2; x2 = temp;
}

for (i = x1-1; i < x2; i++){
img.info[y-1] = c;
}
}/* cmdH */

void cmdK(int x1, int y1, int x2, int y2, char c){
int i, j;

for (i = y1-1; i < y2; i++){
for (j = x1-1; j < x2; j++){
img.info[j] = c;
}
}
}/* cmdK */

int dir[4][2] = {{-1, 0},{1, 0},{0, 1},{0, -1}};

void cmdF(int x, int y, char c){
char ch = img.info[y-1][x-1];
int i;

if (ch == c){
return;
}

img.info[y-1][x-1] = c;

for (i = 0; i < 4; i++){
if (x+dir[0]>0 && x+dir[0]<=img.w && y+dir[1]>0 && y+dir[1]<=img.h && img.info[y+dir[1]-1][x+dir[i][0]-1] == ch){
cmdF(x+dir[i][0], y+dir[i][1], c);
}
}
}/* cmdF */

void cmdS(char *s){
int i, j;

printf("%s\n", s);

for (i = 0; i < img.h; i++){
for (j = 0; j < img.w; j++){
printf("%c", img.info[i][j]);
}

printf("\n");
}
}/* cmdS */

int main(){
char cmd, c;
int x, y, x1, y1, x2, y2;
int m, n;
char szName[105];

while (1){
scanf(" %c", &cmd);

switch (cmd){
case 'X': return 0;

case 'I': scanf("%d%d", &m, &n);
cmdI(m, n);
break;

case 'C': cmdC();
break;

case 'L': scanf("%d%d %c", &x, &y, &c);
cmdL(x, y, c);
break;

case 'V': scanf("%d%d%d %c", &x, &y1, &y2, &c);
cmdV(x, y1, y2, c);
break;

case 'H': scanf("%d%d%d %c", &x1, &x2, &y, &c);
cmdH(x1, x2, y, c);
break;

case 'K': scanf("%d%d%d%d %c", &x1, &y1, &x2, &y2, &c);
cmdK(x1, y1, x2, y2, c);
break;

case 'F': scanf("%d%d %c", &x, &y, &c);
cmdF(x, y, c);
break;

case 'S': scanf(" %[^\n]", szName);
cmdS(szName);
break;

default: scanf("%*[^\n]");
}
}

return 0;
}
Last edited by gaby on Sun Mar 15, 2009 9:30 am, edited 1 time in total.

yufu
New poster
Posts: 1
Joined: Sat Mar 14, 2009 8:30 pm

Re: 10267 - Graphical Editor

Post by yufu » Tue Mar 17, 2009 7:44 am

I hope someone could help me out. I have worked on this problem for amazingly long time. Pardon me to post my code. I thought I have noticed everything other people have mentioned.

Code: Select all

#include <stdio.h>
#include <stdlib.h>

void create_table(char *** grids, char * param) {
    int m,n,i,j;
    if (*grids) {
        m = (*grids)[0][0];
        n = (*grids)[0][1];
        for (i = 0; i < n+1; i++)
            free((*grids)[i]);
        free((*grids));
    }
    sscanf(param, "%d %d", &m, &n);
    (*grids) = (char**)malloc((n+1) * sizeof(char*));
    for (i = 1; i < n+1; i++) {
        (*grids)[i] = (char*)malloc((m+1) * sizeof(char));
        for (j = 1; j < m+1; j++) {
            (*grids)[i][j] = 'O';
        }
    }
    (*grids)[0] = (char*)malloc(2 * sizeof(char));
    (*grids)[0][0] = m;
    (*grids)[0][1] = n;
}

void clear(char *** grids) {
    int m, n, i, j;
    m = (*grids)[0][0];
    n = (*grids)[0][1];
    for (i = 1; i < n+1; i++) {
        for (j = 1; j < m+1; j++) {
            (*grids)[i][j] = 'O';
        }
    }
}

void col_pixel(char *** grids, char * param) {
    int i, j;
    char c;
    sscanf(param, "%d %d %c", &i, &j, &c);
    (*grids)[j][i] = c;
}

void draw_vertical(char *** grids, char * param) {
    int x, y1, y2, i, t;
    char c;
    sscanf(param, "%d %d %d %c", &x, &y1, &y2, &c);
    if (y1 > y2) {
        t = y1;
        y1 = y2;
        y2 = t;
    }
    for (i = y1; i <= y2; i++) {
        (*grids)[i][x] = c;
    }
}

void draw_horizontal(char *** grids, char *param) {
    int x1, x2, y, i, t;
    char c;
    sscanf(param, "%d %d %d %c", &x1, &x2, &y, &c);
    if (x1 > x2) {
        t = x1;
        x1 = x2;
        x2 = t;
    }
    for (i = x1; i <= x2; i++) {
        (*grids)[y][i] = c;
    }
}

void draw_rect(char *** grids, char *param) {
    int t, x1, x2, y1, y2, i, j;
    char c;
    sscanf(param, "%d %d %d %d %c", &x1, &y1, &x2, &y2, &c);
    if (x1 > x2) {
        t = x1;
        x1 = x2;
        x2 = t;
    }
    if (y1 > y2) {
        t = y1;
        y1 = y2;
        y2 = t;
    }
    for (i = x1; i <= x2; i++) {
        for (j = y1; j <= y2; j++) {
            (*grids)[j][i] = c;
        }
    }
}

void fill_coo(char *** grids, int x, int y, char c) {
    if ((*grids)[y][x] != c) {
        char t = (*grids)[y][x];
        (*grids)[y][x] = c;
        if (x > 1 && t == (*grids)[y][x-1])
            fill_coo(grids, x-1, y, c);
        if (y > 1 && t == (*grids)[y-1][x]) 
            fill_coo(grids, x, y-1, c);
        if (x < (*grids)[0][0] && t == (*grids)[y][x+1]) 
            fill_coo(grids, x+1, y, c);
        if (y < (*grids)[0][1] && t == (*grids)[y+1][x]) 
            fill_coo(grids, x, y+1, c);
    }
}

void fill (char *** grids, char * param) {
    int x, y;
    char c;
    sscanf(param, "%d %d %c", &x, &y, &c);
    fill_coo(grids, x, y, c);
}

void write_pic(char *** grids, char * param) {
    if ((*grids) == NULL)
        return;
    int m, n, i, j;
    printf("%s\n", param);
    for (i = 1; i < (*grids)[0][1]+1; i++) {
        for (j = 1; j < (*grids)[0][0]+1; j++) {
            putchar((*grids)[i][j]);
        }
        putchar(10);
    }
}

int main () {
    char ** grids = NULL;
    char cmd[2000];
    int m,n,i;
    char c;
    scanf("%[^\n]\n", cmd);
    while (cmd[0] != 'X') {
        switch (cmd[0]) {
            case 'I':
                create_table(&grids, cmd+2);
                break;
            case 'C':
                clear(&grids);
                break;
            case 'L':
                col_pixel(&grids, cmd+2);
                break;
            case 'V':
                draw_vertical(&grids, cmd+2);
                break;
            case 'H':
                draw_horizontal(&grids, cmd+2);
                break;
            case 'K':
                draw_rect(&grids, cmd+2);
                break;
            case 'F':
                fill(&grids, cmd+2);
                break;
            case 'S':
                write_pic(&grids, cmd+2);
                break;
            case 'X':
                break;
            default:
                break;
        }
        scanf("%[^\n]\n", cmd);
    }
    if (grids) {
        m = (grids)[0][0];
        n = (grids)[0][1];
        for (i = 0; i < n+1; i++)
            free((grids)[i]);
        free((grids));
    }


    return 0;
}
Thanks for help

mohitkanwal
New poster
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: 10267 - Graphical Editor

Post by mohitkanwal » Mon Apr 13, 2009 8:50 pm

Hi all .. I have been working on this problem since one week .. I am getting RTE over and over again .. I am fed up .. I tried my code at programming-challenges.com and it gave WA ., I cannot understand where is the error .. for the last six days I have kept debugging my code by using test data from the forum and all advices that were given here .. still it is not helping .. AND I USE A NON RECURSIVE METHOD of filling .. so I shud not be getting RTE but .. as it seems .. the reverse is true ...I have checked for errors and all .. including discarding of commands newline .. still... :cry: hope somebody can at least tell me where are the possible sources of RTEs in my program..
Thanks

Code: Select all

//removed after AC
Last edited by mohitkanwal on Tue Apr 14, 2009 8:58 am, edited 1 time in total.

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 10267 - Graphical Editor

Post by vahid sanei » Tue Apr 14, 2009 8:10 am

bcoz if cnew and old be same , your fill function check a cell over and over
you can use a map or a 2-D array for marking cells that you visited already
and i`m not sure , you can get Acc after meet it
maybe x1 is greater than x2 or y1 > y2 , you should swap them
Impossible says I`m possible

mohitkanwal
New poster
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: 10267 - Graphical Editor

Post by mohitkanwal » Tue Apr 14, 2009 8:57 am

yup .. that was the only freaking error .. so stupid I cud not see the error :D ha ha got ACC.. time 0.060 secs....Thanks

zzz_hku
New poster
Posts: 1
Joined: Tue Apr 14, 2009 8:09 pm

Re: 10267 - Graphical Editor

Post by zzz_hku » Tue Apr 14, 2009 8:14 pm

I'm new here. I kept getting "Runtime Error" all the time and I don't know why.
Could somebody please help me out here?
I really appreciate your help!!

Code: Select all

#include <iostream>
using namespace std;
#define NLEN 66
#define MAX 259

char img[MAX][MAX];
int n = 0, m = 0;//size of area
char c_back;
void fill(int x, int y, char c)
{
  c_back = img[x][y];
  img[x][y] = c;
  if(x > 1 && img[x-1][y] == c_back) fill(x-1, y, c);
  if(x < m && img[x+1][y] == c_back) fill(x+1, y, c);
  if(y > 1 && img[x][y-1] == c_back) fill(x, y-1, c);
  if(y < n && img[x][y+1] == c_back) fill(x, y+1, c);
  return;
}

int main(){
  char command[MAX];//command to input
  char color;//current color
  char color_b;//color backup
  char name[NLEN];
  int i, j;//iterator
  int x1, x2, y1, y2, temp;//coordinators
  while(1){
    cin.getline(command, MAX);
    switch(command[0]){
    case 'I':
      m = 0;
      n = 0;
      for(i = 2; command[i] != ' '; i++)
	m = m * 10 + command[i] - 48;
      for(++i; command[i] != '\0'; i++)
	n = n * 10 + command[i] - 48;
      for(i = 1;i <= m; i++)
	for(j = 1;j <= n; j++)
	  img[i][j] = 'O';
      break;
    case 'C':
      for(i = 1; i <= m; i++)
	for(j = 1; j <= n; j++)
	  img[i][j] = 'O';
      break;
    case 'L':
      x1 = 0;
      y1 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      color = command[++i];
      img[x1][y1] = color;
      break;
    case 'V':
      x1 = 0;
      y1 = 0;
      y2 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] -48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y2 = y2 * 10 + command[i] - 48;
      color = command[++i];
      if(y1>y2){temp = y1; y1 = y2; y2 = temp;}
      for(j = y1; j <= y2; j++)
	img[x1][j] = color;
      break;
    case 'H':
      x1 = 0;
      x2 = 0;
      y1 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	x2 = x2 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      color = command[++i];
      if(x1>x2){temp = x1; x1 = x2; x2 = temp;}
      for(i = x1; i <= x2; i++)
	img[i][y1] = color;
      break;
    case 'K':
      x1 = 0;
      x2 = 0;
      y1 = 0;
      y2 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	x2 = x2 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y2 = y2 * 10 + command[i] - 48;
      color = command[++i];
      for(i = x1; i<= x2; i++)
	for(j = y1; j <= y2; j++)
	  img[i][j] = color;
      break;
    case 'F':
      x1 = 0;
      y1 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      color = command[++i];
      fill(x1, y1, color);
      break;
    case 'S':
      for(i = 2; command[i]!='\0'; i++)
	name[i-2] = command[i];
      name[i-2] = '\0';
      cout<<name<<endl;
      for(i = 1; i <= n; i++){
	for(j = 1; j <= m; j++)
	  cout<<img[j][i];
	cout<<endl;
      }
      break;
    case 'X':
      return 0;
      break;
    default:
      break;
    }
  }
  return 0;
}

mohitkanwal
New poster
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: 10267 - Graphical Editor

Post by mohitkanwal » Wed Apr 15, 2009 6:14 am

Coz ur fill function .. is inappropriate ... check the previous post I also got the same RTE .. you must check that if the new colour is same as old colour of the pixel then exit ..

zzz_hku wrote:I'm new here. I kept getting "Runtime Error" all the time and I don't know why.
Could somebody please help me out here?
I really appreciate your help!!

Code: Select all

#include <iostream>
using namespace std;
#define NLEN 66
#define MAX 259

char img[MAX][MAX];
int n = 0, m = 0;//size of area
char c_back;
void fill(int x, int y, char c)
{
  c_back = img[x][y];
  img[x][y] = c;
  if(x > 1 && img[x-1][y] == c_back) fill(x-1, y, c);
  if(x < m && img[x+1][y] == c_back) fill(x+1, y, c);
  if(y > 1 && img[x][y-1] == c_back) fill(x, y-1, c);
  if(y < n && img[x][y+1] == c_back) fill(x, y+1, c);
  return;
}

int main(){
  char command[MAX];//command to input
  char color;//current color
  char color_b;//color backup
  char name[NLEN];
  int i, j;//iterator
  int x1, x2, y1, y2, temp;//coordinators
  while(1){
    cin.getline(command, MAX);
    switch(command[0]){
    case 'I':
      m = 0;
      n = 0;
      for(i = 2; command[i] != ' '; i++)
	m = m * 10 + command[i] - 48;
      for(++i; command[i] != '\0'; i++)
	n = n * 10 + command[i] - 48;
      for(i = 1;i <= m; i++)
	for(j = 1;j <= n; j++)
	  img[i][j] = 'O';
      break;
    case 'C':
      for(i = 1; i <= m; i++)
	for(j = 1; j <= n; j++)
	  img[i][j] = 'O';
      break;
    case 'L':
      x1 = 0;
      y1 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      color = command[++i];
      img[x1][y1] = color;
      break;
    case 'V':
      x1 = 0;
      y1 = 0;
      y2 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] -48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y2 = y2 * 10 + command[i] - 48;
      color = command[++i];
      if(y1>y2){temp = y1; y1 = y2; y2 = temp;}
      for(j = y1; j <= y2; j++)
	img[x1][j] = color;
      break;
    case 'H':
      x1 = 0;
      x2 = 0;
      y1 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	x2 = x2 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      color = command[++i];
      if(x1>x2){temp = x1; x1 = x2; x2 = temp;}
      for(i = x1; i <= x2; i++)
	img[i][y1] = color;
      break;
    case 'K':
      x1 = 0;
      x2 = 0;
      y1 = 0;
      y2 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	x2 = x2 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y2 = y2 * 10 + command[i] - 48;
      color = command[++i];
      for(i = x1; i<= x2; i++)
	for(j = y1; j <= y2; j++)
	  img[i][j] = color;
      break;
    case 'F':
      x1 = 0;
      y1 = 0;
      for(i = 2; command[i] != ' '; i++)
	x1 = x1 * 10 + command[i] - 48;
      for(++i; command[i] != ' '; i++)
	y1 = y1 * 10 + command[i] - 48;
      color = command[++i];
      fill(x1, y1, color);
      break;
    case 'S':
      for(i = 2; command[i]!='\0'; i++)
	name[i-2] = command[i];
      name[i-2] = '\0';
      cout<<name<<endl;
      for(i = 1; i <= n; i++){
	for(j = 1; j <= m; j++)
	  cout<<img[j][i];
	cout<<endl;
      }
      break;
    case 'X':
      return 0;
      break;
    default:
      break;
    }
  }
  return 0;
}

mohitkanwal
New poster
Posts: 7
Joined: Sun Mar 29, 2009 6:32 pm

Re: 10267 - Graphical Editor

Post by mohitkanwal » Wed Apr 15, 2009 6:17 am

Code: Select all

void fill(int x, int y, char c)
{
  c_back = img[x][y];
  img[x][y] = c;
  if (c_back == c) //ADD THIS LINE
          return;
  if(x > 1 && img[x-1][y] == c_back) fill(x-1, y, c);
  if(x < m && img[x+1][y] == c_back) fill(x+1, y, c);
  if(y > 1 && img[x][y-1] == c_back) fill(x, y-1, c);
  if(y < n && img[x][y+1] == c_back) fill(x, y+1, c);
  return;
}

trelos
New poster
Posts: 3
Joined: Thu Apr 16, 2009 12:25 am

Re: 10267 - Graphical Editor

Post by trelos » Thu Apr 16, 2009 12:48 am

Hello people! New guy here trying to program everything!!

I really have scratched my head over this one but the judge keeps defying me giving me a refusing RE. If anyone can help, I would appreciate it!

Code: Select all

#include <stdio.h>
#include <stdlib.h>

char **B;
int r,c;

void I(M,N){
  int i,j;

  c = M; r = N;
  B = (char**)malloc((N+2)*sizeof(char *));
  for(i=0;i<N+2;i++){ B[i] = (char *)malloc((M+2)*sizeof(char));} 
  for(i=0;i<N+2;i++){
    for(j=0;j<M+2;j++){
      if(i==0||i==N+1||j==0||j==M+1){B[i][j] = '\0';}
      else{ B[i][j] = '0'; }
    }
  }
}

void Cl(){
  int i,j;
  
  for(i=1;i<=r;i++){ for(j=1;j<=c;j++){ B[i][j] = '0'; } }
}

void L(int X, int Y, char C){

  B[Y][X] = C;
}

void V(int X, int Y1, int Y2, char C){
  int i,t;

  if(Y1>Y2){ t=Y1;Y1=Y2;Y2=t;}
  for(i=Y1;i<=Y2;i++){ L(X,i,C); }
}

void H(int X1, int X2, int Y, char C){
  int i,t;

  if(X1>X2){ t=X1;X1=X2;X2=t;}
  for(i=X1;i<=X2;i++){ L(i,Y,C); }
}

void K(int X1,int Y1, int X2, int Y2,char C){
  int i;

  for(i=X1;i<X2;i++){V(i,Y1,Y2,C);}
}

void F(int X, int Y, char C) {
  char oc;

  oc = B[Y][X];
  L(X,Y,C);
  if(C==oc){return;}
  if(B[Y+1][X]==oc){ F(X,Y+1,C); }
  if(B[Y][X-1]==oc){ F(X-1,Y,C); }
  if(B[Y][X+1]==oc){ F(X+1,Y,C); }
  if(B[Y-1][X]==oc){ F(X,Y-1,C); }
}

void S(char *N){
  int i,j;

  printf("%s\n",N);
  for(i=1;i<=r;i++){for(j=1;j<=c;j++){putchar(B[i][j]);} putchar('\n');}
}

int main() {
  char c,C,*n;
  int M,N,P,Q;

  while( (c = getchar())!='X') {
    switch (c){      
      case 'I': scanf(" %d %d\n",&M,&N); 				        free(B);I(M,N); break;
      case 'C': 							                        Cl(); 		      break;
      case 'L': scanf(" %d %d %c\n",&M,&N,&C); 				L(M,N,C);	      break;
      case 'V': scanf(" %d %d %d %c\n",&M,&N,&P,&C); 		V(M,N,P,C);    break;
      case 'K': scanf(" %d %d %d %d %c\n",&M,&N,&P,&Q,&C); 	K(M,N,P,Q,C);break;
      case 'F': scanf(" %d %d %c\n",&M,&N,&C); 				F(M,N,C);	      break;
      case 'H': scanf(" %d %d %d %c\n",&M,&N,&P,&C); 		H(M,N,P,C);    break;
      case 'S': n = (char *)malloc(sizeof(char)); scanf(" %s\n",n); 	S(n); free(n);    break;
      default : while(getchar()!='\n');							break;
    }
  }
}
The logic seems simple, yet I cannot see the mistake.
Any ideas to defeat the evil judge? :P
For a ship without a destination, no wind is favorable...

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 10267 - Graphical Editor

Post by vahid sanei » Thu Apr 16, 2009 8:06 am

Again
bcoz if new and old color be same , your fill function check a cell over and over
you can use a map or a 2-D array for marking cells that you visited already
and
maybe x1 is greater than x2 or y1 > y2 , you should swap them
Impossible says I`m possible

trelos
New poster
Posts: 3
Joined: Thu Apr 16, 2009 12:25 am

Re: 10267 - Graphical Editor

Post by trelos » Thu Apr 16, 2009 2:14 pm

vahid sanei wrote:Again
bcoz if new and old color be same , your fill function check a cell over and over
you can use a map or a 2-D array for marking cells that you visited already
and
maybe x1 is greater than x2 or y1 > y2 , you should swap them
I think I already do those things...

Code: Select all

void V(int X, int Y1, int Y2, char C){
  int i,t;

  if(Y1>Y2){ t=Y1;Y1=Y2;Y2=t;} //swap
  for(i=Y1;i<=Y2;i++){ L(X,i,C); }
}

void H(int X1, int X2, int Y, char C){
  int i,t;

  if(X1>X2){ t=X1;X1=X2;X2=t;} //swap
  for(i=X1;i<=X2;i++){ L(i,Y,C); }
}
and

Code: Select all

void F(int X, int Y, char C) {
  char oc;

  oc = B[Y][X];
  L(X,Y,C);
  if(C==oc){return;} // same colour
  if(B[Y+1][X]==oc){ F(X,Y+1,C); }
  if(B[Y][X-1]==oc){ F(X-1,Y,C); }
  if(B[Y][X+1]==oc){ F(X+1,Y,C); }
  if(B[Y-1][X]==oc){ F(X,Y-1,C); }
}
so it can't be it right? Or maybe I think I do and I am missing something. But thanks for the reply ;)
For a ship without a destination, no wind is favorable...

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 10267 - Graphical Editor

Post by vahid sanei » Thu Apr 16, 2009 4:57 pm

Code: Select all

void K(int X1,int Y1, int X2, int Y2,char C){
  int i;

  for(i=X1;i<X2;i++){V(i,Y1,Y2,C);}
}
you should swap (x1 and x2) and( y1 and y2) here
Impossible says I`m possible

User avatar
vahid sanei
Learning poster
Posts: 84
Joined: Fri Jan 09, 2009 4:37 pm
Location: IRAN

Re: 10267 - Graphical Editor

Post by vahid sanei » Thu Apr 16, 2009 5:02 pm

i found another problem in your code
i think this function

Code: Select all

void F(int X, int Y, char C) {
  char oc;
  oc = B[Y][X];
  L(X,Y,C);
  if(C==oc){return;} // same colour
  if(B[Y+1][X]==oc){ F(X,Y+1,C); }
  if(B[Y][X-1]==oc){ F(X-1,Y,C); }
  if(B[Y][X+1]==oc){ F(X+1,Y,C); }
  if(B[Y-1][X]==oc){ F(X,Y-1,C); }
}
should check X -- > is >= 1 and <=m
and
Y is >=1 and <= n
Impossible says I`m possible

Post Reply

Return to “Volume 102 (10200-10299)”