Jump to content

Erro de apresentaçao em programa desenho


Recommended Posts

Posted

Bom dia.

Estou com um problema na resolução de um problema comum em torneios de programação, no meu caso o enunciado é o seguinte:

"O objectivo deste programa é desenhar um conjunto de rectângulos (com lados paralelos aos eixos) usando caracteres.

Irá receber uma sequência de rectângulos, um por linha, definidos pelas coordenadas (X e Y) dos seus cantos superior esquerdo e inferior direito. Deverá "pintar" todos os rectagulos usando o caracter #. As coordenadas X e Y crescem para a direita e para baixo, respectivamente.

Ele deverá receber como input:

0 0 2 8

0 7 8 8

10 0 12 8

10 0 18 1

10 7 18 8

20 0 22 8

20 0 28 1

20 7 28 8

e dar como output:

60ei.png

O meu código é o seguinte:

#include <iostream>

using namespace std;

int main(){
char mat[1000][1000];
int x2,y2,x1,y1,i,j, maiorx, maiory;
maiorx=0,maiory=0;

for(i=0;i<1000;i++)
	for(j=0;j<1000;j++)
		mat[i][j]='1';

while(cin >> x1 >> y1 >> x2 >> y2){
	if(x2>maiorx) maiorx=x2;
	if(y2>maiory) maiory=y2;
	for(j=x1;j<=x2;j++){
		for(i=y1;i<=y2;i++){
			mat[i][j]='#';
		}
	}
}

for(i=0;i<=maiory;i++){
	 for(j=0;j<=maiorx;j++){
		 if(mat[i][j]!='#') cout << " ";
		 else cout << mat[i][j];
	 }
	 cout<<endl;
 }
 return 0;
}

Isto está a processar esse input, no entanto falha um vector de teste ao qual eu não tenho acesso, dando erro de apresentação, poderão me indicar o que poderá estar a correr mal?

Posted

Pois o meu problema está ai, não tenho certeza, o professor na altura penso que não disse nada sobre isso, é o unico sitio por onde vejo isto a não funcionar correctamente

Posted (edited)

já agora ... essa solução não é muito c++ ...

#include <iostream>
#include <vector>
#include <set>
using namespace std;

class Rect {
public:
   int x1;
   int x2;
   int y1;
   int y2;

   bool visible(int x, int y) {
       return x >= x1 &&
              x <= x2 &&
              y >= y1 &&
              y <= y2;
   }
};

int main() {
   std::vector<Rect> rects;
   int max_x = 0, max_y = 0;

   Rect rect;
   while(cin >> rect.x1 >> rect.y1 >> rect.x2 >> rect.y2) {
       max_x = rect.x2 > max_x ? rect.x2 : max_x;
       max_y = rect.y2 > max_y ? rect.y2 : max_y;

       rects.push_back(rect);
   }

   for (int j = 0; j <= max_y; j++) {
       for (int i = 0; i <= max_x; i++) {
           char c = ' ';

           for (auto it = std::begin(rects); c == ' ' && it!=std::end(rects); ++it)
               if (it->visible(i, j))
                   c = '#';

           cout << c;
       }

       cout << endl;
   }

   return 0;
}

nota : c++11

Edited by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p
Posted

Infelizmente só podemos utilizar C++98, só utilizamos o C++ para nao estar a implementar Queues, listas, conjuntos e assim

Para além disso, existe alguma diferença para compilar com c++11? Esse trecho de código na minha máquina não compila

Posted

Infelizmente só podemos utilizar C++98, só utilizamos o C++ para nao estar a implementar Queues, listas, conjuntos e assim

c++98

#include <iostream>
#include <vector>
#include <set>
using namespace std;

class Rect {
public:
   int x1;
   int x2;
   int y1;
   int y2;

   bool visible(int x, int y) {
       return x >= x1 &&
              x <= x2 &&
              y >= y1 &&
              y <= y2;
   }
};

int main() {
   std::vector<Rect> rects;
   int max_x = 0, max_y = 0;

   Rect rect;
   while(cin >> rect.x1 >> rect.y1 >> rect.x2 >> rect.y2) {
       max_x = rect.x2 > max_x ? rect.x2 : max_x;
       max_y = rect.y2 > max_y ? rect.y2 : max_y;

       rects.push_back(rect);
   }

   for (int j = 0; j <= max_y; j++) {
       for (int i = 0; i <= max_x; i++) {
           char c = ' ';

           std::vector<Rect>::iterator it = rects.begin();
           while (c == ' ' && it != rects.end()) {
               if (it->visible(i, j))
                   c = '#';
               ++it;
           }

           cout << c;
       }

       cout << endl;
   }

   return 0;
}

Para além disso, existe alguma diferença para compilar com c++11? Esse trecho de código na minha máquina não compila

tens de dizer ao compilador para usar c++11, agora como fazer isso depende do compilador

IRC : sim, é algo que ainda existe >> #p@p
Posted

Já sei qual é o problema. O problema é que quando o output é vazio ele está a gerar um \n.

#include <iostream>
#include <vector>
#include <set>
using namespace std;

class Rect {
public:
   int x1;
   int x2;
   int y1;
   int y2;

   bool visible(int x, int y) {
       return x >= x1 &&
              x <= x2 &&
              y >= y1 &&
              y <= y2;
   }
};

int main() {
   std::vector<Rect> rects;
   int max_x = -1, max_y = -1; // <-----------------------

   Rect rect;
   while(cin >> rect.x1 >> rect.y1 >> rect.x2 >> rect.y2) {
       max_x = rect.x2 > max_x ? rect.x2 : max_x;
       max_y = rect.y2 > max_y ? rect.y2 : max_y;

       rects.push_back(rect);
   }

   for (int j = 0; j <= max_y; j++) {
       for (int i = 0; i <= max_x; i++) {
           char c = ' ';

           std::vector<Rect>::iterator it = rects.begin();
           while (c == ' ' && it != rects.end()) {
               if (it->visible(i, j))
                   c = '#';
               ++it;
           }

           cout << c;
       }

       cout << endl;
   }

   return 0;
}
IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.