Exemplo n.º 1
0
int main(){
//    int matriz[5][5];
//     int num;
//     for(int i=0; i<5; ++i){
//          for(int j=0; j<5; ++j){
//               cin >> num;
//               matriz[i][j] = num;
//          }
//     }
//     printMatriz(matriz);
//     return 0;
     int filas, columnas;
     Matriz matriz;
     cout << "cantidad de filas:\n";
     cin >> filas;
     cout << "cantidad de columnas:\n";
     cin >> columnas;
     Array arr(columnas);     
     for(int i=0; i<filas; ++i){
          for(int j=0; j<columnas; ++j){
               cin >> arr[j];
          }
          matriz.push_back(arr);
     }
     printVector(matriz);
     return 0;
}
Exemplo n.º 2
0
vector<vector<double> > Tsp::readTSPProblemFile(string filename){
//	toolbox *tb = toolbox::instance();

	typedef vector<double> Fila;
	typedef vector<Fila> Matriz;
	typedef vector<double> Fila_d;
	typedef vector<Fila_d> Matriz_d;
	Fila fila(2);
	char * charfile = new char[filename.length()+1];
	strcpy(charfile,filename.c_str());
	ifstream open (charfile,istream::in);
	if (open)
		cout << "El fichero TSP se ha abierto correctamente";
	else{
		cout << "No se ha podido abrir el fichero de problema TSP";
		exit(0);
	}
	cout << endl;
	Matriz matriz;
	string line = "";
	int i =1;
	int k = 1;
	int j = 0;
	int num = 0;
	int aux = 0;
	bool flag = 0;
	string node_coord_section = "NODE_COORD_SECTION";

	while(!open.eof()){
		open >> line;
		if (line[0] != ' ') {
			if (flag) {
				aux = atoi(line.c_str());
				if (k != 1 && line.compare("EOF") != 0){// && line.compare(" ") != 0) {
					num = aux;
					if(j==0 && k == 2){
						fila[0] = (double) num;
						j++;
						k++;
					}else if(k == 3){
						fila[1] = (double) num;
						matriz.push_back(fila);
	//					cout << "VECTOR AGREGADO a matriz "<< matriz[i-1][0] << " " << matriz[i-1][1]  << endl;
						j = 0;
						i++;
						k=1;
					}
				}else{
					k++;
				}
			}
			if (line.compare("NODE_COORD_SECTION") == 0) {
//				cout << "se encontro NODE COORD SECTION" << endl;
				flag = 1;
			}
/*			if (line.compare("EOF") == 0)
				cout << "Fin del archivo"<< endl;*/
		}else{
			open.ignore(99999,'\n');
		}
	}

	open.close();
/*
	cout << "Creando matriz de distancias" << endl;
//===========Creacion de la matriz de distancias===============
	Matriz_d distancias;
	Fila_d filas ;
	double x1, y1, x2, y2 = 0;
	double dist = 0;
	for (int i = 0; i < (int) matriz.size(); i++) {
		for (int j = 0; j < (int) matriz.size(); j++) {
			x1 = (double) matriz[i][0];
			x2 = (double) matriz[j][0];
			y1 = (double) matriz[i][1];
			y2 = (double) matriz[j][1];
			dist = sqrt(pow((y2-y1),2)+pow((x2-x1),2));
			filas.push_back(dist);
//			cout << "Dato agregado: " << dist << " i= " << i+1 << "| j= " << j+1 << endl;
		}
		cout << "Se ha agregado la fila n°" << i+1 << endl;
		distancias.push_back(filas);
		filas.clear();
	}
*/
	/*cout << "Matriz distancias: " << endl;
	for(int i=0; i < (int)distancias.size(); i++){
		for(int j=0; j < (int)distancias[i].size();j++){
			cout << distancias[i][j] << " ";
		}
		cout << endl;
	}*/

//	return distancias;
	return matriz;
}