Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this idea was from Evan Kohilas on Friday's help session (21/4/17)
- #include <stdio.h>
- #include "captcha.h"
- int no_neighbours(int x, int y, int height, int width, int n[height+2][width+2]);
- int find_lowest(int x, int y, int height, int width, int n[height+2][width+2]);
- int replace(int x, int y, int height, int width, int n[height+2][width+2], int to_be_replaced, int replacement);
- int merge(int x, int y, int height, int width, int n[height+2][width+2], int replacement);
- int no_neighbours(int x, int y, int height, int width, int n[height+2][width+2]) {
- return n[x][y+1]==0 && n[x-1][y]==0 && n[x][y-1]==0 && n[x+1][y]==0;
- }
- int find_lowest(int x, int y, int height, int width, int n[height+2][width+2]){ // this function finds the lowest number in the (surrounding) array
- int adj[4]={n[x][y+1], n[x-1][y], n[x][y-1], n[x+1][y]};//must handle the case when n[-1][0]
- int i=1;
- int min=adj[0];
- while(i < 4) {
- if(adj[i]>0 && adj[i]<min && adj[i]!=0) {
- min=adj[i];
- }
- i++;
- }
- return min;
- }
- //FOR TODAY-- NEED TO MAKE REPLACE FUNCTION
- // we need to scan the entire array again and replace every number in the array that is "replacement" with adj[i]
- int replace(int x, int y, int height, int width, int n[height+2][width+2], int to_be_replaced, int replacement){
- x=0;
- while(x < height-1) {
- y=0;
- while(y < width-1) {
- if(n[x][y]==to_be_replaced) {
- n[x][y]=replacement;
- }
- y++;
- }
- x++;
- }
- return replacement;
- }
- //function that finds numbers that aren't 0 or replacement
- int merge(int x, int y, int height, int width, int n[height+2][width+2], int replacement){
- int adj[4]={n[x][y+1], n[x-1][y], n[x][y-1], n[x+1][y]};
- int i=1;
- int min=adj[0];
- int to_be_replaced;
- int m=0;
- while(i < 4) {
- //n, 2, 1
- if(adj[i]>0 && adj[i]!=0 && adj[i]!=replacement) {
- to_be_replaced=adj[i];
- }
- i++;
- }
- if(n[x][y]==to_be_replaced) {
- replace(x, y, height, width, n, to_be_replaced, replacement)==n[x][y];
- m++;
- }
- return m;
- }
- int get_holes(int height, int width, int pixels[height][width]){
- int num=1; //replacement number
- int x, y;
- int replacement;
- int m=0; // number of total merges
- int number_of_holes;
- int to_be_replaced;
- printf("hello");
- int n[height + 2][width + 2];//new array where all numeros to 0 and has a border of zeroes around array
- for(x=0; x < height + 2; x++){
- for(y=0; y < width + 2; y++){
- n[x][y] = 0;
- }
- }
- printf("hello");
- x=1;
- while(x < height-1) {
- y=1;
- while(y < width-1) {
- //printf("%d\n %d\n", x, y);
- if (pixels[x][y]==0) {
- if(no_neighbours(x, y, height, width, n)) { //if everything around the pixel is zero, then change the pixel value to num, and num increments
- n[x][y]=num;
- num++;
- } else { //if surrounding pixels are not all zero, the pixel value becomes the lowest adjacent number
- replacement=find_lowest(x, y, height, width, n);
- merge(x, y, height, width, n, replacement);
- printf("lol");
- }
- } else {
- y++;
- }
- }
- x++;
- }
- number_of_holes = num - m - 2;
- printf("num is: %d\n", num);
- printf("m is: %d\n", m);
- printf("number of holes is: %d\n", number_of_holes);
- return number_of_holes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement