Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/05/2011, 09:03
yochiwarez
 
Fecha de Ingreso: enero-2009
Mensajes: 7
Antigüedad: 15 años, 3 meses
Puntos: 0
Información Necesito entender este codigo de Sudoku en C++

Hola tengo que hacer un sudoku simple.. en C++, lo he intentado de muchas formas pero siempre había una parte en donde se trababa , hasta que decidí buscar algún código en Internet , me di con la sorpresa de que el sudoku era mucho mas complejo de lo que creía, pero no necesito hacerlo tan complejo aun... así que encontré uno sin muchas lineas de código...que hace exactamente lo que necesito pero,...no puedo entenderlo muy bien....porfavor alguien que me ayude... ...... perdón si ubique mal mi tema

Código:
#include "stdafx.h"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "math.h"
using namespace std;

int a[9][9], b[9][9];

bool Verif_cuadros(int grupo){
	int posic, i;
	bool acepto;

	acepto= true;
	for (posic=1; posic<10; posic++) {
		for (i=1; i<posic; i++) {
			if (b[grupo][posic]==b[grupo][i]) {
				acepto=false;
				goto sali;
			}
		}
	}
sali: return acepto;

}

void Mezclar(){
	int restoX, restoY;
	int subir, t, x, y;
	float X, Y;
	int grupo, posic;
	bool repet=false, acepta;

	srand((unsigned)time(NULL));

	//recorre la matriz a partir del 1
	for (y=1; y<10; y++) {
		for (x=1; x<10; x++) {

			a[y][x]= (rand()%9) + 1;

			//busca repetidos antes de x
			for (t=1; t<x; t++) {
				if (a[y][x]==a[y][t]) repet=true;
			}

			//busca repetidos antes de y
			if (repet==false) {
				for (t=1; t<y; t++) {
						if (a[y][x]==a[t][x]) repet=true;
				}
			}

			//si se repite
			if (repet==true){
				repet=false;
				if (subir> 20){
					subir= 0;
					x= 0;
				}
				else {
					subir++;
					x--;
				}
            }
			// si no se repite
			else {

				//busca multiplos de 3
				/*
					1 : 1
					2 : 2
					3 : 0
					4 : 1
					5 : 2
					6 : 0
					7 : 1
					8 : 2
					9 : 0

				Ceil de 1 sobre 3 : 1
				Ceil de 2 sobre 3 : 1
				Ceil de 3 sobre 3 : 1
				Ceil de 4 sobre 3 : 2
				Ceil de 5 sobre 3 : 2
				Ceil de 6 sobre 3 : 2
				Ceil de 7 sobre 3 : 3
				Ceil de 8 sobre 3 : 3
				Ceil de 9 sobre 3 : 3

					 */
				restoY= y % 3;
				restoX= x % 3;
				
				if (restoX==0) restoX=3;
				if (restoY==0) restoY=3;
				Y= y; X= x;
				
				posic= 3*(restoY-1) + restoX;
				grupo= 3*(ceil(Y/3)-1) + ceil(X/3);
				b[grupo][posic]= a[y][x];
				

				subir= 0;
			}
	
		}

		
		if ((y%3)==0) {
			for (grupo=y-2; grupo<=y; grupo++){
				acepta= Verif_cuadros(grupo);
				if (acepta==false) {
					y= y-3;
					goto sali;
				}
			}
		}
sali:;
	}
}


void Imprimir(){
	int y, x;

	for (y=1; y<10; y++) {
		for (x=1; x<10; x++) {
			cout << a[y][x] << " ";
		}
		cout << endl;
	}

	cout<<endl;
}


int _tmain(int argc, _TCHAR* argv[])
{

	Mezclar();
	Imprimir();

	system("pause");
	return 0;
}

Última edición por yochiwarez; 14/05/2011 a las 09:16