반응형
https://www.algospot.com/judge/problem/read/BADUK2
상, 하, 좌, 우의 탐색을 스택을 사용하여 해결함
Trouble Shooting
1. 탐색과정에서 주변 바둑색과 현재 바둑색이 다를 경우 탐색을 중단했지만 내부적으로 빈 공간을 모두 검색해야 하기 때문에 해당 부분을 삭제함으로써 해결했다.
#include<stdio.h>
#define MAX 23
int B, W, M;
int func2(int grid[MAX][MAX], int n, int i, int j)
{
int m_queue[20 * 20 * 4];
int m_queue_end = 0;
int count = 0;
m_queue[m_queue_end++] = i*MAX + j;
grid[i][j] = -1;
++count;
M = 0;
while (m_queue_end > 0)
{
int coord = m_queue[--m_queue_end];
int c = coord / MAX, r = coord%MAX;
if (c - 1 >= 0){
if (grid[c - 1][r] == 0){
m_queue[m_queue_end++] = (c - 1)*MAX + r;
grid[c - 1][r] = -1;
++count;
}
else if (grid[c - 1][r] != -1 && M == 0)
M = grid[c - 1][r];
else if (grid[c - 1][r] != -1 && M != grid[c - 1][r])
M = 3; // fail
}
if (c + 1 <= n + 1){
if (grid[c + 1][r] == 0){
m_queue[m_queue_end++] = (c + 1)*MAX + r;
grid[c + 1][r] = -1;
++count;
}
else if (grid[c + 1][r] != -1 && M == 0)
M = grid[c + 1][r];
else if (grid[c + 1][r] != -1 && M != grid[c + 1][r])
M = 3; // fail
}
if (r - 1 >= 0){
if (grid[c][r - 1] == 0){
m_queue[m_queue_end++] = c*MAX + (r - 1);
grid[c][r - 1] = -1;
++count;
}
else if (grid[c][r - 1] != -1 && M == 0)
M = grid[c][r - 1];
else if (grid[c][r - 1] != -1 && M != grid[c][r - 1])
M = 3; // fail
}
if (r + 1 <= n + 1){
if (grid[c][r + 1] == 0){
m_queue[m_queue_end++] = c*MAX + (r + 1);
grid[c][r + 1] = -1;
++count;
}
else if (grid[c][r + 1] != -1 && M == 0)
M = grid[c][r + 1];
else if (grid[c][r + 1] != -1 && M != grid[c][r + 1])
M = 3; // fail
}
}
if (M == 1)
B += count;
else if (M == 2)
W += count;
return 1;
}
void func(int grid[MAX][MAX], int n)
{
int i, j;
for (i = 1; i < n + 1; i++)
for (j = 1; j < n + 1; j++)
{
if (grid[i][j] == 0)
{
func2(grid, n, i, j);
}
}
}
int main()
{
int n, w, b;
int grid[MAX][MAX];
for (scanf("%d", &n); n != 0; scanf("%d", &n))
{
int i, j;
scanf("%d %d", &b, &w);
B = 0; W = 0;
for (i = 0; i < n + 2; i++)
for (j = 0; j < n + 2; j++)
{//Initialize grid
if (i == 0 || //outline coordinate
j == 0 ||
i == n + 1 ||
j == n + 1)grid[i][j] = -1;
else //empty coordinate
grid[i][j] = 0;
}
// our gird is 0| < 1 .. n > |n+1
for (i = 0; i < b; i++)
{//input b coordinate INFO
int r, c;
//black stone '1'
scanf("%d %d", &r, &c); grid[r][c] = 1;
}
for (i = 0; i < w; i++)
{//input w coordinate INFO
int r, c;
//white stone '2'
scanf("%d %d", &r, &c); grid[r][c] = 2;
}
func(grid, n);
if (B > W)
printf("Black wins by %d\n", B - W);
else if (B < W)
printf("White wins by %d\n", W - B);
else
printf("Draw\n");
}
return 0;
}
반응형