Basically we got rover and its movement is coded with number 1-4 ( 1 = x+1, y+1 and 2 = x+1, y-1 and so on). It's starting position is x0 and y0, BUT it's also it's objective ( It has to move in full circle from where it started ). What I wrote can complete it's orders, BUT I can't figure out how to stop it when it reaches it's target early.
this is data file:
1 2 3 4 5
|
1 1
3
9 1 4 1 2 3 2 3 4 1
1 1
3 2 3 2
|
First two numbers are x0 and y0.
second number is the amount of command sequences.(Next three strings of numbers )
first number in the sequence is the length of the string. So first has 9 commands and third has 3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <iostream>
#include <fstream>
using namespace std;
struct misija
{
int seka[30];
int sekosIlg;
int komand=0;
int galX, galY;
};
int main()
{
misija uzd[10];
ifstream duom ("U2.txt");
int x0,y0;
duom >> x0 >> y0;
int n;
duom >> n;
for (int i=0; i<n; i++)
{
duom>>uzd[i].sekosIlg;
for(int j=0; j<uzd[i].sekosIlg; j++)
{
duom >> uzd[i].seka[j];
}
}
int dabarX=0,dabarY=0;
for(int i=0; i<n; i++)
{
for (int j=0; j<uzd[i].sekosIlg; j++)
{
if(uzd[i].seka[j]==1)
{
dabarX+=1;
dabarY+=1;
uzd[i].komand++;
}
else if(uzd[i].seka[j]==2)
{
dabarX+=1;
dabarY-=1;
uzd[i].komand++;
}
else if(uzd[i].seka[j]==3)
{
dabarX-=1;
dabarY-=1;
uzd[i].komand++;
}
else if(uzd[i].seka[j]==4)
{
dabarX-=1;
dabarY+=1;
uzd[i].komand++;
}
}
cout << dabarX << dabarY << endl;
}
return 0;
}
|
I have been trying lots of things, using while and do-while, if statements, but I can't find a way to solve it fully.
The results should be:
first sequence: "Completed" 1 1 after completing 8 commands.
second: "Failed" 2 2 after completing 1 given command
third: "Failed" 2 -2 after completing it's commands.