我需要帮助这个有二分搜索的项目

时间:2017-05-28 11:46:46

标签: c

基本上我需要找到一个重复两次或更多次的模式,打印它然后程序结束,我需要尽快找到解决方案:)

这只是为了填补空白并满足网站所以我可以发布问题。

这是测试用例: 输入:

7 8 342174762134751279

#。#... ##

##。###。

##### ..

。##。##。#

。#... ##。

...#####

##。## ...

输出:

..#。#..#

#### ..#

###。#..#

#。#。#。#。

## .. ##。#

#### ..#

#### ..#。

这是我目前的代码

 #include<stdio.h>
 #include<stdlib.h>
 #include<malloc.h>


 void fill(char ** arr, int i, int j, int up, int upright, int right, int downright, int down, int downleft, int left, int upleft);
 void display(char ** arr, int rows, int col);

 int main()
 {
       int rows, col;
       unsigned long long int days, k;
       int i, j;
       unsigned long long int *binaryNumbers;
       int a, b;
       int check_i = 0;
   int check_j = 0;
   char ch;
scanf("%d %d %llu", &rows, &col, &days);
if (rows <2)
    return 0;
char ** arr = (char **)malloc(sizeof(char*)* rows);
char ** temp_arr = (char **)malloc(sizeof(char*)* rows);
binaryNumbers = malloc(sizeof(unsigned long long int) * 10737418);
for (i = 0; i<rows; i++)
{
    arr[i] = (char*)malloc(sizeof(char)* col);
}
for (i = 0; i<rows; i++)
{
    temp_arr[i] = (char*)malloc(sizeof(char)* col);
}
for (i = 0; i<rows; i++)
{
    for (j = 0; j<col; j++)
    {
        scanf("%c", &ch);
        if (ch != '\n')
        {
            arr[i][j] = ch;
        }
        else
        {
            j--;
        }
    }
}
for (k = 0; k<days; k++)
{
    for (i = 0; i<rows; i++)
    {
        for (j = 0; j<col; j++)
        {
            if (arr[i][j] == '#')
            {
                check_i = i;
                check_j = j;
            }
        }
    }
    for (i = 0; i<rows; i++)
    {
        for (j = 0; j<col; j++)
        {
            temp_arr[i][j] = arr[i][j];
        }
    }

    for (i = 0; i<rows; i++)
    {
        for (j = 0; j<col; j++)
        {
            if (i == 0)//very first row and more then 1 rows
            {
                if (j == 0) //very first column
                {
                    fill(arr, i, j, 0, 0, 1, 0, 1, 0, 0, 0);
                }
                else if (j >0 && j != col - 1) //not very first column and 
       not last column too
                {
                    //check for right,left,down,down left
                    fill(arr, i, j, 0, 0, 1, 0, 1, 1, 1, 0);

                }
                else if (j == col - 1) // very last column
                {
                    //check for down,down left,left
                    fill(arr, i, j, 0, 0, 0, 0, 1, 1, 1, 0);

                }
            }
            if (i > 0 && i != rows - 1 && i % 2 != 0) // not very first row and not last. Also rows are more than 1. ROW IS ODD
            {
                //printf("\n if(i > 0 && i != rows -1 && i%2 != 0) %d \n",i);
                if (j == 0) //very first column
                {
                    //check for up, upright, right, downright, down
                    fill(arr, i, j, 1, 1, 1, 1, 1, 0, 0, 0);

                }
                else if (j >0 && j != col - 1) //not very first column and not last column too
                {
                    //check for up, upright, right, downright, down, left
                    fill(arr, i, j, 1, 1, 1, 1, 1, 0, 1, 0);

                }
                else if (j == col - 1) // very last column
                {
                    //check for up, down, left
                    fill(arr, i, j, 1, 0, 0, 0, 1, 0, 1, 0);

                }
            }
            if (i > 0 && i != rows - 1 && i % 2 == 0)//not very first row and not last row,rows are more then 1 and row is even
            {

                if (j == 0)
                {

                    fill(arr, i, j, 1, 0, 1, 0, 1, 0, 0, 0);
                }
                else if (j >0 && j != col - 1) //not very first column and not last column too
                {
                    //check for upleft, up, right, down, downleft, left
                    fill(arr, i, j, 1, 0, 1, 0, 1, 1, 1, 1);

                }
                else if (j == col - 1) // very last column
                {
                    //check for up, down, downleft. left, upleft
                    fill(arr, i, j, 1, 0, 0, 0, 1, 1, 1, 1);

                }
            }
            if (i == rows - 1 && i % 2 != 0) // last row and row is odd
            {
                if (j == 0)
                {
                    //check for up, upright, right
                    fill(arr, i, j, 1, 1, 1, 0, 0, 0, 0, 0);
                }
                else if (j >0 && j != col - 1) //not very first column and not last column too
                {
                    //check for up, upright, right, left
                    fill(arr, i, j, 1, 1, 1, 0, 0, 0, 1, 0);
                }
                else if (j == col - 1) // very last column
                {
                    //check for up, left
                    fill(arr, i, j, 1, 0, 0, 0, 0, 0, 1, 0);
                }
            }
            if (i == rows - 1 && i % 2 == 0)
            {
                if (j == 0)
                {
                    //check for up, right
                    fill(arr, i, j, 1, 0, 1, 0, 0, 0, 0, 0);
                }
                else if (j >0 && j != col - 1)
                {
                    //check for up, right, left, upleft
                    fill(arr, i, j, 1, 0, 1, 0, 0, 0, 1, 1);
                }
                else if (j == col - 1)
                {
                    //check for up, left, upleft
                    fill(arr, i, j, 1, 0, 0, 0, 0, 0, 1, 1);
                }
            }
        }
    }
    //printf("End\n");
    for (a = 0; a<rows; a++)
    {
        for (b = 0; b<col; b++)
        {
            if (arr[a][b] == '$' || arr[a][b] == '*')
                arr[a][b] = '#';
            else if (arr[a][b] == '#')
                arr[a][b] = '.';
        }

    }
    binaryNumbers[k] = 0;
    for (a = 0; a<rows; a++)
    {
        for (b = 0; b<col; b++)
        {
            if (arr[a][b] == '#')
            {
                binaryNumbers[k] = binaryNumbers[k] * 0b10;
                binaryNumbers[k] ^= 1;
            }
            else
            {
                binaryNumbers[k] = binaryNumbers[k] * 0b10;
            }
        }

    }
    for (a = 0; a<k; a++)
    {
        if (binaryNumbers[k] == binaryNumbers[a])
        {
            display(temp_arr, rows, col);
            return 0;
        }
    }
}
display(arr, rows, col);
return 0;
 }
   void display(char ** arr, int rows, int col)
{
int i, j;
printf("\n");
for (i = 0; i<rows; i++)
{
    for (j = 0; j<col; j++)
    {
        printf("%c", arr[i][j]);
    }
    printf("\n");
}
    }
     void fill(char ** arr, int i, int j, int up, int upright, int right, int downright, int down, int downleft, int left, int upleft)
    {
int count;
count = 0;
if (up == 1)
{
    if (arr[i - 1][j] == '#' || arr[i - 1][j] == '*')   //up
        count++;
}
if (upright == 1)
{
    if (arr[i - 1][j + 1] == '#' || arr[i - 1][j + 1] == '*') //upright
        count++;
}
if (right == 1)
{
    if (arr[i][j + 1] == '#' || arr[i][j + 1] == '*') //right
        count++;
}
if (downright == 1)
{
    if (arr[i + 1][j + 1] == '#' || arr[i + 1][j + 1] == '*') //downright
        count++;
}
if (down == 1)
{
    if (arr[i + 1][j] == '#' || arr[i + 1][j] == '*') //down
        count++;
}
if (downleft == 1)
{
    if (arr[i + 1][j - 1] == '#' || arr[i + 1][j - 1] == '*') //downleft
        count++;
}
if (left == 1)
{
    if (arr[i][j - 1] == '#' || arr[i][j - 1] == '*') //left
        count++;
}
if (upleft == 1)
{
    if (arr[i - 1][j - 1] == '#' || arr[i - 1][j - 1] == '*')
        count++;
}
if (count % 2 != 0)
{
    if (arr[i][j] == '#')
    {
        arr[i][j] = '*';
    }
    else
    {
        arr[i][j] = '$';
    }
}
  }

0 个答案:

没有答案