How to implement bfs on a matrix? - c++

I am having trouble implementing bfs on a matrix. It seems like my code only checks for children of the starting node.
My goal is to find shortest path from 'B' to 'H'. I also think that my code needs a lot of modification. Thank you in advance!
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int bfs(int, int);
bool visited[100][100];
char matrica[100][100];
int m, n, d;
int main()
{
scanf("%d %d", &m, &n);
for(int i = 0;i < m;i++){
for(int j = 0; j < n; j++){
cin >> matrica[i][j];
visited[i][j] = false;
}
}
for(int i = 0;i < m;i++){
for(int j = 0; j < n; j++){
if(matrica[i][j] == 'B'){
bfs(i, j);
}
}
}
cout << endl << d;
return 0;
}
int gk[] = {1, 0, -1, 0};
int gr[] = {0, 1, 0, -1};
int bfs(int x, int y){
cout << endl;
queue<int>queue_x, queue_y;
int topx, topy, d=0;
//memset(visited, 0, sizeof visited);
visited[x][y] = true;
queue_x.push(x);
queue_y.push(y);
while(!queue_x.empty()){
topx = queue_x.front();
topy = queue_y.front();
queue_x.pop();
queue_y.pop();
if(matrica[topx][topy] == 'H'){
cout << endl << d << endl;
d++;
return d;
}
for(int i = 0; i < 4; i++){
x += gk[i];
y += gr[i];
if(visited[x][y] == false && matrica[x][y] != '#'){
visited[x][y] = true;
matrica[x][y] = '*';
queue_x.push(x);
queue_y.push(y);
d++;
//-------------
for(int i = 0; i < m;i++){
for(int j = 0; j < n;j++){
cout << matrica[i][j];
}
cout << endl;
}
//-------------
}
}
}
}
Input/Output:
Input:
5
5
#####
#..B#
#...#
#...#
###H#
Output:
#####
#..B#
#...#
#...#
###H#
0

The variables x and y
x += gk[i];
y += gr[i];
shouldn't "remember" their values from one iteration to the next.
Change these lines to
x = topx + gk[i];
y = topy + gr[i];

Related

all possible combinations to divide pack of candies

I have problem to solve and I'm stuck, I don't know how to start.
Suppose I have R childrens and S candies. I want to divide candies between childrens. Each child can get 0, 1, 2, 3 or 4 candies. How to find all the possibilities of such a division?
#include <iostream>
using namespace std;
void solve(int r, int s) {
if (s == 0)
{
cout << "no more candies" << endl;
return;
}
if (r == 0)
{
cout << "last child" << endl;
return;
}
for (int j = 0; j < 4 && j <= s; ++j)
{
cout << "r: " << r << " j: " << j << endl;
solve(r-1, s - j);
}
}
int main () {
int r, s;
cin >> r >> s;
solve(r, s);
return 0;
}
For now I have sth like this, I see in output that I have solutions here, but I don't know how to grab and store all possibilities into for example vector.
Just store counts and save variants at the last recursion level
vector<int> counts;
vector<vector<int>> sol;
void solve(int r, int s) {
if (s == 0)
{
sol.push_back(counts);
return;
}
if (r == 0)
{
return;
}
for (int j = 0; j <= 4 && j <= s; ++j)
{
counts[r - 1] += j;
solve(r - 1, s - j);
counts[r - 1] -= j;
}
}
int main() {
int r, s;
r = 3;
s = 5;
for (int j = 0; j < r; ++j)
counts.push_back(0);
solve(r, s);
for (int i = 0; i < sol.size(); i++) {
for (int j = 0; j < sol[i].size(); j++) {
cout << sol[i][j] << ' ';
}
cout << endl;
}
return 0;
}

How do i add all the values in my ascending array?

