Shortest distance using BFS - c++

I am trying to solve this SPOJ problem. The question asks to find the shortest path for each black(1) pixel.
Since it is a unweighted graph I used BFS.
for input:
3 3
010
000
000
it's giving:
323
434
343
instead of:
101
212
323
This is my code
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef pair < int, int >ii;
int R, C, i, j;
queue < ii > myQueue;
int visit[100][100];
int dist[100][100];
void bfs(ii s)
{
int i, j;
int count = 0;
ii node;
memset(visit, 0, sizeof(visit));
memset(dist, 0, sizeof(dist));
myQueue.push(s);
dist[node.first][node.second] = 0;
while (!myQueue.empty()) {
node = myQueue.front();
myQueue.pop();
if (visit[node.first][node.second])
continue;
visit[node.first][node.second] = 1;
//cout << node.first << " " << node.second << "\n";
i = node.first;
j = node.second;
if (j - 1 < R && j - 1 >= 0) {
myQueue.push(make_pair(i, j - 1));
if(dist[i][j - 1] == 0)
dist[i][j - 1] = dist[i][j] + 1;
}
if (j + 1 < R && j + 1 >= 0) {
myQueue.push(make_pair(i, j + 1));
if(dist[i][j+1] == 0)
dist[i][j + 1] = dist[i][j] + 1;
}
if (i - 1 < C && i - 1 >= 0) {
myQueue.push(make_pair(i - 1, j));
if(dist[i-1][j] == 0)
dist[i - 1][j] = dist[i][j] + 1;
}
if (i + 1 < C && i + 1 >= 0) {
myQueue.push(make_pair(i + 1, j));
if(dist[i+1][j] == 0)
dist[i + 1][j] = dist[i][j] + 1;
}
}
}
int main()
{
char input[100][100];
scanf("%d %d", &R, &C);
for (i = 0; i < R; i++)
scanf("%s", &input[i]);
int GRID[R][C];
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
GRID[i][j] = input[i][j] - '0';
for (i = 0; i < R; i++)
for (j = 0; j < C; j++) {
if (GRID[i][j] == 1)
bfs(make_pair(i, j));
}
for (i = 0; i < R; i++) {
for (j = 0; j < C; j++) {
printf("%d", dist[i][j]);
}
printf("\n");
}
}
ideone

Try this:
if (j - 1 < R && j - 1 >= 0) {
myQueue.push(make_pair(i, j - 1));
if(dist[i][j - 1] == 0)
dist[i][j - 1] = dist[i][j] + 1;
}
do this for all dist[][].

You have doubled result may be because you run your BFS twice between paired vertices.
But I'm not sure.

Related

Running into unexpected error while running C++ code on CLion IDE

