I made this backtracking recursive code.
this code shows every 4x4 array filled with 1, 2, 3, 4
but no duplication in every one row and line.
but this prints only one answers, what I expected is every answers.
#include <stdio.h>
#include <stdbool.h>
// initializing array
void init_arr(int arr[][4])
{
int x;
int y;
y = 0;
while (y < 4)
{
x = 0;
while (x < 4)
{
arr[y][x] = 0;
x++;
}
y++;
}
}
// check about promising correct
bool promising(int arr[][4], int x, int y)
{
int i;
i = 0;
while (i < 4)
{
if (arr[y][i] == arr[y][x] && i != x)
return (0);
i++;
}
i = 0;
while (i < 4)
{
if (arr[i][x] == arr[y][x] && i != y)
return (0);
i++;
}
return (1);
}
// recursive function
void fill_arr(int arr[][4], int x, int y)
{
int n;
if (x == 0 && y == 0)
init_arr(arr);
if (y == 4)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
printf("\n");
return ;
}
else if (x == 4)
fill_arr(arr, 0, y + 1);
else
{
n = 1;
while (n < 5)
{
arr[y][x] = n;
if (promising(arr, x, y))
fill_arr(arr, x + 1, y);
n++;
}
}
}
int main(void)
{
int arr[4][4];
fill_arr(arr, 0, 0);
return (0);
}
when I put printf in if(promising), this comes out
it looks like some variables are not initializing, but when I put init function to every other line, it getting messier.
Your fill_arr() isn't cleaning up before it leaves.
Add this line before the final brace:
arr[y][x] = 0;
Related
In line 10 I cannot find out where my problem is at first. I place int a[100][100]={0} but the cpu speed is stuck.
Then, I try to change it into a[n][n] but no output is shown.
Last, I try to change it again as if it resembles the original ones.
However, nothing works instead of a new question.
#include<stdio.h>
int main() {
int n;
while (scanf("%d", &n)) {
n *= 2;
int x = 0, y = 0, num = 1;
int a[n][n] = {0};
a[x][y] = num++;
while (n * n >= num) //定義陣列
{
while (y + 1 < n && !a[x][y + 1]) //向右
a[x][++y] = num++;
while (x + 1 < n && !a[x + 1][y]) //向下
a[++x][y] = num++;
while (y - 1 >= 0 && !a[x][y - 1]) //向左
a[x][--y] = num++;
while (x - 1 >= 0 && !a[x - 1][y]) //向上
a[--x][y] = num++;
}
for (x = 0; x < n; x++) //print 陣列
{
for (y = 0; y < n; y++) {
if (y != n - 1) {
printf("%d ", a[x][y]);
} else {
printf("%d", a[x][y]);
}
}
printf("\n");
}
break;
}
return 0;
}
At least this problem:
Variable Length Arrays (VLA) cannot be initialized via the C standard.
Alternate, assign via memset() after defining a.
// int a[n][n]={0};
int a[n][n];
memset(a, 0, sizeof a);
As you see on the image there are no * in lower part of the circle. Why is that?
Equations in the loops: y = 10 - i , x = j-10 or k-10
Circle formula = r^2 = (x-a)^2 + (y-b)^2
int i, j, k;
for (i = 0; i < 21; i++) {
if (i == 10) {
for (j = 0; j < 21; j++) {
if (pow(r * r - (j -10 -a)*(j -10 -a), 0.5) + b == 0) {
printf("*");
}
else if (j == 10) {
printf("|");
}
else {
printf("-");
}
}
}
else {
for (k = 0; k < 21; k++) {
if (pow(r * r - (k -10 -a)*(k -10 -a), 0.5) + b == 10 - i) {
printf("*");
}
else if (k == 10) {
printf("|");
}
else {
printf(" ");
}
}
}
printf("\n");
}
enter image description here
You program should be more like this:
#include <stdio.h>
#include <math.h>
int main() {
int i, j, k;
int a = 3, b = 4, r = 5;
int x, y;
for (i = 0; i < 21; i++) {
y = -(i - 10);
if (y == 0) {
for (j = 0; j < 21; j++) {
x = j - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf("-");
}
}
}
else {
for (k = 0; k < 21; k++) {
x = k - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(" ");
}
}
}
printf("\n");
}
}
When you match your equation, you should allow for (-3) ^ 2 is 9, while if you take the square root of 9, you could only match a 3, unless if you also check whether the "minus" of the square root is a 3, which is troublesome.
It might also be good that if you use x and y and do the calculations, and then at the last moment, map to the screen's method of displaying it, or a similar way, instead of using i and then -(i - 10) or 10 - i every where to mean y, so I added the x and y in your program. You might also comment that (a, b) is supposed to be the center of the circle.
There is also one point about, it seems the difference between y being 0 or not is that you print out the hyphen (for the x-axis), vs a space, so your program could be:
#include <stdio.h>
#include <math.h>
int main() {
int i, j;
int a = 3, b = 4, r = 5;
int x, y;
for (i = 0; i < 21; i++) {
y = -(i - 10);
for (j = 0; j < 21; j++) {
x = j - 10;
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(y == 0 ? "-" : " ");
}
}
printf("\n");
}
}
and in fact, you don't need i and j, but can directly iterate through x and y, as your way of displaying is only one character each time:
#include <stdio.h>
#include <math.h>
int main() {
int a = 3, b = 4, r = 5;
int x, y;
for (y = 10; y >= -10; y--) {
for (x = -10; x <= 10; x++) {
if (r * r == pow(x - a, 2) + pow(y - b, 2)) {
printf("*");
}
else if (x == 0) {
printf("|");
}
else {
printf(y == 0 ? "-" : " ");
}
}
printf("\n");
}
}
I need to find the sum of all numbers that are less or equal with my input number (it requires them to be palindromic in both radix 10 and 2). Here is my code:
#include <stdio.h>
#include <stdlib.h>
int pal10(int n) {
int reverse, x;
x = n;
while (n != 0) {
reverse = reverse * 10 + n % 10;
n = n / 10;
}
if (reverse == x)
return 1;
else
return 0;
}
int length(int n) {
int l = 0;
while (n != 0) {
n = n / 2;
l++;
}
return l;
}
int binarypal(int n) {
int v[length(n)], i = 0, j = length(n);
while (n != 0) {
v[i] = n % 2;
n = n / 2;
i++;
}
for (i = 0; i <= length(n); i++) {
if (v[i] == v[j]) {
j--;
} else {
break;
return 0;
}
}
return 1;
}
int main() {
long s = 0;
int n;
printf("Input your number \n");
scanf("%d", &n);
while (n != 0) {
if (binarypal(n) == 1 && pal10(n) == 1)
s = s + n;
n--;
}
printf("Your sum is %ld", s);
return 0;
}
It always returns 0. My guess is I've done something wrong in the binarypal function. What should I do?
You have multiple problems:
function pal10() fails because reverse is not initialized.
function binarypal() is too complicated, you should use the same method as pal10().
you should avoid comparing boolean function return values with 1, the convention in C is to return 0 for false and non zero for true.
you should avoid using l for a variable name as it looks very similar to 1 on most constant width fonts. As a matter of fact, it is the same glyph for the original Courier typewriter font.
Here is a simplified and corrected version with a multi-base function:
#include <stdio.h>
#include <stdlib.h>
int ispal(int n, int base) {
int reverse = 0, x = n;
while (n > 0) {
reverse = reverse * base + n % base;
n = n / base;
}
return reverse == x;
}
int main(void) {
long s = 0;
int n = 0;
printf("Input your number:\n");
scanf("%d", &n);
while (n > 0) {
if (ispal(n, 10) && ispal(n, 2))
s += n;
n--;
}
printf("Your sum is %ld\n", s);
return 0;
}
in the function pal10 the variable reverse is not initialized.
int pal10(int n)
{
int reverse,x;
^^^^^^^
x=n;
while(n!=0)
{
reverse=reverse*10+n%10;
n=n/10;
}
if(reverse==x)
return 1;
else
return 0;
}
In the function binarypal this loop is incorrect because the valid range of indices of an array with length( n ) elements is [0, length( n ) - 1 ]
for(i=0;i<=length(n);i++)
{
if(v[i]==v[j])
{
j--;
}
else
{
break;
return 0;
}
}
And as #BLUEPIXY pointed out you shall remove the break statement from this else
else
{
break;
return 0;
}
I made a working sudoku solver using a basic backtracking algorithm.
It works reasonably well even though there are many optimizations to be done.
I tried modifying my code to return the total number of solutions for a given sudoku grid. To do this I simply changed the solving function to add up every possibility instead of stopping at one.
However I only get 1 or 0.
Here is the code for the basic solver:
int check_row(char **tab, int y, int n)
{
int i;
i = 0;
while (i < 9)
{
if (tab[y][i] == n + '0')
return (0);
i++;
}
return (1);
}
int check_column(char **tab, int x, int n)
{
int j;
j = 0;
while (j < 9)
{
if (tab[j][x] == n + '0')
return (0);
j++;
}
return (1);
}
int check_square(char **tab, int x, int y, int n)
{
int i;
int j;
i = (x / 3) * 3;
while (i < (x / 3) * 3 + 3)
{
j = (y / 3) * 3;
while (j < (y / 3) * 3 + 3)
{
if (tab[j][i] == n + '0')
return (0);
j++;
}
i++;
}
return (1);
}
int solve(char **tab, int x, int y)
{
int n;
if (y >= 9 || x >= 9)
return (1);
if (tab[y][x] == '.')
{
n = 1;
while (n < 10)
{
if (check_row(tab, y, n) && check_column(tab, x, n)
&& check_square(tab, x, y, n))
{
tab[y][x] = n + '0';
if (solve(tab, (x + 1) % 9, y + ((x + 1) / 9)))
return (1);
}
n++;
}
tab[y][x] = '.';
return (0);
}
else
return (solve(tab, (x + 1) % 9, y + ((x + 1) / 9)));
}
And here is the modified function that should count the solutions:
int solve_count(char **tab, int x, int y)
{
int n;
int count;
count = 0;
if (y >= 9 || x >= 9)
return (1);
if (tab[y][x] == '.')
{
n = 1;
while (n < 10)
{
if (check_row(tab, y, n) && check_column(tab, x, n)
&& check_square(tab, x, y, n))
{
tab[y][x] = n + '0';
count += solve_count(tab, (x + 1) % 9, y + ((x + 1) / 9));
}
n++;
}
tab[y][x] = '.';
return (count);
}
else
return (solve_count(tab, (x + 1) % 9, y + ((x + 1) / 9)));
}
The main() and helper functions are as follows:
#include <unistd.h>
int solve(char **tab, int x, int y);
int solve_count(char **tab, int x, int y);
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
int i;
i = 0;
while (*(str + i) != '\0')
{
ft_putchar(*(str + i));
i++;
}
}
void ft_putnbr(int n)
{
int i;
int vect[20];
long nb;
nb = n;
i = -1;
if (nb < 0)
{
ft_putchar('-');
nb = -nb;
}
if (nb == 0)
ft_putchar('0');
while (nb > 0)
{
i++;
vect[i] = nb % 10;
nb = nb / 10;
}
while (i > -1)
{
ft_putchar('0' + vect[i]);
i--;
}
}
int ft_check_input(int argc, char **argv)
{
int i;
int j;
i = 1;
j = 0;
if (argc != 10)
return (1);
while (i < argc)
{
while (argv[i][j])
j++;
if (j != 9)
return (1);
j = 0;
while (argv[i][j] == '.' || (argv[i][j] > '0' && argv[i][j] <= '9'))
j++;
if (j != 9)
return (1);
j = 0;
i++;
}
if (i != 10)
return (1);
else
return (0);
}
void ft_print_sudoku(char **tab)
{
int i;
int j;
i = 1;
j = 0;
while (i < 10)
{
while (j < 9)
{
ft_putchar(tab[i][j]);
if (j < 8)
ft_putchar(' ');
j++;
}
ft_putchar('\n');
j = 0;
i++;
}
}
int main(int argc, char **argv)
{
if (ft_check_input(argc, argv))
ft_putstr("Error: not a good sudoku\n");
else
{
if (solve(argv + 1, 0, 0))
{
ft_print_sudoku(argv);
ft_putnbr(solve_count(argv + 1, 0, 0));
}
else
ft_putstr("Error: no solution\n");
}
return (0);
}
To get the number of solutions for an empty sudoku you would run ('.' means empty item):
./sudoku "........." "........." "........." "........." "........." "........." "........." "........." "........."
It runs, but still stops at the first solution it finds, and returns 1.
What am I missing? I've been scratching my head for a while now.
Eventually I'm thinking of using this function to create a grid by adding random numbers until there's just one solution.
I Did this a long time ago for fun...
What I did to Solve the most difficult ones was to return for each squares, All possible numbers
And then destroy each possible numbers one by one for each grid...
so even if you get 9 possibilities for the first grid you enter the first and if it doesn't fit. you delete it and try the second.
One of them needs too fit :)
To know how may possible solutions to a soduku puzzle exists that would take a brute force calculation.
In a contest, they asked to write a C function which returns the minimum distance between X and Y in the given array, where X and Y are elements of the array Provided X AND Y ARE Distinct.
If have written a piece of code, but that code runs into many if's and else's,
My code (Has Some Bugs):
int getMinXYDist(int arr[],int n,int x,int y){
int i,flag = 0,ele = -1 ,dist = 0;
int minDist = 1000; // SETTING minDist TO MAX VALUE.
for( i = 0 ; i< n; i++)
if(arr[i] == x || arr[i] == y){
if(flag == 0){
flag = 1;
ele = arr[i]==x?x:y;
dist = 0;
}
else{
if(ele == x ){
if(arr[i] == y){
minDist = dist < minDist ? dist : minDist;
dist = 0;
ele = y;
}
else //if(arr[i] == x){
dist = 0;
}
else { //if(ele == y)
if(arr[i] == x){
minDist = dist < minDist ? dist : minDist;
dist = 0;
ele = x;
}
}
}
}
else {
if(flag == 1)
dist++;
}
return minDist;
}
void main(){
int arr = {6,1,5,1,8,6,3,4};
printf("\n%d" ,getMinXYDist(arr,sizeof(arr)/sizeof(int),6,5) ); //Must return 2.
}
Could Any one suggest a smarter way [ Just as in O(n) time complexity ] of calculating the distance?
If x or y is found, record the index it was found at. Once both have been found, each time you find either, compute distance to the last index containing the other value. Update the minimum value if the distance is lower than the previous minimum.
int getMinXYDist(int arr[],int n,int x,int y)
{
int i, indexX, indexY;
int foundX = 0;
int foundY = 0;
int curDist;
int minDist = n;
for (i = 0; i < n; i++)
{
if (arr[i] == x)
{
foundX = 1;
indexX = i;
if (foundX && foundY)
{
curDist = indexX - indexY;
if (curDist < minDist)
{
minDist = curDist;
}
}
}
else if (arr[i] == y)
{
foundY = 1;
indexY = i;
if (foundX && foundY)
{
curDist = indexY - indexX;
if (curDist < minDist)
{
minDist = curDist;
}
}
}
}
return minDist;
}
Basically, I think OP's solution is already optimum, the lower bound for this algorithm is n steps, i.e., done in one iteration.
// if -1 is returned, then none of x and y are in the array
// if n is returned, then one of x and y is not in the array
// otherwise, mindist(x, y) is returned.
int test(int v[], int n, int x, int y)
{
int flag = -1;
int i, a = -1, b = -1, dist = n;
for (i = 0; i < n; ++i) {
if (v[i] == x) {
flag = 0;
a = i;
break;
} else if (v[i] == y) {
flag = 1;
b = i;
break;
}
}
if (flag < 0) return -1; // x and y are both not in array;
for (++i; i < n; ++i) {
if (v[i] == x) {
if (0 == flag) a = i;
else {
flag = 0;
if (i - b < dist) dist = i - b;
a = i;
}
} else if (v[i] == y) {
if (1 == flag) b = i;
else {
flag = 1;
if (i - a < dist) dist = i - a;
b = i;
}
}
}
return dist;
}
int minDistance ( int arr[], int n, int x, int y) {
if(x == y) return 0;
int index1 = -1;
int index2 = -1;
int minvalue = n;
for(int i = 0 ; i < n; i++){
if((arr[i] == x) && ((i-index2) < minvalue)){
index1 = i;
if( index2 != -1)minvalue = i-index2;
}else if((arr[i] == y) && ((i-index1) < minvalue)){
index2 = i;
if( index1 != -1)minvalue = i-index1;
}
}
return minvalue;
}
where
n: size of array.
x and y: two input number of array.
If minvalue returned is n then x or y is not present in array.
complexity: O(n), One Pass.