First i need to re-arrange all the values of my array into ascending order then add it afterwards. For example the user input 9 2 6, it will display in ascending order first ( 2 6 9 ) before it will add the sum 2 8 17.. The problem is my ascending order is not working, is there something wrong in my code?
#include <iostream>
#include<conio.h>
using namespace std;
int numberof_array, value[10], temp;
int i = 0, j;
void input()
{
cout << "Enter number of array:";
cin >> numberof_array;
for (i = 0; i < numberof_array; i++)
{
cout << "Enter value for array [" << i + 1 << "] - ";
cin >> value[i];
cout << endl;
}
}
void computation()
{
// this is where i'll put all the computation
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
for (i = 0; i <= numberof_array; i++)
{
for (j = 0; j <= numberof_array - i; j++)
{
if (value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
}
void display()
{
// display all the computation i've got
cout << "\nData after sorting: ";
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
getch();
}
int main()
{
input();
computation();
display();
}
void computation(){
for (int j = 0; j < numberof_array; j++) cout << value[j]<<"\t";
for (int i = 0; i <= numberof_array; i++) {
temp = value[i];
int temp_idx = i;
for (int j = i; j < numberof_array; j++) {
if (value[j] < temp) {
temp = value[j];
temp_idx = j;
}
}
int temp_swap = value[i];
value[i] = value[temp_idx];
value[temp_idx] = temp_swap;
}
}
How about changing your second function to something like above.
I have to agree with other commentators that your coding style is not preferred but there might be more to the story than meets the eye.

Having trouble implementing pseudocode for Subset Sum

so I am trying to implement the following pseudocode but it will not work as it is supposed to. Here is the problem description in the slide, "Given an integer bound, "W", and a collection of "n" items, each with a positive integer weight "wi", find a subset S of items that: maximizes Sigma sub i where i is an element of S "wi" while keeping this sum less than or equal or to W. I will attach the following slides for where I am getting the problem description and pseudocode from. The problem with my implementation is that it will only find the total max value and not the value that is less than or equal to the weight. So for example, if I had Weight 10 (W = 10) and items 3 (n = 3) with item weights 1, 4, & 8 then the following answer should be 9; however, my solution gives 12. Here are the slides (*Please not, where it says w[j] it is meant to be w[i] - the slide had a typo):
Here is my code that implements the pseudocode:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int max(int a, int b, int c) {
if (a >= b)
return a;
else
return b;
}
int optimal_weight(int W, const vector<int> &wt, int n){
vector<vector<int> > M;
M.resize(n+1);
for(int i = 0; i < n+1; ++i){
M[i].resize(W+1);
}
for(int w = 0; w < W+1; w++){
M[0][w] = 0;
}
for(int i = 1; i < n+1; i++){
M[i][0] = 0;
}
for(int i = 1; i < n+1; i++){
for(int w = 0; w < W+1; w++){
if(wt[i] > w){
M[i][w] = M[i-1][w];
}
M[i][w] = max(M[i-1][w], wt[i] + M[i-1][W-wt[i]], W);
}
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= W; j++)
printf ("%4d", M[i][j]);
printf("\n");
}
return M[n][W];
}
int main()
{
//int val[] = {1, 1, 1};
int W;
int n;
cin >> W >> n;
vector<int> wt(n);
for(int i = 0; i < n; i++){
cin >> wt[i];
}
cout << optimal_weight(W, wt, n) << endl;
}
Thank you for any help!
I figured it out! Here is my solution:
#include <iostream>
#include <vector>
using namespace std;
using std::vector;
int optimal_weight(int W, const vector<int> &wt) {
//write your code here
int n = wt.size();
vector<vector<int> > matrix;
matrix.resize(W+1);
for(int i = 0; i < W+1; i++){
matrix[i].resize(n);
}
for(int j = 0; j < n; j++){
matrix[0][j] = 0;
}
for(int w = 0; w < W + 1; w++){
matrix[w][0] = 0;
}
for(int i = 1; i < n; i++){
for(int w = 1; w < W+1; w++){
matrix[w][i] = matrix[w][i-1];
if(wt[i] <= w){
//cout << wt[i] << endl;
int val = matrix[w-wt[i]][i-1] + wt[i];
if(matrix[w][i] < val){
matrix[w][i] = val;
}
}
}
}
return matrix[W][n-1];
}
int main() {
int n, W;
std::cin >> W >> n;
vector<int> wt(n+1);
for (int i = 1; i < n+1; i++) {
wt[0]=0;
std::cin >> wt[i];
}
std::cout << optimal_weight(W, wt) << '\n';
}

Codeforces "The New President "

I tried to answer write a code to solve this problem, but I'm still getting a wrong answer at test 15 and I don't know what is missing in my code.
I tried a lot of test cases but the code has solved them all correctly.
My Code :
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
int c; cin >> c;
int v; cin >> v;
if (c == 1 && v == 0)
{
cout << 1 << " " << 1;
}
else
{
int cArray[c + 1];
int voting[v][c];
for (int j = 0; j<v; j++)
{
for (int z = 0; z<c; z++)
{
int temp; cin >> temp;
voting[j][z] = temp;
}
}
for (int j = 0; j <= c; j++)cArray[j] = 0;
for (int j = 0; j<v; j++)cArray[voting[j][0]]++;
int maxim = 0;
int maxN = 0;
int count = 0;
map<int, int > cand;
for (int j = 1; j <= c; j++)
{
if (cArray[j]>maxN)
{
cand.clear();
cand[j] = 1;
maxN = cArray[j];
maxim = j;
count = 0;
}
else if (cArray[j] == maxN)
{
cand[j] = 1;
count++;
}
}
if (count == 0)
cout << maxim << " " << 1;
else
{
for (int j = 0; j<v; j++)
{
for (int z = 1; z<c; z++)
{
if (cand.count(voting[j][z]))
{
cArray[voting[j][z]]++;
break;
}
}
}
maxim = 0;
maxN = 0;
count = 0;
for (int j = 1; j <= c; j++)
{
if (cArray[j]>maxN)
{
maxN = cArray[j];
maxim = j;
count = 0;
}
else if (cArray[j] == maxN)
{
count++;
}
}
cout << maxim << " " << 2;
}
}
return 0;
}
Your algorithm for checking the first round (win or top two candidates) seems wrong. It looks like you are expecting the top two candidates to have the same number of primary votes - this is not the case. You want to pick the top two candidates and the top one wins if it has more than 50 % of the vote.
I don't want to give you the answer (as that is the point of doing the exercises), but you need to rethink how you are processing the first part of the vote.
Also note that once someone has voted for one of the top two candidates, their secondary votes should not then count toward the other candidate (which you are currently doing).

C++ Nested For Loops

I thought this would be easier than originally planned. I'm trying to make this image out of nested for loops:
Any suggestions or solutions would be helpful.
#include <iostream>
using namespace std;
int main()
{
for(int i=0; i<1;i++)
{
cout<<i+1<<endl;
for(int j=0;j<2;j++)
{
cout<<j+1;
}
}
cout<<"\n";
for(int k=0; k<3; k++)
{
cout<<k+1;
}
cout<<"\n";
for(int l=0; l<4; l++)
{
cout<<l+1;
}
cout<<"\n";
for(int m=4; m>0; m--)
{
cout<<m;
}
cout<<"\n";
for(int n=3; n>0; n--)
{
cout<<n;
}
cout<<"\n";
for(int o=2; o>0; o--)
{
cout<<o;
}
cout<<"\n";
for(int p=0; p<1; p++)
{
cout<<p+1;
}
cin.get();
return 0;
}
Here's a solution in C for you =)
#include <stdio.h>
#include <string.h>
int main(void) {
char forward[5] = "1";
char reverse[5] = "4321";
int i;
for( i = 1; i <= 4; i++ ) {
printf( "%s\n", forward );
forward[i] = forward[i-1]+1;
}
for( i = 0; i < 4; i++ ) printf( "% 4s\n", reverse+i );
}
If you want an extremely compact and not very follow-able solution I decided to give it a shot.
int length = 4;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < length; j++)
{
for(int k = 0; k < (i == 1 ? length - j : j + 1); k++)
{
if(i == 1 && k == 0)
for(int x = 0; x < j; x++)
cout << " ";
cout << (i == 1 ? (length - k) - j : k + 1);
}
cout << endl;
}
}
Where length is the number of iterations from 1 to length.
Ok, since paddy posted his C solution (and someone said "compact" =P)...
#include <stdio.h>
int main()
{
char line[] = "1234321";
int i=0;
for (; i<4; printf("%.*s\n",++i,line));
for (i=0;i<4; printf("%4s\n",line+3+i++));
return 0;
}
Output
1
12
123
1234
4321
321
21
1
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/*first half: From level1 = 1 to less than 5, level2 = 1 to less than or equal to level1, print level2 */
for(int level1 = 1; level1 < 5; level1++) {
for(int level2 = 1; level2 <= level1; level2++){
cout << level2;
}
cout << '\n'; /*prints the newline AFTER each iteration of the `level2` loop*/
}
/*second half: reverse the logic of part1, but also add spaces in the beginning */
for(int level1 = 4; level1 > 0; level1--){
for(int interim = 4; interim > level1; interim--) cout << ' ';
for(int level2 = level1; level2 > 0; level2--){
cout << level2;
}
cout << '\n';
}
return 0;
}
Let me know if you do not understand this code :-)