below is my c++ code
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int trials = 0;
cin >> trials;
int c = 1;
while (trials--) {
string grid[51];
int r = 0;
cin >> r;
int n = r;
int m = 0;
for (int i = 0; i < r; i++) {
string s;
cin >> s;
m = s.size();
grid[i] = s;
}
if (n < 3 || m < 3) {
cout << "Case #" << c++ << ": " << 0 << endl;
continue;
}
int rowCount[51][51] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
rowCount[i + 1][j + 1] = rowCount[i + 1][j];
if (grid[i].at(j) == '0')
rowCount[i + 1][j + 1] += 1;
}
}
int columnCount[51][51] = {0};
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
columnCount[i + 1][j + 1] = columnCount[i][j + 1];
if (grid[i].at(j) == '0')
columnCount[i + 1][j + 1] += 1;
}
}
int table[51][51][51][51] = {0};
for (int h = 3; h <= n; h++) {
for (int w = 3; w <= m; w++) {
for (int i = 1; i + h - 1 <= n; i++) {
for (int j = 1; j + w - 1 <= m; j++) {
int i1 = i + h - 1;
int j1 = j + w - 1;
int vals[] = {table[i][j][i1][j1],
table[i + 1][j][i1][j1],
table[i][j + 1][i1][j1],
table[i][j][i1 - 1][j1],
table[i][j][i1][j1 - 1]};
table[i][j][i1][j1] = *max_element(vals, vals + 5);
// if (grid[i - 1].substr(j - 1, j1).find('.') == string::npos ||
// grid[i1 - 1].substr(j - 1, j1).find('.') == string::npos) {
// continue;
// }
if (rowCount[i][j1] - rowCount[i][j - 1] != j1 - j + 1 ||
rowCount[i1][j1] - rowCount[i1][j - 1] != j1 - j + 1) {
continue;
}
if (columnCount[i1][j] - columnCount[i - 1][j] != i1 - i + 1 ||
columnCount[i1][j1] - columnCount[i - 1][j1] != i1 - i + 1) {
continue;
}
if (table[i + 1][j + 1][i1 - 1][j1 - 1] + 1 > table[i][j][i1][j1]) {
table[i][j][i1][j1] = table[i + 1][j + 1][i1 - 1][j1 - 1] + 1;
}
}
}
}
}
cout << "Case #" << c++ << ": " << table[1][1][n][m] << endl;
}
return 0;
}
On running this code in Clion IDE (on my windows 11 system) I get the following error
Process finished with exit code -1073741571 (0xC00000FD)
This is basically a code for solving a competitive programming question. As soon as I execute the code I get the error. It doesn't even let me input trials.
I am not sure what the reason is for this. I believe it is to do with the memory available for execution. I have tried to change the memory settings for the CLion IDE but I still get the same error. The code above is long but you can just copy/paste it into your own environment and maybe have a look? It works alright on ideone (online compiler). Could anyone help with this?

boolean value changes without me coding it in

#include<iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
int t, n;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
vector<vector<int>> numbers;
for (int k = 1; k < n * n; k += n) {
vector<int> temp;
for (int j = 0; j < n; j++) {
temp.push_back(k + j);
}
numbers.push_back(temp);
}
for (int j = 0; j < numbers.size(); j++) //row
for (int k = 0; k < numbers.size(); k++) { //column
bool adjacent = false;
if (j - 1 > 0)
if (abs(numbers[j][k] - numbers[j - 1][k]) == 1)
adjacent = true;
if (j + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[j + 1][k]) == 1)
adjacent = true;
if (k - 1 > 0)
if (abs(numbers[j][k] - numbers[j][k - 1]) == 1)
adjacent = true;
if (k + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[j][k + 1]) == 1)
adjacent = true;
if (adjacent)
for (int l = 0; l < numbers.size(); l++)
for (int m = 0; m < numbers.size(); m++) {
bool adjacent2 = false, adjacent3 = false;
if (j - 1 > 0)
if (abs(numbers[l][m] - numbers[j - 1][k]) == 1)
adjacent2 = true;
if (j + 1 < numbers.size())
if (abs(numbers[l][m] - numbers[j + 1][k]) == 1)
adjacent2 = true;
if (k - 1 > 0)
if (abs(numbers[l][m] - numbers[j][k - 1]) == 1)
adjacent2 = true;
if (k + 1 < numbers.size())
if (abs(numbers[l][m] - numbers[j][k + 1]) == 1) {
adjacent2 = true;
cout << "hi " << adjacent2 << endl; //HERE
}
if (!adjacent2) {
cout << adjacent2 << endl;
if (l - 1 > 0)
if (abs(numbers[j][k] - numbers[l - 1][m]) == 1)
adjacent3 = true;
if (l + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[l + 1][m]) == 1)
adjacent3 = true;
if (m - 1 > 0)
if (abs(numbers[j][k] - numbers[l][m - 1]) == 1)
adjacent3 = true;
if (m + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[l][m + 1]) == 1)
adjacent3 = true;
if (!adjacent3) {
int temp = numbers[j][k];
numbers[j][k] = numbers[l][m];
numbers[l][m] = temp;
}
}
}
}
if (n > 2)
for (auto x : numbers) {
for (int y : x) {
cout << y << " ";
}
cout << endl;
}
else if (n == 1)
cout << 1 << endl;
else cout << -1 << endl;
}
}
I'm trying to solve this codeforces problem: https://codeforces.com/contest/1520/problem/C. The way I'm trying to solve it is by generating a 2d array from 1 to n squared. I then start from the first row, left to right, and work my way down to the last number. For each element, in the 2d array, I check for adjacent elements and see if their differences are equal to one. If there's at least one instance of that, I search for an element that can be swapped with the original element.
Swapping the elements works correctly, except for the first one.
For example, if n = 3, I should start with:
123
456
789
and then I should get
423
156
789
However, I get
213
456
789
It doesn't matter if n=3 or n=10. It will always start as 213...
I tried debugging and noticed that in the line denoted as "HERE", adjacent2 is true. However, the next if-statement, if(!adjacent2) runs. Why does it run? If adjacent2 is true, shouldn't !adjacent2 equal false? I also printed out adjacent2 after if(!adjacent2) and it's somehow false and I never changed it!
I believe this part might not be correct:
if (k + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[j][k + 1]) == 1)
adjacent = true;
If the matrix is not square, numbers.size() might not be the same as numbers[j].size(). And then k + 1 should be compared to the latter.

