[ create a new paste ] login | about

Link: http://codepad.org/BB64ktzn    [ raw code | output | fork ]

C, pasted on Dec 5:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define N 5
char gDt[N][N];

void Disp(void);
int Func(void);

int main(void) {
  int seed;
  int x, y, i;
  memset(gDt, '.', sizeof(gDt));
  seed = time(NULL);
/*  seed = 31415; */
  printf("seed : %d \n", seed);
  srand(seed);
  for (i = 0; i < 2 * N; i++) {
    x = (rand() /* >>3 */) % N;
    y = (rand() /* >>3 */) % N;
    gDt[y][x] = '#';
  }
  Disp();
  printf("Ans : %d\n",Func());
  return 0;
}

void check_island(int x, int y) {
  if (gDt[y][x] == '#') {
    gDt[y][x] = 'o';
    if (x > 0) /* left */
      check_island(x - 1, y);
    if (x < N - 1) /* right */
      check_island(x + 1, y);
    if (y > 0) /* up */
      check_island(x, y - 1);
    if (y < N - 1) /* down */
      check_island(x, y + 1);
  }
}

int Func(void) {
  int count;
  count = 0;
  for (;;) {
    int x, y;
    for (x = 0; x < N; x++)
      for (y = 0; y < N; y++)
        if (gDt[y][x] == '#')
          goto jump;
  jump:
    if (x == N && y == N)
      break;
    check_island(x, y);
    count++;
  }
  return count;
}

void Disp(void) {
  int x,y;
  for (y = 0; y < N; y++) {
    for (x = 0; x < N; x++) {
      printf("%c",gDt[y][x]);
    }
    putchar('\n');
  }
}
/* end */


Output:
1
2
3
4
5
6
7
seed : 1291562075 
.#...
##..#
.##..
....#
..#.#
Ans : 4


Create a new paste based on this one


Comments: