Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- int*** games;
- int number_of_games = 0;
- int* number_of_points;
- int** pieces;
- int num_pieces , counter = 0;
- int** points;
- int cmp(int *a, int *b){
- int* xi = (int*)a;
- int* xj = (int*)b;
- if(xi[0] < xj[0])return -1;
- if(xi[0] > xj[0]) return 1;
- if(xi[1] < xj[1])return -1;
- if(xi[1] > xj[1])return 1;
- }
- int* input(){
- int *visited;
- int out = 0;
- int pos = 0;
- int contador = 0;
- int i,j,aux_x,aux_y;
- scanf("%d",&num_pieces);
- visited = (int*)malloc(num_pieces*sizeof(int));
- for(i=0;i<num_pieces;i++){
- visited[i] = 1;
- }
- pieces = (int**)malloc(num_pieces*sizeof(int*));
- for(i = 0; i < num_pieces; i++){
- scanf("%d %d",&aux_x,&aux_y);
- for(j=0;j<pos;j++){
- if((aux_x==pieces[j][0] && aux_y==pieces[j][1]) || (aux_y==pieces[j][0] && aux_x==pieces[j][1])){
- visited[j]++;
- contador++;
- out = 1;
- break;
- }
- }
- if(out==0){
- pieces[pos] = (int*) malloc(2*sizeof(int));
- pieces[pos][0] = aux_x;
- pieces[pos][1] = aux_y;
- pos++;
- }
- out=0;
- }
- num_pieces -= contador;
- return visited;
- }
- int** insere(int** points, int num_points,int index,int x1,int y1,int x2,int y2,int n){
- // n = -1 eliminate index
- // n == 0 del index, insert x1,y1,
- // n == 1 del index, insert x1,y1,x2,y2,
- //sort
- int** aux = (int**) malloc((num_points+n)*sizeof(int*));
- int i, j ,aux1,aux2;
- if(n == -1){
- for(i = 0; i< num_points+n; i++){
- aux[i] = (int*) malloc(2 * sizeof(int));
- if( i >= index){
- aux[i][0] = points[i+1][0];
- aux[i][1] = points[i+1][1];
- }
- else{
- aux[i][0] = points[i][0];
- aux[i][1] = points[i][1];
- }
- }
- }
- else if(n == 0 || n == 1){
- for(i = 0 ; i<num_points + n ; i++){
- aux[i] = (int*) malloc(2 * sizeof(int));
- if( i == index){
- aux[i][0] = x1;
- aux[i][1] = y1;
- }
- else if(i < num_points){
- aux[i][0] = points[i][0];
- aux[i][1] = points[i][1];
- }
- }
- if(n == 1){
- aux[num_points][0] = x2;
- aux[num_points][1] = y2;
- }
- }
- for(i = 1;i < num_points + n;i++){
- j = i - 1;
- aux1 = aux[i][0];
- aux2 = aux[i][1];
- while(j >= 0 && aux[j][0] > aux1){
- aux[j+1][0] = aux[j][0];
- aux[j+1][1] = aux[j][1];
- j--;
- }
- aux[j+1][0] = aux1;
- aux[j+1][1] = aux2;
- }
- return aux;
- }
- void printarray(int** array,int size){
- int i;
- for(i = 0;i<size;i++){
- printf("%d %d\n",array[i][0],array[i][1]);
- }
- printf("\n");
- }
- int check(int** currently_game,int points){
- int i,j;
- for(i=0;i<number_of_games;i++){
- if(number_of_points[i]!=points)
- continue;
- for(j=0;j<points;j++){
- if(games[i][j][0] != currently_game[j][0] || games[i][j][1] != currently_game[j][1]){
- break;
- }
- if(j==points-1){
- return 0;
- }
- }
- if(i == number_of_games - 1){
- return 1;
- }
- }
- return 0;
- }
- int ** sort(int** pieces, int size){
- int i,j;
- int* aux = (int*)malloc(2*sizeof(int));
- int** arr = (int**)malloc(size*sizeof(int*));;
- for(i=0;i<size;i++){
- arr[i] = (int*)malloc(2*sizeof(int));
- arr[i][0] = pieces[i][0];
- arr[i][1] = pieces[i][1];
- }
- for(i = 0;i<size;i++){
- for(j = i+1;j<size;j++){
- if(cmp(arr[i],arr[j]) == 1){
- aux[0] = arr[i][0];
- aux[1] = arr[i][1];
- arr[i][0] = arr[j][0];
- arr[i][1] = arr[j][1];
- arr[j][0] = aux[0];
- arr[j][1] = aux[1];
- }
- }
- }
- return arr;
- }
- void algo(int** points, int* visited,int num_points,int** currently_game,int points_game){
- int i, j, k, max, limx,limy,m = 0;
- int** aux;
- //printf(" --- %d\n",currently_game[0][1]);
- //for(j = 0;j < number_of_games;j++){
- //}
- //Condição saida
- if(num_points <= 2){
- for(i = 0; i < num_pieces; i++){
- if(visited[i] != 0){
- m = 1;
- break;
- }
- if(m != 1 && i == num_pieces-1){
- currently_game = sort(currently_game,points_game);
- if(number_of_games != 0){
- if(check(currently_game,points_game) == 1){
- counter++;
- number_of_games++;
- // games = (int***)realloc(games, (number_of_games)*sizeof(int**));
- // games[number_of_games-1] = (int**)malloc(points_game*sizeof(int*));
- for(k=0;k<points_game;k++){
- //games[number_of_games-1][k] = (int*)malloc(2*sizeof(int));
- games[number_of_games-1][k][0] = currently_game[k][0];
- games[number_of_games-1][k][1] = currently_game[k][1];
- }
- //number_of_points = (int*)realloc(number_of_points,number_of_games*sizeof(int));
- number_of_points[number_of_games-1] = points_game;
- //printf("JOGO:\n");
- //printarray(currently_game,points_game);
- }
- }else{
- for(k=0;k<points_game;k++){
- //games[number_of_games][k] = (int*)malloc(2*sizeof(int));
- games[number_of_games][k][0] = currently_game[k][0];
- games[number_of_games][k][1] = currently_game[k][1];
- }
- //number_of_points = (int*)malloc(sizeof(int));
- number_of_points[0] = points_game;
- number_of_games++;
- counter++;
- }
- return;
- }
- }
- }
- for(j = 0;j < num_points; j++){
- for(i = 0;i < num_pieces; i++){
- if(visited[i] > 0){
- if(j == 0){
- limx = points[j+1][0];
- limy = INT_MAX;
- }
- else if(j == (num_points-1)){
- limx = INT_MAX;
- limy = points[j-1][1];
- }
- else{
- limx = points[j+1][0];
- limy = points[j-1][1];
- }
- //SQUARE
- if(pieces[i][0] == pieces[i][1])
- max = 1;
- else
- max = 2;
- for(k = 0; k < max; k++){
- if((points[j][0] + pieces[i][k]) <= limx && (points[j][1] + pieces[i][abs(k-1)]) <= limy ){
- //add point same y
- if(points[j][0] + pieces[i][k] < limx && points[j][1] + pieces[i][abs(k-1)] == limy){
- points_game++;
- //currently_game = (int**)realloc(currently_game,points_game*sizeof(int*));
- //currently_game[points_game-1] = (int*)malloc(2*sizeof(int));
- currently_game[points_game-1][0] = points[j][0]+pieces[i][k];
- currently_game[points_game-1][1] = points[j][1];
- //falta funcao para ordenar currently_game por x e y quando x e igual
- aux = insere(points,num_points,j,points[j][0]+pieces[i][k],points[j][1],0,0,0);
- visited[i]--;
- algo(aux,visited,num_points,currently_game,points_game);
- points_game--;
- visited[i]++;
- }
- //add point two points
- else if(points[j][0] + pieces[i][k] < limx && points[j][1] + pieces[i][abs(k-1)] < limy){
- points_game+=2;
- //currently_game = (int**)realloc(currently_game,points_game*sizeof(int*));
- //currently_game[points_game-2] = (int*)malloc(2*sizeof(int));
- //currently_game[points_game-1] = (int*)malloc(2*sizeof(int));
- currently_game[points_game-2][0] = points[j][0];
- currently_game[points_game-2][1] = points[j][1]+pieces[i][abs(k-1)];
- currently_game[points_game-1][0] = points[j][0]+pieces[i][k];
- currently_game[points_game-1][1] = points[j][1];
- //falta funcao para ordenar currently_game por x e y quando x e igual
- aux = insere(points,num_points,j,points[j][0],points[j][1]+pieces[i][abs(k-1)],points[j][0]+pieces[i][k],points[j][1],1);
- num_points++;
- visited[i]--;
- algo(aux,visited,num_points,currently_game,points_game);
- visited[i]++;
- num_points--;
- points_game-=2;
- }
- //Perfect
- else if(points[j][0] + pieces[i][k] == limx && points[j][1] + pieces[i][abs(k-1)] == limy){
- aux = insere(points,num_points,j,0,0,0,0,-1);
- visited[i]--;
- num_points--;
- algo(aux,visited,num_points,currently_game,points_game);
- num_points++;
- visited[i]++;
- }
- //add point same x
- else if(points[j][0] + pieces[i][k] == limx && points[j][1] + pieces[i][abs(k-1)] < limy){
- points_game++;
- //currently_game = (int**)realloc(currently_game,points_game*sizeof(int*));
- //currently_game[points_game-1] = (int*)malloc(2*sizeof(int));
- currently_game[points_game-1][0] = points[j][0];
- currently_game[points_game-1][1] = points[j][1]+pieces[i][abs(k-1)];
- aux = insere(points,num_points,j,points[j][0],points[j][1]+pieces[i][abs(k-1)],0,0,0);
- visited[i]--;
- algo(aux,visited,num_points,currently_game,points_game);
- visited[i]++;
- points_game--;
- }
- }
- }
- }
- }
- }
- }
- int main()
- {
- int i, j, max;
- int** points;
- int* used_pieces = input();
- points = (int**)malloc(20*sizeof(int*));
- games = (int***)malloc(20*sizeof(int**));
- number_of_points = (int*)malloc(20*sizeof(int));
- int** currently_game = (int**)malloc(20*sizeof(int*));
- for(i=0;i<8;i++){
- games[i] = (int**)malloc(20*sizeof(int*));
- for(j = 0;j<8;j++){
- games[i][j] = (int*)malloc(2*sizeof(int));
- }
- }
- for(i = 0; i < 8 ; i++){
- points[i] = (int*)malloc(20*sizeof(int));
- currently_game[i] = (int*)malloc(2*sizeof(int));
- }
- for(i=0;i<num_pieces;i++){
- points[0][0] = pieces[i][0];
- points[0][1] = 0;
- points[1][0] = 0;
- points[1][1] = pieces[i][1];
- //SQUARE
- points = insere(points,3,2,0,0,0,0,-1);
- if(pieces[0][0] == pieces[0][1]){
- max = 1;
- }
- else
- max = 2;
- for(j = 0;j < max; j++ ){
- if(j == 1){
- points[0][0] = pieces[i][1];
- points[0][1] = 0;
- points[1][0] = 0;
- points[1][1] = pieces[i][0];
- points = insere(points,3,2,0,0,0,0,-1);
- }
- //currently_game[0] = (int*)malloc(2*sizeof(int));
- //currently_game[1] = (int*)malloc(2*sizeof(int));
- currently_game[0][0] = points[0][0];
- currently_game[0][1] = points[0][1];
- currently_game[1][0] = points[1][0];
- currently_game[1][1] = points[1][1];
- used_pieces[i]--;
- algo(points,used_pieces,2,currently_game,2);
- used_pieces[i]++;
- }
- }
- for(i = 0;i<number_of_games;i++){
- printf("JOGO:\n");
- printarray(games[i],number_of_points[i]);
- }
- printf("%d",counter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement