-1

I have a problem with Input and Output in my program.

I can’t make sure that the BFS response for each horse is separated and at the end the BFS response of all horses of a given number of examples is summarized.

Everything works well for this example:

  • 1- number of examples
  • 1 - the number of chess knights
  • 5 5 - the starting point
  • 5 6 - the final point

answer: 3

but not this example:

  • 1- number of examples
  • 2 - the number of chess knights
  • 0 0 - the starting point of the first knight
  • 1 0 - the starting point of the second knight
  • 0 1 - the final point of the first knight
  • 1 1 - the final point of the second knight

I need the answer(BFS) for the first horse and for the second horse to be summed up (for first = 2, for second = 2, for all horses = 4). But if you check this example using my code (below) then the answer is 3, the program considers only the first horse.

Here is my code:

    int main()
{
    int number_of_examples;
    cin >> number_of_examples;                             //number of examples   
    for (int i = 1; i <= number_of_examples; i++) {
        int number_of_horse;
        cin >> number_of_horse;                         //number of horse
        vector<Node> src;
        vector<Node> dest;



        int x, y;
        for (int i = 1; i <= number_of_horse; i++)
            cin >> x >> y;
        src.push_back(Node(x, y));

        for (int i = 1; i <= number_of_horse; i++)
            cin >> x >> y;
        dest.push_back(Node(x, y));

        for (int i = 0; i < src.size(); i++)
        {
            for (int j = 0; j < dest.size(); j++)
            {
                cout << BFS(src[i], dest[j]);
            }
        }

    }


    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Hard pressed to find a better example of code that should be run *under a debugger*. Your code blindly ignores all but the last horse position for both src and dest. Look at `src` and `dest` after each for-loop, and in particular, ask yourself why you're pushing `Node(x,y)` *after* each loop rather than *within* each loop. – WhozCraig Jan 10 '20 at 14:11
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Jan 10 '20 at 15:02

1 Answers1

4

the program considers only the first horse

As you told it to do...

for (int i = 1; i <= number_of_horse; i++)
    cin >> x >> y;
src.push_back(Node(x, y));

Only the first line following the for is repeated. If you want (I wager you do) repeat multiple statement, you need to enclose them in a block:

for (int i = 1; i <= number_of_horse; i++) {
    cin >> x >> y;
    src.push_back(Node(x, y));
}

You might want to read a bit more about the for syntax.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • I changed and got the answer 3423 – rprogramist Jan 10 '20 at 14:34
  • 2
    @rprogramist What am I supposed to do with that information? – YSC Jan 10 '20 at 14:35
  • Why did it show such an answer, if earlier the answer was 3? – rprogramist Jan 10 '20 at 14:39
  • @rprogramist because you modified the program? – YSC Jan 10 '20 at 14:40
  • 3
    @rprogramist Again, **debugger** . Your outer loop is now running twice, and the inner loop is doing the same. Therefore *four* iterations of the inner-most code where before there was only one. And each iteration is sending the results of the `BFS` call to stdout. Thus four results output, thus your output. *Use a debugger* . – WhozCraig Jan 10 '20 at 14:42
  • @rprogramist This is why you want to ask questions based around a [mcve]. With a [mcve] there is only one bug in the code. You don't want multiple bugs when debugging because it's hard to tell for sure that a fix actually did fix. YSC found and fixed a bug, exposing another bug. – user4581301 Jan 10 '20 at 16:08