본문 바로가기

_BOJ/_C , C++

BOJ_18268 : COW GYMNASTICS, 소체조

반응형

https://www.acmicpc.net/problem/18268

 

18268번: Cow Gymnastics

The consistent pairs of cows are $(1,4)$, $(2,4)$, $(3,4)$, and $(1,3)$.

www.acmicpc.net

문제

In order to improve their physical fitness, the cows have taken up gymnastics! Farmer John designates his favorite cow Bessie to coach the N other cows and to assess their progress as they learn various gymnastic skills.

In each of K practice sessions (1≤K≤10), Bessie ranks the N cows according to their performance (1≤N≤20). Afterward, she is curious about the consistency in these rankings. A pair of two distinct cows is consistent if one cow did better than the other one in every practice session.

Help Bessie compute the total number of consistent pairs.

입력

The first line of the input file contains two positive integers K and N. The next K lines will each contain the integers 1…N in some order, indicating the rankings of the cows (cows are identified by the numbers 1…N). If A appears before B in one of these lines, that means cow A did better than cow B.

출력

Output, on a single line, the number of consistent pairs.

예제 입력 1

3 4 4 1 2 3 4 1 3 2 4 2 1 3

예제 출력 1

4

힌트

The consistent pairs of cows are (1,4), (2,4), (3,4), and (1,3).

 

대충 해석해봤는데

소 체조

육체적 피트니스를 향상시키기 위해 소들도 체조를 시작했습니다. 존이 가장 아끼는 배씨(소) 를 N 다른 소들과 진전을 파악하게 해서 다양한 체조를 배울 수 있게 했어요.
매번 K번(1<K<10)할때마다 배씨를 포함한 다른 소들의 랭크를 매겨서(1<N<20), 배씨(여자)의 등수에 대한 궁금증을 해소시켜주려고 합니다. 한짝의 소들 중에 누가 더 나은지 매 번 알려주게 해주려고 합니다.

배씨가 모든 짝에서 몇등인지 알려주세요.

입력
첫줄은 양의 정수 K하고 N이 입력됩니다.
다음 K 개의 줄은 1부터 N 까지 조금 정렬되어있을텐데요, 소들의 등수를 나타내는 순서대로 입니다. 만약 A가 B보다 늦게 정렬되어있으면, A소가 B소보다 잘했다는 뜻입니다.

출력
한줄로 일관된 짝을 출력하세요.


힌트
모든 K 번 에서 4는 항상 잘했기에
4 1 / 4 2 / 4 3 성립함.
1이랑 3도 고정됨.
1 3 성립함.
도합 4개 성립함. 

동적 배열로 2차원 배열을 만든 다음에, N개의 배열을 K개 만들었다. 그다음 첫번째 배열의 모든 경우의 수를 밑에 배열에 대입하여 하나라도 틀리면 그 케이스는 BREAK, 모든 케이스를 통과하면 TEMP 값을 증가시켜 TEMP 만 출력하게 하였다.

#include <stdio.h>                           //4123
#include <stdlib.h>                          //4132
int k, n;                                    //4213

int cowgym(int **arr, int head, int neck){
	int i, j, z;
	for(i=0;i<k;i++){
		for(j=0;j<n;j++){
			if(arr[i][j]==head){
				//printf("ok\n");
				break;
			}
			else if(arr[i][j]==neck){
				//printf("------ : %d\n",arr[i][j]);
				return -1;
			}
		}
	}
	return 1;
}

int main(void){
	int** arr, i, j, temp=0, check=0;

	scanf("%d %d", &k, &n);

	arr=(int**)malloc(sizeof(int*)*k);
	for (i=0;i<k;i++){
		arr[i] = (int*)malloc(sizeof(int)*n);
	}
	for(i=0;i<k;i++){
		for(j=0;j<n;j++){
			scanf("%d",&arr[i][j]);
		}
	}
	for(i=0;i<n-1;i++){
		for(j=1;j<n;j++){
			if(arr[0][i]==arr[0][j]){
				//printf("%d %d continue \n", i, j);
				continue;
			}
			//printf("%d, %d\n",arr[0][i], arr[0][j]);
			check = cowgym(arr, arr[0][i], arr[0][j]);
			if(check==1){
				//printf("check\n");
				temp++;
			}
		}
	}
	printf("%d",temp);
	return 0;
}

#include <stdio.h>

#include <stdlib.h> //malloc 사용하기 위해

int k, n;

 

int cowgym(int **arr, int head, int neck){ //cowgym이란 함수를 만들어 주었다. 배열에서 경우의 수 뽑아주는함수다.

        int i, j, z;

        for(i=0;i<k;i++){

                for(j=0;j<n;j++){

                        if(arr[i][j]==head){ //여기가 포인트인데, head 를 먼저 찾으면 head 다음 neck가 왔다는 뜻이니까

                                //printf("ok\n"); //  ok 해준다, 더 찾을 필요가 없기에

                                break;

                        }

                        else if(arr[i][j]==neck){ //반면에 neck가 먼저 나오면 틀린 배열이니까

                                //printf("------ : %d\n",arr[i][j]);

                                return -1; //-1 반환해준다.

                        }

                }

        }

        return 1; //위에서 head에 해당하는 수를 찾았기에 정상 값을 반환한다.

}

 

int main(void){

        int** arr, i, j, temp=0, check=0; //2중 포이터 배열 선언

 

        scanf("%d %d", &k, &n);

 

        arr=(int**)malloc(sizeof(int*)*k); //k개의 칸 확보해주고

        for (i=0;i<k;i++){

                arr[i] = (int*)malloc(sizeof(int)*n); //n칸만큼의 배열을 만든다.

        }

        for(i=0;i<k;i++){

                for(j=0;j<n;j++){

                        scanf("%d",&arr[i][j]);

                }

        }

        for(i=0;i<n-1;i++){

                for(j=1;j<n;j++){

                        if(arr[0][i]==arr[0][j]){ //배열의 *첫단어와 *둘째단어가(이하 예시임) 같으면

                                //printf("%d %d continue \n", i, j);

                                continue; // 그 케이스는 패스

                        }

                        //printf("%d, %d\n",arr[0][i], arr[0][j]);

                        check = cowgym(arr, arr[0][i], arr[0][j]); 첫번째를 head, 둘째를 neck 라고 말해뒀다 위에.

                        if(check==1){ //1이 나오면 정상.

                                //printf("check\n");

                                temp++;

                        }

                }

        }

        printf("%d",temp);

        return 0;

}

반응형