Skip to content

Instantly share code, notes, and snippets.

@jmkim
Last active May 11, 2016 06:43
Show Gist options
  • Save jmkim/b82cca4fdd0991063ff3b7b1c7a0b8cc to your computer and use it in GitHub Desktop.
Save jmkim/b82cca4fdd0991063ff3b7b1c7a0b8cc to your computer and use it in GitHub Desktop.
/* https://dl.dropboxusercontent.com/u/16142350/ProbSolving2016/chap03/practice01.pdf */
#include <stdio.h>
#define O_SIZE_MAX 100
#define O_PNONE 0
#define O_PBLACK 1
#define O_PWHITE 2
int bsize;
int bgame[O_SIZE_MAX][O_SIZE_MAX];
void
read_file(const char path[]);
int
best_position(int *out_x, int *out_y);
int
count_of_captured_stone(const int x, const int y);
int
count(const int x, const int y, const int dir);
int
main(void)
{
read_file("input.txt");
int x, y;
int bestcount = best_position(&x, &y);
printf("size: %d\nx: %d\ny: %d\ncount:%d\n", bsize, x, y, bestcount);
return 0;
}
void
read_file(const char path[])
{
FILE *fp = fopen(path, "r");
int i, j;
fscanf(fp, "%d", &bsize);
for(i = 0; i < bsize; ++i)
{
for(j = 0; j < bsize; ++j)
{
fscanf(fp, "%d", &bgame[i][j]);
printf("%d ", bgame[i][j]);
}
printf("\n");
}
}
int
best_position(int *out_x, int *out_y)
{
int bestcount = 0;
int i, j;
for(i = 0; i < bsize; ++i)
{
for(j = 0; j < bsize; ++j)
{
int bc = count_of_captured_stone(i, j);
if(bc > bestcount)
{
bestcount = bc;
*out_x = i;
*out_y = j;
}
}
}
return bestcount;
}
int
count_of_captured_stone(const int x, const int y)
{
if(bgame[x][y] != O_PNONE) return 0;
int captured_stone = 0;
int dir;
for(dir = 0; dir < 7; ++dir)
captured_stone += count(x, y, dir);
return captured_stone;
}
int
count(const int x, const int y, const int dir)
{
switch(dir)
{
case 0:
if(x < 0 || y < 0 || x > bsize || y+1 > bsize || bgame[x][y] != bgame[x][y+1]) return 0;
return 1 + count(x, y+1, dir);
case 1:
if(x < 0 || y < 0 || x-1 > bsize || y+1 > bsize || bgame[x][y] != bgame[x-1][y+1]) return 0;
return 1 + count(x-1, y+1, dir);
case 2:
if(x < 0 || y < 0 || x-1 > bsize || y > bsize || bgame[x][y] != bgame[x-1][y]) return 0;
return 1 + count(x-1, y, dir);
case 3:
if(x < 0 || y < 0 || x-1 > bsize || y-1 > bsize || bgame[x][y] != bgame[x-1][y-1]) return 0;
return 1 + count(x-1, y-1, dir);
case 4:
if(x < 0 || y < 0 || x > bsize || y-1 > bsize || bgame[x][y] != bgame[x][y-1]) return 0;
return 1 + count(x, y-1, dir);
case 5:
if(x < 0 || y < 0 || x+1 > bsize || y-1 > bsize || bgame[x][y] != bgame[x+1][y-1]) return 0;
return 1 + count(x+1, y-1, dir);
case 6:
if(x < 0 || y < 0 || x+1 > bsize || y > bsize || bgame[x][y] != bgame[x+1][y]) return 0;
return 1 + count(x+1, y, dir);
case 7:
if(x < 0 || y < 0 || x+1 > bsize || y+1 > bsize || bgame[x][y] != bgame[x+1][y+1]) return 0;
return 1 + count(x+1, y+1, dir);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment