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);
Related
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?
#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.
So basically I'm trying to make this matrix (with a 2d array) in the format
1
1 1
1 1
1 1 1
1 2 1
1 1 1
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
depending on what size the user inputs. I can do this with hard code as shown:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int userSize;
do {
cin >> userSize;
int userArray[userSize][userSize];
int size = 1;
int maxSize = 99;
for (int i = 0; i < userSize; i++) {
for (int j = 0; j < userSize; j++) {
if ((j > 2 && j < userSize - 3) &&
(i > 2 && i < userSize - 3)){
userArray[i][j] = size + 3;
}
if ((j > 1 && j < userSize - 2) &&
(i > 1 && i < userSize - 2)) {
userArray[i][j] = size + 2;
}
else if ((j > 0 && j < userSize - 1) &&
(i > 0 && i < userSize - 1)) {
userArray[i][j] = size + 1;
}
else {
userArray[i][j] = size;
}
}
}
for (int i = 0; i < userSize; i++) {
for (int j = 0; j < userSize; j++) {
cout << setw(3) << userArray[i][j];
}
cout << endl;
}
cout << endl;
}while(userSize != 0);
return 0;
}
however I need to be able to do this up to 99, which would obviously be a lot to write. Is there a way I can do this with a while loop instead? Below is my failed attempt that only outputs garbage:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int userSize;
int maxSize = 99;
int size = 1;
do {
cin >> userSize;
int myMatrix[userSize][userSize];
for (int i = 0; i < userSize; i++) {
for (int j = 0; j < userSize; j++) {
while (maxSize >= 0) {
if ((j > maxSize) && (j < userSize - (maxSize + 1)) &&
((i > maxSize) && (i < userSize - (maxSize + 1)))) {
myMatrix[i][j] = size + maxSize;
}
/*
if ((j > 1 && j < userSize - 2) &&
(i > 1 && i < userSize - 2)) {
myMatrix[i][j] = size + 2;
}
else if ((j > 0 && j < userSize - 1) &&
(i > 0 && i < userSize - 1)) {
myMatrix[i][j] = size + 1;
}
*/
else {
myMatrix[i][j] = size;
}
maxSize = maxSize - 1;
}
}
}
for (int i = 0; i < userSize; i++) {
for (int j = 0; j < userSize; j++) {
cout << setw(3) << myMatrix[i][j];
}
cout << endl;
}
cout << endl;
}while(userSize != 0);
return 0;
}
looks to me like you want the minimum steps from an outer edge. Here is some psuedo-code
int[,] ComputeMatrix(int m,int n)
{
int[,] matrix = new int[m,n]
for(int i = 0;i < m;i++)
for(int j = 0;j < n;j++)
{
//Minimum distance from each of the 4 walls
matrix[i,j] = 1 + Math.Min(Math.Min(Math.Min(i,j),m-i-1),n-j-1);
}
return matrix;
}
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.
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.