제가 생각할 수 있는 모든 예제들을 넣어서 확인해 보았을 때 결과가 정상적으로 나오고 있는데
정답 제출만 하면 오답이라고 나오네요 T.T
혹시 어떤 부분이 잘못 된건지 알 수 있을까요?
제가 생각하는 기본 알고리즘은 주석으로 적었습니다...
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAX_ROWCOL 501
#define manual_input 0 // how to get the input
#define debug 0
int test_case;
int width, hight;
char bact_table[MAX_ROWCOL][MAX_ROWCOL];
int IsChanged = 0;
int seconds = 0;
int read_input();
int check_around(int row, int col);
void init_data();
int main() {
int result = 0;
result = read_input();
return result; // no meaning
}
int read_input() {
int result = 0, IsExtinct =1;
FILE * fp;
if (!manual_input) {
fp = fopen("input.txt", "rt");
if (fp == NULL)
return 1;
}
if(manual_input)
scanf("%d", &test_case); // input number of test case
else
fscanf(fp, "%d", &test_case);
for (int k = 0; k < test_case; k++) {
if (!manual_input)
fscanf(fp, "%d %d", &width, &hight);
else
scanf("%d %d", &width, &hight); // input W and H
for (int i = 0; i < hight; i++) {
if (!manual_input)
fscanf(fp, "%s", &bact_table[i]); // input bacterias
else
scanf("%s", &bact_table[i]);
}
if (debug) {// only for debug
for (int i = 0; i < hight; i++) {
for (int j = 0; j < width; j++) {
printf("%c", bact_table[i][j]);
}
printf("\n");
}
}
while (1) { // repeat until there are no change(dead)
IsChanged = 0;
IsExtinct = 1;
for (int i = 0; i < hight; i++) {
for (int j = 0; j < width; j++) {
result = check_around(i, j);
if (result)
IsExtinct = 0;
// IsExtinct is a flag to check initial state have bacteria or not
if (debug) printf("%d", result);
}
if (debug) printf("\n");
}
for (int i = 0; i < hight; i++) {
for (int j = 0; j < width; j++) {
if (bact_table[i][j] == '@') {
// if bacteria died just ago, replace @ to . again.
bact_table[i][j] = '.';
// and set IsChanged flag as true
IsChanged = 1;
}
}
}
if (debug) printf("IsChanged : %d, seconds %d ,IsExtinct %d\n", IsChanged, seconds, IsExtinct);
if (!IsChanged)
// stop if there are no change of bacteria life
break;
else
// increase seconds counter if there are changes of bacteria life
seconds++;
};
// In case of all bacteria extincted or initial state have no bacteria
if ((seconds && IsExtinct) || (!seconds && IsExtinct))
printf("%d\n", seconds); // print seconds( or 0)
else
printf("-1\n"); // In case of no baceria extincts
init_data(); // init variables
}
if (!manual_input)
fclose(fp);
return 0;
}
int check_around(int row, int col) {
int cnt =0, ret = 0;
if (bact_table[row][col] == '*') {
if (col)
if ((bact_table[row][col - 1] == '*')
|| (bact_table[row][col - 1] == '@')) // check left cell
cnt++;
if (col != (width - 1))
if ((bact_table[row][col + 1] == '*')
|| (bact_table[row][col + 1] == '@')) // check right cell
cnt++;
if (row)
if ((bact_table[row - 1][col] == '*')
|| (bact_table[row - 1][col] == '@')) // check upper cell
cnt++;
if (row != (hight -1))
if ((bact_table[row + 1][col] == '*')
|| (bact_table[row + 1][col] == '@')) //check below cell
cnt++;
ret = 1;
}
if ((cnt != 2) && (bact_table[row][col] != '.')) { // if it will die
bact_table[row][col] = '@'; // @ means just ago "dead"
}
return ret; // return 0 if it is not a bacteria
// return 1 if it is a bacteria
}
void init_data() {
for (int i = 0; i < MAX_ROWCOL; i++)
for (int j = 0; j < MAX_ROWCOL; j++) {
bact_table[i][j] = 0;
}
IsChanged = 0;
seconds = 0;
}
oasis876
제가 생각할 수 있는 모든 예제들을 넣어서 확인해 보았을 때 결과가 정상적으로 나오고 있는데
정답 제출만 하면 오답이라고 나오네요 T.T
혹시 어떤 부분이 잘못 된건지 알 수 있을까요?
제가 생각하는 기본 알고리즘은 주석으로 적었습니다...
11년 전