0

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;



}
jpf
  • 1,447
  • 12
  • 22
  • 1
    Please use indentation to make your code easier to read and understand. Just about all I can say is that `h=h++` leads to *undefined behavior*. And that you are doing other bad things you should not be doing. – Some programmer dude May 16 '20 at 11:40
  • Thanks, but h++ is not the solution – Janusz Nowakowski May 16 '20 at 11:45
  • Well you have the same problem with `przejscia=przejscia++`. And why are you doing that directly after `przejscia=0`? Time for you to learn some common debugging techniques (likes some plain [*rubber duck debugging*](https://en.wikipedia.org/wiki/Rubber_duck_debugging) or just using an actual debugger to step through your code statement by statement while monitoring variables and their values). – Some programmer dude May 16 '20 at 11:49
  • Yes I saw it too, but it is not the problem – Janusz Nowakowski May 16 '20 at 11:49
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095/5910058) – Jesper Juhl May 16 '20 at 13:27
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/5910058) – Jesper Juhl May 16 '20 at 13:28
  • I am sorry to tell you this. Your code is unsalvageable. You have no coherent algorithm. You have had one insight: that the answer is "-1" if and only if the first symbol of the string is "Z". There is another insight which will lead to an efficient solution, but even an inefficient solution requires an algorithm. *Try a simpler problem first.* Write a program that does something simpler, like counting the B's and Z's; get that working perfectly before you attempt the more difficult problem. – Beta May 16 '20 at 14:00

0 Answers0