Created
February 18, 2014 19:45
-
-
Save gonzalezfj/9078485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
#include<queue> | |
#include <sstream> | |
#include <stdlib.h> /* atoi */ | |
using namespace std; | |
void Fill(char** matrix,int x1,int y1,int x2,int y2, char c); | |
void Clear(char** matrix,int b, int c) | |
{ | |
Fill(matrix,0,0,(b-1),(c-1),'0'); | |
} | |
void Fill(char** matrix,int x1,int y1,int x2,int y2, char c) | |
{ | |
for(int i=x1;i<=x2;i++) | |
{ | |
for(int j=y1;j<=y2;j++) | |
{ | |
matrix[i][j]=c; | |
} | |
} | |
} | |
struct point | |
{ | |
int x,y; | |
}; | |
int main() | |
{ | |
int M,N; | |
int x,y; | |
int x2,y2; | |
string str; | |
char** matrix; | |
char color; | |
char a; | |
string nombre; | |
queue<struct point> cola; | |
struct point aux; | |
vector<string> tokens; | |
string buf; | |
while (!getline(cin,str).eof()) { | |
stringstream ss(str); | |
tokens.clear(); | |
while (ss >> buf) | |
tokens.push_back(buf); | |
switch(str[0]) | |
{ | |
case 'I': | |
N = atoi(tokens[1].c_str()); | |
M = atoi(tokens[2].c_str()); | |
matrix = new char*[M]; | |
for(int i=0;i<M;i++) | |
{ | |
matrix[i] = new char[N]; | |
} | |
Clear(matrix,M,N); | |
break; | |
case 'C': | |
Clear(matrix,M,N); | |
break; | |
case 'L': | |
x = atoi(tokens[1].c_str()); | |
y = atoi(tokens[2].c_str()); | |
matrix[x][y]=tokens[3][0]; | |
break; | |
case 'V': | |
//V X Y1 Y2 C | |
x = atoi(tokens[1].c_str()); | |
y = atoi(tokens[2].c_str()); | |
y2= atoi(tokens[3].c_str()); | |
for(int i=y;i<=y2;i++) | |
{ | |
matrix[i][x] = tokens[4][0]; | |
} | |
break; | |
case 'H': | |
//H X1 X2 Y C | |
x = atoi(tokens[1].c_str()); | |
x2= atoi(tokens[2].c_str()); | |
y = atoi(tokens[3].c_str()); | |
for(int i=x;i<=x2;i++) | |
{ | |
matrix[y][i] = tokens[4][0]; | |
} | |
break; | |
case 'K': | |
//K X1 Y1 X2 Y2 C | |
Fill( | |
matrix, | |
atoi(tokens[1].c_str()), | |
atoi(tokens[2].c_str()), | |
atoi(tokens[3].c_str()), | |
atoi(tokens[4].c_str()), | |
tokens[5][0]); | |
break; | |
case 'F': | |
//FXYC | |
x = atoi(tokens[1].c_str()); | |
y = atoi(tokens[2].c_str()); | |
color = matrix[x][y]; | |
//c = str[8]; | |
matrix[x][y]=tokens[3][0]; | |
aux.x = x; | |
aux.y = y; | |
while(!cola.empty()) | |
{ | |
cola.pop(); | |
} | |
cola.push(aux); | |
while(!cola.empty()) | |
{ | |
aux = cola.front(); | |
x = aux.x; | |
y = aux.y; | |
cola.pop(); | |
if(x+1<M) | |
if(matrix[x+1][y]==color) | |
{ | |
aux.x = x+1; | |
aux.y = y; | |
cola.push(aux); | |
matrix[x+1][y]= tokens[3][0]; | |
} | |
if(x-1>=0) | |
if(matrix[x-1][y]==color) | |
{ | |
aux.x = x - 1; | |
aux.y = y; | |
cola.push(aux); | |
matrix[x-1][y]= tokens[3][0]; | |
} | |
if(y-1>=0) | |
if(matrix[x][y-1]==color) | |
{ | |
aux.x = x; | |
aux.y = y -1; | |
cola.push(aux); | |
matrix[x][y-1]= tokens[3][0]; | |
} | |
if(y+1<N) | |
if(matrix[x][y+1]==color) | |
{ | |
aux.x = x; | |
aux.y = y+1; | |
cola.push(aux); | |
matrix[x][y+1]= tokens[3][0]; | |
} | |
} | |
break; | |
case 'S': | |
nombre = str.substr(2); | |
cout << nombre << endl; | |
for(int i=0;i<M;i++) | |
{ | |
for(int j=0;j<N;j++) | |
{ | |
cout << matrix[i][j]; | |
} | |
cout << endl; | |
} | |
break; | |
case 'X': | |
return 0; | |
} | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment