개발자 톡
연습문제 톡
[HSAT 1회 정기 코딩 인증평가 기출] 안전운전을 도와줄 차세대 지능형 교통시스템
테스트 케이스 b8한개만 오답이라고 나옵니다.. 반례 부탁드립니다!
- 등록일
- 2023-12-21 18:26:03
- 조회수
- 428
- 작성자
- hschoi0416
특수한 케이스가 문제인가 싶기도 한데 도저히 감을 못잡겠어서 질문드립니다!
gpt 보고 메모리 누수 관련해서 수정해봤는데 결과는 똑같습니다..
#include <stdio.h>
#include <stdlib.h>
typedef struct sig_info {
int into;
int len;
int to[3];
}Info;
typedef struct car{
int x, y, into, t;
struct car* tail;
}Car;
typedef struct Queue{
struct car* front;
struct car* rear;
} Queue;
int** sig;
int Q_len = 0;
Info I[13];
Car car;
Car* head;
int** vst;
Queue* Q;
void Q_init() {
head = (Car*)malloc(sizeof(Car));
Q = (Queue*)malloc(sizeof(Queue));
Q->front = NULL;
Q->rear = NULL;
head->tail = head;
}
int Q_empty_check() {
if (Q->front == NULL && Q->rear == NULL) {
//printf("Q is empty\n");
return 1;
}
else {
return 0;
}
}
void enQ (Car* c, int x, int y, int into, int t){
//printf("enqu %d %d %d %d\n", x, y, into, t);
c = (Car*)malloc(sizeof(Car));
c->x = x;
c->y = y;
c->into = into;
c->t = t;
c->tail = c;
if (Q->rear == NULL){
Q->rear = c;
Q->front = c;
}
else {
Q->rear->tail = c;
Q->rear = c;
}
Q_len++;
}
Car* deQ (){
if (Q_empty_check()){
return NULL;
}
Car *s = NULL;
s = Q->front;
Q->front = Q->front->tail;
//printf("deQ: %d %d %d\n", s->x, s->y, s->into);
Q_len--;
if (Q_len == 0){
Q->front = NULL;
Q->rear = NULL;
}
return s;
}
void delete_Q(Car* c){
Car* s;
while(1){
if (c->tail == c){
//printf("free: %p c->x: %d, c->y: %d \n", c, c->x, c->y);
free(c);
return;
}
s = c->tail;
//printf("free: %p c->x: %d, c->y: %d \n", c, c->x, c->y);
free(c);
c = s;
}
}
void BFS(int T, int N){
int sig_num;
while(1) {
if (Q_empty_check()){
//printf("BFS_end\n");
return;
}
Car* a = NULL;
Car* b = NULL;
Car* c = NULL;
Car* d = NULL;
Car* s = deQ();
if (s == NULL){
break;
}
/*
if (s->t > T) {
//printf("s->t > T\n");
continue;
} */
sig_num = s->t % 4;
//printf("s->x %d, s->y %d, s->into %d\n", s->x, s->y, s->into);
vst[s->x][s->y]++;
int xy = (s->x-1) + (s->y-1) * N;
//printf("s_into: %d, sig_into: %d, xy: %d\n", s->into, I[sig[xy][s->t]].into, xy);
//printf("t: %d xy: %d\n", s->t, xy);
if (s->into != I[sig[xy][sig_num]].into){
//printf("into != sig_into\n");
}
else{
//printf("xy: %d\n", xy);
//printf("I[sig[xy][sig_num]].to[0]: %d\n", I[sig[xy][sig_num]].to[0]);
//printf("I[sig[xy][sig_num]].to[1]: %d\n", I[sig[xy][sig_num]].to[1]);
//printf("I[sig[xy][sig_num]].to[2]: %d\n", I[sig[xy][sig_num]].to[2]);
for (int i = 0; i < 3; i++) {
if (I[sig[xy][sig_num]].to[i] == 0){
if (s->y - 1 > 0 ){
//xy = (s->x-1) + (s->y-2) * 3;
//printf("enQ 0: %d, into: %d\n", xy, I[sig[xy][s->t]].to[i]);
if (s->t < T) {
//printf("s->t > T\n");
enQ(a, s->x, s->y - 1, I[sig[xy][sig_num]].to[i], s->t + 1);
}
}
}
else if (I[sig[xy][sig_num]].to[i] == 1){
if (s->x + 1 <= N){
//xy = (s->x) + (s->y - 1) * 3;
//printf("enQ 1: %d, into: %d\n", xy, I[sig[xy][s->t]].to[i]);
//printf("B: %p, s->x: %d, s->y + 1: %d, I[sig[xy][sig_num]].to[i]: %d, s->t + 1: %d\n", c, s->x, s->y + 1, I[sig[xy][sig_num]].to[i], s->t + 1);
if (s->t < T) {
//printf("s->t > T\n");
enQ(b, s->x + 1, s->y, I[sig[xy][sig_num]].to[i], s->t + 1);
}
}
}
else if (I[sig[xy][sig_num]].to[i] == 2){
if (s->y + 1 <= N){
//xy = (s->x - 1) + (s->y) * 3;
//printf("enQ 2: %d, into: %d\n", xy, I[sig[xy][s->t]].to[i]);
//printf("c: %p, s->x: %d, s->y + 1: %d, I[sig[xy][sig_num]].to[i]: %d, s->t + 1: %d\n", c, s->x, s->y + 1, I[sig[xy][sig_num]].to[i], s->t + 1);
if (s->t < T) {
//printf("s->t > T\n");
enQ(c, s->x, s->y + 1, I[sig[xy][sig_num]].to[i], s->t + 1);
}
}
}
else if (I[sig[xy][sig_num]].to[i] == 3){
if (s->x - 1 > 0){
//xy = (s->x - 2) + (s->y - 1) * 3;
//printf("enQ 3: %d, into: %d\n", xy, I[sig[xy][s->t]].to[i]);
if (s->t < T) {
//printf("s->t > T\n");
enQ(d, s->x - 1, s->y, I[sig[xy][sig_num]].to[i], s->t + 1);
}
}
}
else continue;
}
}
}
}
int main(void)
{
int N, T;
scanf("%d %d", &N, &T);
int N_squre = N * N;
int T_size = T + 1;
int N_size = N + 1;
int cnt = 0;
sig = (int**)malloc(sizeof(int*) * N_squre);
for (int i = 0; i < N_squre; i++){
sig[i] = (int*)malloc(sizeof(int) * (4));
}
for (int i = 0; i < N_squre; i++){
for (int j = 0; j < 4; j++){
scanf("%d", &sig[i][j]);
}
}
vst = (int**)malloc(sizeof(int*) * (N_size));
for (int i = 0; i < N_size; i ++){
vst[i] = (int*)malloc(sizeof(int) * (N_size));
}
for (int i = 0; i < N_size; i++){
for (int j = 0; j < N_size; j++){
vst[i][j] = 0;
}
}
Q_init();
I[1].into = 1;
I[1].len = 3;
I[1].to[0] = 0;
I[1].to[1] = 1;
I[1].to[2] = 2;
I[2].into = 0;
I[2].len = 3;
I[2].to[0] = 3;
I[2].to[1] = 0;
I[2].to[2] = 1;
I[3].into = 3;
I[3].len = 3;
I[3].to[0] = 0;
I[3].to[1] = 3;
I[3].to[2] = 2;
I[4].into = 2;
I[4].len = 3;
I[4].to[0] = 3;
I[4].to[1] = 2;
I[4].to[2] = 1;
I[5].into = 1;
I[5].len = 2;
I[5].to[0] = 0;
I[5].to[1] = 1;
I[5].to[2] = -1;
I[6].into = 0;
I[6].len = 2;
I[6].to[0] = 3;
I[6].to[1] = 0;
I[6].to[2] = -1;
I[7].into = 3;
I[7].len = 2;
I[7].to[0] = 2;
I[7].to[1] = 3;
I[7].to[2] = -1;
I[8].into = 2;
I[8].len = 2;
I[8].to[0] = 1;
I[8].to[1] = 2;
I[8].to[2] = -1;
I[9].into = 1;
I[9].len = 2;
I[9].to[0] = 1;
I[9].to[1] = 2;
I[9].to[2] = -1;
I[10].into = 0;
I[10].len = 2;
I[10].to[0] = 0;
I[10].to[1] = 1;
I[10].to[2] = -1;
I[11].into = 3;
I[11].len = 2;
I[11].to[0] = 0;
I[11].to[1] = 3;
I[11].to[2] = -1;
I[12].into = 2;
I[12].len = 2;
I[12].to[0] = 3;
I[12].to[1] = 2;
I[12].to[2] = -1;
Car* c;
if (I[sig[0][0]].into == 0){
for (int i = 0; i < I[sig[0][0]].len; i++){
//printf("I[sig[0][0]].to[i]: %d\n", I[sig[0][0]].to[i]);
if (1 == I[sig[0][0]].to[i]){
//printf("car into correct!: %d\n", I[sig[0][0]].into);
enQ(c, 1, 1, 0, 0);
}
}
BFS(T, N);
}
else {
//printf("%d", 0);
}
for (int i = 1; i < N_size; i++){
for (int j = 1; j < N_size; j++){
if (vst[j][i] > 0){
cnt++;
}
//printf("%d ", vst[j][i]);
}
//printf("\n");
}
printf("%d\n", cnt);
/*
for(int i = 0; i < N_squre; i++){
for (int j = 0; j < 4; j++){
printf("i: %d j: %d: sig: %d\n",i, j, sig[i][j]);
}
}
*/
free(Q);
delete_Q(head);
for (int i = 0; i < N_size; i ++){
free(vst[i]);
}
free(vst);
for (int i = 0; i < N_squre; i++){
free(sig[i]);
}
free(sig);
return 0;
}
#[hsat_1회_정기_코딩_인증평가_기출]_안전운전을_도와줄_차세대_지능형_교통시스템