mundo Posted February 24, 2014 at 10:26 AM Report #546435 Posted February 24, 2014 at 10:26 AM 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: 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?
HappyHippyHippo Posted February 24, 2014 at 10:38 AM Report #546438 Posted February 24, 2014 at 10:38 AM sabes se o valor máximo que uma coordenada pode tomar é 1000 ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
mundo Posted February 24, 2014 at 10:55 AM Author Report #546441 Posted February 24, 2014 at 10:55 AM 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
HappyHippyHippo Posted February 24, 2014 at 11:01 AM Report #546443 Posted February 24, 2014 at 11:01 AM (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 February 24, 2014 at 11:01 AM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
mundo Posted February 24, 2014 at 01:45 PM Author Report #546468 Posted February 24, 2014 at 01:45 PM 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
HappyHippyHippo Posted February 24, 2014 at 01:48 PM Report #546469 Posted February 24, 2014 at 01:48 PM 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 Portugol Plus
mundo Posted February 24, 2014 at 04:19 PM Author Report #546493 Posted February 24, 2014 at 04:19 PM O problema continua o mesmo, penso eu que seja bug do mooshak, avaliar que é usado.
HappyHippyHippo Posted February 24, 2014 at 04:43 PM Report #546501 Posted February 24, 2014 at 04:43 PM não sei se tens um limite de tempo ou memória para a resolução do resultado IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
mundo Posted February 25, 2014 at 10:15 AM Author Report #546561 Posted February 25, 2014 at 10:15 AM Já sei qual é o problema. O problema é que quando o output é vazio ele está a gerar um \n.
HappyHippyHippo Posted February 25, 2014 at 10:17 AM Report #546562 Posted February 25, 2014 at 10:17 AM 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 Portugol Plus
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now