Creates A Number Pattern Using Array

So the numbers pattern going like this :
Input : 2
Output :
0 0 0 1 0 0 0
0 0 2 0 12 0 0
0 3 0 0 0 11 0
4 0 0 0 0 0 10
0 5 0 0 0 9 0
0 0 6 0 8 0 0
0 0 0 7 0 0 0
I have solved it using usual method, but when I tried with array, the output is really messed up. Any suggestion how to make this number pattern with array?
This is my code that creates this number pattern :
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) cout << i << " ";
else if (i > mid && j == mid-n+i) cout << i << " ";
else cout << "0 ";
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) cout << (2*n-i) << " ";
else if (i < mid && j == mid+i-1) cout << (2*n-i) << " ";
else cout << "0 ";
}
cout << endl;
}
Thanks in advance.
I think this code(working) might help you.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
int arr[n][n];
memset(arr, 0, sizeof(arr));
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) arr[i - 1][j - 1] = i;
else if (i > mid && j == mid-n+i) arr[i - 1][j - 1] = i;
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) arr[i - 1][j - 1] = 2 * n - i;
else if (i < mid && j == mid+i-1) arr[i - 1][j - 1] = 2 * n - i;
}
}
}
Supplementing baymaxx, if you desire dynamic memory allocation:
#include <iostream>
#include <stdio.h>
#include <string.h>
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
// create array of the specified size.
int** arr = (int**) malloc (n * sizeof(int**));
for (int p = 0; p < n; ++p) {
arr[p] = (int*) malloc(n * sizeof(int));
for (int q = 0; q < n; ++q) {
arr[p][q] = 0;
}
}
// fill array (algorithm attr. baymaxx)
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) {
arr[i - 1][j - 1] = i;
}
else if (i > mid && j == mid-n+i) {
arr[i - 1][j - 1] = i;
}
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) {
arr[i - 1][j - 1] = 2 * n - i;
}
else if (i < mid && j == mid+i-1) {
arr[i - 1][j - 1] = 2 * n - i;
}
}
}
// print array
for (int p = 0; p < n; ++p) {
for (int q = 0; q < n; ++q) {
int x = arr[p][q];
std::cout << x << " ";
}
std::cout << endl;
}
// delete array
for (int i = 1; i < n; ++i) {
free(arr[i]);
}
free(arr);

Longest Common Subsequence is not showing the result

