Hello Friends, I was trying to make Triplet form of a sparse matrix using pointers and DMA but the output in first row is returning garbage values can anyone help me in resolving this issue.
int main(){
int k=0,z=0,arr[6][6]={
{15,0,0,22,0,-15},
{0,11,3,0,0,0},
{0,0,0,-6,0,0},
{0,0,0,0,0,0},
{91,0,0,0,0,0},
{0,0,28,0,0,0}
};
int **trip=new int *[3];
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
if (arr[i][j]==0){
z++;
}
else{
trip[k]=new int(1);
trip[k][0]=i;
trip[k][1]=j;
trip[k][2]=arr[i][j];
k++;
}
}
}
}
Output-
-65511600 22028 -65511568
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28
Expected Output
0 0 15
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28
Related
The problem: I need to print the Pascal triangle for any (unsigned int) input passed as a command line argument. All the values must be stored in a LINEAR array and elements must only be manipulated as dereferenced pointers. Following this, the array elements must printed as a lower triangular matrix and subsequently deleted. My implementation functions perfectly for input ranging from 0 to 12 but produces spurious results for higher values.
I tried two different implementations.
Declare a pointer to an array of size (n+1)*(n+2)/2 (which is the number of elements in the triangle for input 'n'). Assign/print variables within a nested loop. Delete the pointer once both loops have been executed.
Run a nested loop, 0 <= i <= n, and 0 <= j <= i. Declare a pointer to an array of size (i+1) in the outer loop. Assign/print elements in the inner loop. Delete the pointer once the inner loop has been executed.
// VERSION 1
unsigned N = (n+1)*(n+2)/2;
unsigned* elements = new unsigned[N];
for(i = 0; i <= n; i++) {
for(j = 0; j <= i; j++) {
*(elements + j+(i*i+i)/2) = fact(i) / (fact(j) * fact(i-j));
// print statement
}
cout << endl;
}
delete [] elements;
// VERSION 2
for(i = 0; i <= n; i++) {
unsigned* elements = new unsigned[i+1];
for(j = 0; j <= i; j++) {
*(elements + j) = fact(i) / (fact(j) * fact(i-j));
// print statement
}
delete [] elements;
cout << endl;
}
Both these versions were tried separately on Xcode. In both cases, the triangle printed correctly until the 12th layer, i.e. n=12, but generated incorrect results for higher values.
0 | 1
1 | 1 1
2 | 1 2 1
3 | 1 3 3 1
4 | 1 4 6 4 1
5 | 1 5 10 10 5 1
6 | 1 6 15 20 15 6 1
7 | 1 7 21 35 35 21 7 1
8 | 1 8 28 56 70 56 28 8 1
9 | 1 9 36 84 126 126 84 36 9 1
10 | 1 10 45 120 210 252 210 120 45 10 1
11 | 1 11 55 165 330 462 462 330 165 55 11 1
12 | 1 12 66 220 495 792 924 792 495 220 66 12 1
13 | 1 4 24 88 221 399 532 532 399 221 88 24 4 1
14 | 1 0 1 5 14 29 44 50 44 29 14 5 1 0 1
15 | 1 1 0 0 2 4 7 9 9 7 4 2 0 0 1 1
16 | 1 0 0 0 0 4 0 1 1 1 0 4 0 0 0 0 1
The debugger, to the extent that I can use it, produced no error messages.
What is happening and how do I fix it?
fact(i) overflows really fast. I haven't checked the numbers, but I'm pretty sure that's what's happening.
Instead, use the fact that a number in Pascal's triangle is the sum of the two numbers above it.
Wikipedia has a nice animation for this.
When i is 13, fact(i) is 6227020800, which is too big to fit in a 32-bit unsigned integer, so integer overflow occurs.
I wrote an implementation of the following tutorial: LINK
Basically, since C/C++ does not have BIG Integer we are storing the factorial decimal values in an array. This is equivalent to writing a multiplication that performs the multiplication kids are taught at schools.
Problem: It works fine for values up to 17! after that (18!, 19!,... ) it does not output correct values.
#include <iostream>
using namespace std;
int main(){
int fact[1000]={1};
int n; scanf("%d", &n); //n are the number of factorials we will calculate
while(n--){
int number; scanf("%d", &number); //scan the number
if(number == 0) printf("%d", 1);
int flag = number;
int index = 0, length = 0;
//following lines we find the length of the entered number
while(flag!=0){
fact[index] = flag%10;
flag /= 10;
index++; length++;
}
//following lines are the multiplication code
while(number>1){
index = 0;
int temp = 0;
number--;
for(index = 0; index<length; index++){
int x = (fact[index] * number) + temp;
fact[index] = x%10;
temp = x/10;
}
//here we append the carry over left from multiplication
while(temp){
fact[index] = temp%10;
temp /= 10;
length++;
}
}
//print the array from most to least significant digit
for(int i = length-1; i>=0; i--){
printf("%d", fact[i]);
}
printf("\n");
}
return 0;
}
For a start, you need to be very careful with:
long long int x = (fact[index] * number) + temp;
Since fact[], number and temp are all int types, the calculation will be done as an int and only widened to a long long when placing the value into x.
You would be better off with:
long long x = fact[index];
x *= number;
x += temp;
That way, it becomes a long long early enough that the calculations will be done with that type.
However, that doesn't actually fix your problem, so let's modify your code a little to see where the problem lies:
#include <iostream>
using namespace std;
int main(){
int fact[1000]={1};
int n = 18, numberx = 0;
while(n-- > 0){
int number = ++numberx;
if(number == 0) { printf("%d", 1); continue; }
int flag = number;
int index = 0, length = 0;
//following lines we find the length of the entered number
while(flag!=0){
fact[index] = flag%10;
flag /= 10;
index++; length++;
}
//following lines are the multiplication code
while(number>1){
index = 0;
int temp = 0;
number--;
for(index = 0; index<length; index++){
long long int x = fact[index];
x *= number;
x += temp;
fact[index] = x%10;
temp = x/10;
}
//here we append the carry over left from multiplication
while(temp){
fact[index] = temp%10;
temp /= 10;
length++;
}
}
//print the array from most to least significant digit
printf("%d! = ", number);
for(int i = length-1; i>=0; i--){
printf("%d ", fact[i]);
}
printf("\n");
}
return 0;
}
Running this gives you:
1! = 1
2! = 2
3! = 6
4! = 2 4
5! = 1 2 0
6! = 7 2 0
7! = 5 0 4 0
8! = 4 0 3 2 0
9! = 3 6 2 8 8 0
10! = 3 6 2 8 8 0 0
11! = 3 9 9 1 6 8 0 0
12! = 4 7 9 0 0 1 6 0 0
13! = 6 2 2 7 0 2 0 8 0 0
14! = 8 7 1 7 8 2 9 1 2 0 0
15! = 1 3 0 7 6 7 4 3 6 8 0 0 0
16! = 2 0 9 2 2 7 8 9 8 8 8 0 0 0
17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
18! = 1 9 9 1 0 4 7 1 7 3 8 5 7 2 8 0 0 0
which is, as you state okay up until 18!, where if fails. And, in fact, you can see the ratio between 17! and 18! is about 500 rather than 18 so that's where we should look.
Let's first strip out the extraneous stuff by starting at 17!. That can be done simply by changing a couple of starting values:
int n = 2, numberx = 16;
and that gives:
17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
18! = 1 9 9 1 0 4 7 1 7 3 8 5 7 2 8 0 0 0
Then we can add debug code to see what's happening, outputting temporary results along the way. The main loop can become:
while(number>1){
index = 0;
int temp = 0;
number--;
if (numberx > 17) printf("\n");
for(index = 0; index<length; index++){
if (numberx > 17) printf("index %d fact[] %d number %d temp %d", index, fact[index], number, temp);
long long int x = fact[index];
x *= number;
x += temp;
fact[index] = x%10;
temp = x/10;
if (numberx > 17) printf(" -> fact[] %d temp %d\n", fact[index], temp);
}
//here we append the carry over left from multiplication
while(temp){
fact[index] = temp%10;
temp /= 10;
length++;
}
if (numberx > 17) {
printf("temp: ");
for(int i = length-1; i>=0; i--){
printf("%d ", fact[i]);
}
printf("\n");
}
}
This shows you *exactly where things start to go wrong (// bits are added by me):
17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
index 0 fact[] 8 number 17 temp 0 -> fact[] 6 temp 13
index 1 fact[] 1 number 17 temp 13 -> fact[] 0 temp 3
temp: 3 0 6 // okay: 18 * 17 = 306
index 0 fact[] 6 number 16 temp 0 -> fact[] 6 temp 9
index 1 fact[] 0 number 16 temp 9 -> fact[] 9 temp 0
index 2 fact[] 3 number 16 temp 0 -> fact[] 8 temp 4
temp: 4 8 9 6 // okay 306 * 16 = 4896
index 0 fact[] 6 number 15 temp 0 -> fact[] 0 temp 9
index 1 fact[] 9 number 15 temp 9 -> fact[] 4 temp 14
index 2 fact[] 8 number 15 temp 14 -> fact[] 4 temp 13
index 3 fact[] 4 number 15 temp 13 -> fact[] 3 temp 7
temp: 7 3 4 4 0 // okay 4896 * 15 = 73440
index 0 fact[] 0 number 14 temp 0 -> fact[] 0 temp 0
index 1 fact[] 4 number 14 temp 0 -> fact[] 6 temp 5
index 2 fact[] 4 number 14 temp 5 -> fact[] 1 temp 6
index 3 fact[] 3 number 14 temp 6 -> fact[] 8 temp 4
index 4 fact[] 7 number 14 temp 4 -> fact[] 2 temp 10
temp: 8 1 2 8 1 6 0 // no good: 73440 * 14 = 10128160 !!!
1 0 2 8 1 6 0 // is what it should be
With a bit of thought, it appears to be the point where the final "carry" from the multiplication is greater than nine, meaning it's almost certainly in the code for handling that:
while(temp){
fact[index] = temp%10;
temp /= 10;
length++;
}
Thinking about that (and comparing it to other code that changes index and length together), it becomes obvious - even though you increase the length of the array, you're not increasing the index. That means, for a final carry of ten or more, the subsequent carry will not populate the correct index, it will simply overwrite the same index each time.
This can be seen here:
temp: 8 1 2 8 1 6 0 // no good: 73440 * 14 = 10128160 !!!
1 0 2 8 1 6 0 // is what it should be
where it will have placed the zero (10 % 10) at that second location (increasing the length) but then placed the one (10 / 10) at the same index, leaving the 8 at whatever value it had before.
So, if we increment index as well, what do we see (going back to the less verbose code)?
1! = 1
2! = 2
3! = 6
4! = 2 4
5! = 1 2 0
6! = 7 2 0
7! = 5 0 4 0
8! = 4 0 3 2 0
9! = 3 6 2 8 8 0
10! = 3 6 2 8 8 0 0
11! = 3 9 9 1 6 8 0 0
12! = 4 7 9 0 0 1 6 0 0
13! = 6 2 2 7 0 2 0 8 0 0
14! = 8 7 1 7 8 2 9 1 2 0 0
15! = 1 3 0 7 6 7 4 3 6 8 0 0 0
16! = 2 0 9 2 2 7 8 9 8 8 8 0 0 0
17! = 3 5 5 6 8 7 4 2 8 0 9 6 0 0 0
18! = 6 4 0 2 3 7 3 7 0 5 7 2 8 0 0 0
19! = 1 2 1 6 4 5 1 0 0 4 0 8 8 3 2 0 0 0
20! = 2 4 3 2 9 0 2 0 0 8 1 7 6 6 4 0 0 0 0
That solves your specific problem and hopefully provides some education on debugging as well :-)
I’m trying to implement Radix-Sort with arrays of long int.
My Algorithm has 2 inputs:
The number ’n’ of elements to be sorted
The number ‘b’ of bits per digit (So, insted of taking the last decimal number, I take the groups of ‘b’ digits, representing one digit)
But, when I try to sort the vector, I find 2 problems:
First one: It doesn’t sort the vector properly
Second one: I have implemented it for taking from the last group of bits to the first one. But it seems like it tries to sort in in the opposite direction.
Here is the code:
#define MAX_BITS sizeof(long int)*8
#define VectorL vector<long int>
struct Compare
{
int beg = 0;
int bits_per_digit = 2;
Compare(int x, int y){
beg = x;
bits_per_digit = y;
}
bool operator() (long& a, long& b){
int _begin = beg;
int _final = _begin + bits_per_digit - 1;
assert(_begin <= _final); // b==f if bits_per_digit = 1;
bitset<sizeof(long int)> _x(a), _y(b);
for(_begin; _begin <= _final; _begin++)
if(_x[_begin] != _y[_begin])
return (_x[_begin] < _y[_begin]) ? true : false;
return false;
}
};
void ALG(VectorL& nums, int n, int b)
{
assert(nums.size() == n);
assert(MAX_BITS%b == 0);
int _grupos = MAX_BITS/b;
Compare myobject(MAX_BITS,b);
for (int g = 1; g <= _grupos; g++)
{
myobject.beg -= b;
sort(nums.begin(), nums.end(), myobject);
}
}
Here’s the main:
int n=5, b=2;
VectorL nums(n);
nums[0] = 2;
nums[1] = 6;
nums[2] = 1;
nums[3] = 5;
nums[4] = 0;
ALG(nums, n, b);
And here’s the output:
Initial Vector: 2 6 1 5 0
From bit 62 to bit 63
Vector after the 1º sort: 2 6 1 5 0
From bit 60 to bit 61
Vector after the 2º sort: 2 6 1 5 0
From bit 58 to bit 59
Vector after the 3º sort: 2 6 1 5 0
From bit 56 to bit 57
Vector after the 4º sort: 2 6 1 5 0
From bit 54 to bit 55
Vector after the 5º sort: 2 6 1 5 0
From bit 52 to bit 53
Vector after the 6º sort: 2 6 1 5 0
From bit 50 to bit 51
Vector after the 7º sort: 2 6 1 5 0
From bit 48 to bit 49
Vector after the 8º sort: 2 6 1 5 0
From bit 46 to bit 47
Vector after the 9º sort: 2 6 1 5 0
From bit 44 to bit 45
Vector after the 10º sort: 2 6 1 5 0
From bit 42 to bit 43
Vector after the 11º sort: 2 6 1 5 0
From bit 40 to bit 41
Vector after the 12º sort: 2 6 1 5 0
From bit 38 to bit 39
Vector after the 13º sort: 2 6 1 5 0
From bit 36 to bit 37
Vector after the 14º sort: 2 6 1 5 0
From bit 34 to bit 35
Vector after the 15º sort: 2 6 1 5 0
From bit 32 to bit 33
Vector after the 16º sort: 2 6 1 5 0
From bit 30 to bit 31
Vector after the 17º sort: 2 6 1 5 0
From bit 28 to bit 29
Vector after the 18º sort: 2 6 1 5 0
From bit 26 to bit 27
Vector after the 19º sort: 2 6 1 5 0
From bit 24 to bit 25
Vector after the 20º sort: 2 6 1 5 0
From bit 22 to bit 23
Vector after the 21º sort: 2 6 1 5 0
From bit 20 to bit 21
Vector after the 22º sort: 2 6 1 5 0
From bit 18 to bit 19
Vector after the 23º sort: 2 6 1 5 0
From bit 16 to bit 17
Vector after the 24º sort: 2 6 1 5 0
From bit 14 to bit 15
Vector after the 25º sort: 2 6 1 5 0
From bit 12 to bit 13
Vector after the 26º sort: 2 6 1 5 0
From bit 10 to bit 11
Vector after the 27º sort: 2 6 1 5 0
From bit 8 to bit 9
Vector after the 28º sort: 2 6 1 5 0
From bit 6 to bit 7
Vector after the 29º sort: 2 6 1 5 0
From bit 4 to bit 5
Vector after the 30º sort: 2 6 1 5 0
From bit 2 to bit 3
Vector after the 31º sort: 2 1 0 6 5
From bit 0 to bit 1
Vector after the 32º sort: 0 2 6 1 5
As you can see in the output, the changes only happens in the 31st and 32nd sort (so, that means the significant bits are found in the last searchs).
Anyone can help me to find my mistake?
Thanks in advance.
I am handling parquet file format. For example:
a group of data:
1 2 null 3 4 5 6 null 7 8 null null 9 10 11 12 13 14
I got a bit vector to indicate null element:
1 1 0 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1
and only store the non-null element:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
I want to evaluate a predicate: big then 5
I compared non-null element to 5 and got a bit vector:
0 0 0 0 0 1 1 1 1 1 1 1 1 1
I want to got a bit vector for all elements:
0 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 1
the 0 in bold is null elements, should be false.
void IntersectBitVec(vector<int64_t>& bit_vec, vector<int64_t>& sub_bit_vec) {
int data_idx = 0,
int bit_idx = 63;
for (int i = 0; i < bit_vec.size(); ++i) {
for (int j = 63; j >=0; --j) {
if (bit_vec[i] & 0x01 << j) {
if (!(sub_bit_vec[data_idx] & 0x01 << bit_idx)) {
bit_vec[i] &= ~(0x01 << j);
}
if (--bit_idx < 0) {
--data_idx;
bit_idx = 63;
}
}
}
}}
My code is quite ugly, is there anyway to make it fast? Great thanks!
The problem is defined as follows:
You're given a square. The square is lined with flat flagstones size 1m x 1m. Grass surround the square. Flagstones may be at different height. It starts raining. Determine where puddles will be created and compute how much water will contain. Water doesn't flow through the corners. In any area of grass can soak any volume of water at any time.
Input:
width height
width*height non-negative numbers describing a height of each flagstone over grass level.
Output:
Volume of water from puddles.
width*height signs describing places where puddles will be created and places won't.
. - no puddle
# - puddle
Examples
Input:
8 8
0 0 0 0 0 1 0 0
0 1 1 1 0 1 0 0
0 1 0 2 1 2 4 5
0 1 1 2 0 2 4 5
0 3 3 3 3 3 3 4
0 3 0 1 2 0 3 4
0 3 3 3 3 3 3 0
0 0 0 0 0 0 0 0
Output:
11
........
........
..#.....
....#...
........
..####..
........
........
Input:
16 16
8 0 1 0 0 0 0 2 2 4 3 4 5 0 0 2
6 2 0 5 2 0 0 2 0 1 0 3 1 2 1 2
7 2 5 4 5 2 2 1 3 6 2 0 8 0 3 2
2 5 3 3 0 1 0 3 3 0 2 0 3 0 1 1
1 0 1 4 1 1 2 0 3 1 1 0 1 1 2 0
2 6 2 0 0 3 5 5 4 3 0 4 2 2 2 1
4 2 0 0 0 1 1 2 1 2 1 0 4 0 5 1
2 0 2 0 5 0 1 1 2 0 7 5 1 0 4 3
13 6 6 0 10 8 10 5 17 6 4 0 12 5 7 6
7 3 0 2 5 3 8 0 3 6 1 4 2 3 0 3
8 0 6 1 2 2 6 3 7 6 4 0 1 4 2 1
3 5 3 0 0 4 4 1 4 0 3 2 0 0 1 0
13 3 6 0 7 5 3 2 21 8 13 3 5 0 13 7
3 5 6 2 2 2 0 2 5 0 7 0 1 3 7 5
7 4 5 3 4 5 2 0 23 9 10 5 9 7 9 8
11 5 7 7 9 7 1 0 17 13 7 10 6 5 8 10
Output:
103
................
..#.....###.#...
.......#...#.#..
....###..#.#.#..
.#..##.#...#....
...##.....#.....
..#####.#..#.#..
.#.#.###.#..##..
...#.......#....
..#....#..#...#.
.#.#.......#....
...##..#.#..##..
.#.#.........#..
......#..#.##...
.#..............
................
I tried different ways. Floodfill from max value, then from min value, but it's not working for every input or require code complication. Any ideas?
I'm interesting algorithm with complexity O(n^2) or o(n^3).
Summary
I would be tempted to try and solve this using a disjoint-set data structure.
The algorithm would be to iterate over all heights in the map performing a floodfill operation at each height.
Details
For each height x (starting at 0)
Connect all flagstones of height x to their neighbours if the neighbour height is <= x (storing connected sets of flagstones in the disjoint set data structure)
Remove any sets that connected to the grass
Mark all flagstones of height x in still remaining sets as being puddles
Add the total count of flagstones in remaining sets to a total t
At the end t gives the total volume of water.
Worked Example
0 0 0 0 0 1 0 0
0 1 1 1 0 1 0 0
0 1 0 2 1 2 4 5
0 1 1 2 0 2 4 5
0 3 3 3 3 3 3 4
0 3 0 1 2 0 3 4
0 3 3 3 3 3 3 0
0 0 0 0 0 0 0 0
Connect all flagstones of height 0 into sets A,B,C,D,E,F
A A A A A 1 B B
A 1 1 1 A 1 B B
A 1 C 2 1 2 4 5
A 1 1 2 D 2 4 5
A 3 3 3 3 3 3 4
A 3 E 1 2 F 3 4
A 3 3 3 3 3 3 A
A A A A A A A A
Remove flagstones connecting to the grass, and mark remaining as puddles
1
1 1 1 1
1 C 2 1 2 4 5 #
1 1 2 D 2 4 5 #
3 3 3 3 3 3 4
3 E 1 2 F 3 4 # #
3 3 3 3 3 3
Count remaining set size t=4
Connect all of height 1
G
C C C G
C C 2 D 2 4 5 #
C C 2 D 2 4 5 #
3 3 3 3 3 3 4
3 E E 2 F 3 4 # #
3 3 3 3 3 3
Remove flagstones connecting to the grass, and mark remaining as puddles
2 2 4 5 #
2 2 4 5 #
3 3 3 3 3 3 4
3 E E 2 F 3 4 # # #
3 3 3 3 3 3
t=4+3=7
Connect all of height 2
A B 4 5 #
A B 4 5 #
3 3 3 3 3 3 4
3 E E E E 3 4 # # #
3 3 3 3 3 3
Remove flagstones connecting to the grass, and mark remaining as puddles
4 5 #
4 5 #
3 3 3 3 3 3 4
3 E E E E 3 4 # # # #
3 3 3 3 3 3
t=7+4=11
Connect all of height 3
4 5 #
4 5 #
E E E E E E 4
E E E E E E 4 # # # #
E E E E E E
Remove flagstones connecting to the grass, and mark remaining as puddles
4 5 #
4 5 #
4
4 # # # #
After doing this for heights 4 and 5 nothing will remain.
A preprocessing step to create lists of all locations with each height should mean that the algorithm is close to O(n^2).
This seems to be working nicely. The idea is it is a recursive function, that checks to see if there is an "outward flow" that will allow it to escape to the edge. If the values that do no have such an escape will puddle. I tested it on your two input files and it works quite nicely. I copied the output for these two files for you. Pardon my nasty use of global variables and what not, I figured it was the concept behind the algorithm that mattered, not good style :)
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
int SIZE_X;
int SIZE_Y;
bool **result;
int **INPUT;
bool flowToEdge(int x, int y, int value, bool* visited) {
if(x < 0 || x == SIZE_X || y < 0 || y == SIZE_Y) return true;
if(visited[(x * SIZE_X) + y]) return false;
if(value < INPUT[x][y]) return false;
visited[(x * SIZE_X) + y] = true;
bool left = false;
bool right = false;
bool up = false;
bool down = false;
left = flowToEdge(x-1, y, value, visited);
right = flowToEdge(x+1, y, value, visited);
up = flowToEdge(x, y+1, value, visited);
down = flowToEdge(x, y-1, value, visited);
return (left || up || down || right);
}
int main() {
ifstream myReadFile;
myReadFile.open("test.txt");
myReadFile >> SIZE_X;
myReadFile >> SIZE_Y;
INPUT = new int*[SIZE_X];
result = new bool*[SIZE_X];
for(int i = 0; i < SIZE_X; i++) {
INPUT[i] = new int[SIZE_Y];
result[i] = new bool[SIZE_Y];
for(int j = 0; j < SIZE_Y; j++) {
int someInt;
myReadFile >> someInt;
INPUT[i][j] = someInt;
result[i][j] = false;
}
}
for(int i = 0; i < SIZE_X; i++) {
for(int j = 0; j < SIZE_Y; j++) {
bool visited[SIZE_X][SIZE_Y];
for(int k = 0; k < SIZE_X; k++)//You can avoid this looping by using maps with pairs of coordinates instead
for(int l = 0; l < SIZE_Y; l++)
visited[k][l] = 0;
result[i][j] = flowToEdge(i,j, INPUT[i][j], &visited[0][0]);
}
}
for(int i = 0; i < SIZE_X; i++) {
cout << endl;
for(int j = 0; j < SIZE_Y; j++)
cout << result[i][j];
}
cout << endl;
}
The 16 by 16 file:
1111111111111111
1101111100010111
1111111011101011
1111000110101011
1011001011101111
1110011111011111
1100000101101011
1010100010110011
1110111111101111
1101101011011101
1010111111101111
1110011010110011
1010111111111011
1111110110100111
1011111111111111
1111111111111111
The 8 by 8 file
11111111
11111111
11011111
11110111
11111111
11000011
11111111
11111111
You could optimize this algorithm easily and considerably by doing several things. A: return true immediately upon finding a route would speed it up considerably. You could also connect it globally to the current set of results so that any given point would only have to find a flow point to an already known flow point, and not all the way to the edge.
The work involved, each n will have to exam each node. However, with optimizations, we should be able to get this much lower than n^2 for most cases, but it still an n^3 algorithm in the worst case... but creating this would be very difficult(with proper optimization logic... dynamic programming for the win!)
EDIT:
The modified code works for the following circumstances:
8 8
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 1 1 1 1 0 1
1 0 1 0 0 1 0 1
1 0 1 1 0 1 0 1
1 0 1 1 0 1 0 1
1 0 0 0 0 1 0 1
1 1 1 1 1 1 1 1
And these are the results:
11111111
10000001
10111101
10100101
10110101
10110101
10000101
11111111
Now when we remove that 1 at the bottom we want to see no puddling.
8 8
1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 1 1 1 1 0 1
1 0 1 0 0 1 0 1
1 0 1 1 0 1 0 1
1 0 1 1 0 1 0 1
1 0 0 0 0 1 0 1
1 1 1 1 1 1 0 1
And these are the results
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1