I have to do homework and have been sitting for 4 hours. I don't know what to do next. The program compiles correctly but spits out the wrong data. He's not finished because I can't get through this problem. I'm just a beginner in c ++. Would anyone be able to help me?
Safe Mountaineers
A group of mountaineers climbs to the top following a narrow trail. Mountaineers are following one another carefully, because the path is steep and dangerous. Some of the climbers have a rope protection, and others unfortunately do not have it. The equipment of the group of mountaineers can be described using a series of letters: • B means a safe climber (equipped with a rope); • Z means mountaineer at risk (without rope).
For example, the string BZZBZ means a group of five mountaineers, two of whom have ropes. The group is headed by the first mountaineer on the right (in this case without a rope). Colleagues, however, are not left without help. A climber with a rope can pass the rope to a colleague walking directly in front of him (and then they are both safe), and this gesture is performed at once by all climbers who can do it. So in this case the string will change as follows:
BZZBZ- → BBZBB
Mountaineers who have become safe are marked in red. Such friendly gestures are repeated as long as possible:
BBZBB- → BBBBB
At this point, all mountaineers are safe - after two mate gestures. Of course, it will not be possible for every group of mountaineers to ensure safety for everyone. A mountaineer who walks at the beginning of the group cannot lend his rope to a friend because nobody walks in front of him.
Write a program that for a given group of mountaineers calculates the number of gestures needed to make everyone become safe, or declares that it is impossible.
Entrance
The first line of the standard input contains a positive integer denoting the number of data sets or groups of mountaineers (N≤200).
Each subsequent line contains a series of letters B or Z describing the equipment of mountaineers in a given group (according to the task). Each group has between 1 and 1,000,000 people, and it can be assumed that the total number of all groups will not exceed 20,000,000.
Exit
For each group, the program should write a line of text containing the number of gestures needed that will lead to a situation when all climbers in the group will become safe, or the number -1 if this is impossible.
Example
For the example input below: 3 BZZBBZ Bzzz ZBBB
the correct result is: 2 3 -1
Explanation of the example: The first case is described in the body of the task. In the second case, the rope feed proceeds as follows:
BZZZ- BBZZ- → → → BBBB BBBZ-
In the third case, the last climber cannot receive the rope.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int N, przejscia;
string a,b,c;
main()
{
N=3;
przejscia=0;
cout << N<<endl;
a="BZZBB\n";
char a1=(a[1]);
b="ZBBZBZBB\n";
c="BBZZZZBZBZBZ\n";
int ailosc = a.length();
int bilosc = b.length();
int cilosc = c.length();
cout <<a;
cout <<b;
cout <<c;
if (a[0]=='Z')
{cout << "-1";
}
if (b[0]=='Z')
{cout << "-1";
}
if (c[0]=='Z')
{cout << "-1";
}
int h, k;
h=0;
k=0;
if (a[0]=='B')
{for (int i=1; i<=ailosc; i++)
{h++;
if ((a[h]=='Z')and(a[k]=='B'))
{a[h]='B';
przejscia++;
}
k++;
}
}
cout << przejscia;
}