I have written this code using dynamic programming approach and I think the logic is good but the code is not displaying the result. The code is as follows:
#include <iostream>
using namespace std;
void LCS(int input1[], int input2[], int n, int m) {
int L[n + 1][m + 1]; /*This matrix stores the length of common subsequences*/
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (input1[i - 1] == input2[j - 1])
L[i][j] = 1 + L[i - 1][j - 1];
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
int index = L[n][m];
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[index - 1] = input1[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
for (int i = 0; i < index; i++){
cout << lcs[i];
}
}
int main() {
int n, m;
cin >> n >> m;
int input1[n], input2[m]; /*two arrays from which longest subsequnce is to be found*/
for (int i = 0; i < n; i++)
cin >> input1[i];
for (int i = 0; i < m; i++)
cin >> input2[i];
LCS(input1, input2, n, m);
return 0;
}
The code terminates without showing any result!
I even switched to a different IDE but its the same. What is wrong with this?
You are modifying the index variable. Create a copy of it and modify that. Here I used temp.
int index = L[n][m];
int temp = index;
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[temp - 1] = input1[i - 1];
i--;
j--;
temp--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
for (int i = 0; i < index; i++){
cout << lcs[i];
}
In your version index is decremented to zero when you want to print the result so nothing will be printed.

equidivision partition using breadth first search

i was trying to solve a breadth first search problem on spoj.the problem statement is as follows:
An equidivision of an n × n square array of cells is a partition of the n^2 cells in the array in exactly n sets, each one with n contiguous cells. Two cells are contiguous when they have a common side.
the problem link is:http://www.spoj.com/problems/EQDIV/
my code:http://ideone.com/OjluJG
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<queue>
using namespace std;
#define pii pair< int, int>
int n, m, grid[105][105];
char inp[101 * 101];
bool inRange(int i, int j)
{
m = n;
return (i >= 0 && i < n && j >= 0 && j < m);
}
int main()
{
int i, j, comp;
scanf("%d", &n);
while (n != 0)
{
if (n == 1)
{
printf("good\n");
scanf("%d", &n);
continue;
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
grid[i][j] = 0;
}
}
int a, b, start1[n], start2[n], print = 0, c, d;
queue< pii > Q;
pii p;
getchar();
for (i = 1; i <= n - 1; i++)
{
gets(inp);
stringstream ss(inp);
for (j = 1; j <= n; j++)
{
ss >> a >> b;
if (j == 1)
{
start1[i - 1] = a;
start2[i - 1] = b;
//printf("%d %d\n",start1[i-1],start2[i-1]);
}
grid[a - 1][b - 1] = i;
}
}
for (int x = 0; x < n; x++)
{
int count = 1;
if (x == n - 1)
{
comp = 0;
p.first = c;
p.second = d;
Q.push(p);
}
else
{
comp = x + 1;
p.first = start1[x] - 1;
p.second = start2[x] - 1;
Q.push(p);
}
while (!Q.empty())
{
p = Q.front();
i = p.first;
j = p.second;
Q.pop();
grid[i][j] = -1;
if (x != n - 1)
{
if (inRange(i + 1, j) && grid[i + 1][j] == 0)
{
c = i + 1;
d = j;
}
if (inRange(i - 1, j) && grid[i - 1][j] == 0)
{
c = i - 1;
d = j;
}
if (inRange(i, j + 1) && grid[i][j + 1] == 0)
{
c = i;
d = j + 1;
}
if (inRange(i, j - 1) && grid[i][j - 1] == x + 1)
{
c = i;
d = j - 1;
}
}
if (inRange(i + 1, j) && grid[i + 1][j] == comp)
{
p.first = i + 1;
p.second = j;
Q.push(p);
count += 1;
}
if (inRange(i - 1, j) && grid[i - 1][j] == comp)
{
p.first = i - 1;
p.second = j;
Q.push(p);
count += 1;
}
if (inRange(i, j + 1) && grid[i][j + 1] == comp)
{
p.first = i;
p.second = j + 1;
Q.push(p);
count += 1;
}
if (inRange(i, j - 1) && grid[i][j - 1] == comp)
{
p.first = i;
p.second = j - 1;
Q.push(p);
count += 1;
}
}
if (count != n)
{
print = 1;
printf("wrong\n");
break;
}
}
if (print == 0)
printf("good\n");
//printf("%d %d\n",c,d);
scanf("%d", &n);
}
}
i am getting wrong answer ,don't know why?
can someone provide the testcase for which it is wrong?