|  | 
|  | 
| back to board | C++, What`s wrong with this code ? Test 3 fail. Posted by Ilya  27 Nov 2016 01:42#include<iostream>using namespace std;
 int main()
 {
 int CityCount, RoutesCount;
 
 cin >> CityCount;
 int *Road = new int[(CityCount - 1)*2];
 //Roads
 for (int i = 0; i < (CityCount - 1)*2; i++)
 {
 cin >> Road[i];
 }
 
 cin >> RoutesCount;
 int *Route = new int[RoutesCount*2];
 //Routes
 for (int i = 0; i < RoutesCount*2; i++)
 {
 cin >> Route[i];
 }
 //Road Matrix
 int j = 0;
 int **RMatrix= new int*[CityCount];
 for (int i = 0; i < CityCount; i++)
 {
 int *LineofMatrix = new int[CityCount];
 for (int i = 0; i < (CityCount - 1) * 2; i += 2)
 {
 if (Road[i] == j + 1)
 LineofMatrix[Road[i + 1] - 1] = 1;
 if (Road[i + 1] == j + 1)
 LineofMatrix[Road[i] - 1] = 1;
 }
 j++;
 RMatrix[i] = LineofMatrix;
 }
 //Output Road Matrix
 /*
 cout << endl;
 for (int i = 0; i < CityCount; i++)
 {
 for (int j = 0; j < CityCount; j++)
 if (RMatrix[i][j]>0)
 cout << RMatrix[i][j] << ' ';
 else
 {
 RMatrix[i][j] = 0;
 cout << 0 << ' ';
 }
 
 cout << endl;
 }
 cout << endl;
 */
 // alghoritm
 int *Value = new int[CityCount];
 int Vl = 0;
 int *Locked = new int[CityCount];
 int Lock = 0;
 bool BLock = false;
 
 int Bu = 0;
 int Final;
 int Cur;
 int PreCur=-1;
 int Iter=0;
 int min = INT_MAX;
 for (int i = 0; i < RoutesCount * 2; i+=2)
 {
 int *Buff = new int[CityCount * 2];
 Cur = Route[i] - 1;
 Final = Route[i + 1] - 1;
 Buff[Bu++] = Cur;
 Buff[Bu++] = Final;
 if (RMatrix[Cur][Final] == 1)
 {
 goto fin;
 }
 else
 {
 for (int j = 0; j < CityCount; j++)
 {
 if ((RMatrix[Cur][j] == 1) && (j != PreCur))
 {
 PreCur = Cur;
 Buff[Bu++] = j;
 Iter++;
 Cur = j;
 j = -1;
 }
 if (RMatrix[Cur][Final] == 1)
 {
 goto fin;
 }
 if (j == CityCount - 1)
 {
 j = Cur;
 Cur = PreCur;
 Bu -= Iter;
 Iter = 0;
 }
 }
 }
 fin:
 for (int z = 0; z < Bu; z++)
 {
 if (min > Buff[z])
 {
 min = Buff[z];
 }
 }
 Bu = 0;
 Iter = 0;
 cout << min+1 << endl;
 min = INT_MAX;
 delete[] Buff;
 }
 
 
 
 
 //system("pause");
 return 0;
 }
Re: C++, What`s wrong with this code ? Test 3 fail. Posted by mms  6 Mar 2017 11:00I also WA#3 help meRe: C++, What`s wrong with this code ? Test 3 fail. I fixed mine by changing "writeln(answer)" to "if x = y then writeln(x) else writeln(answer)".(program wasn't working correctly if start and end point were the same)
Re: C++, What`s wrong with this code ? Test 3 fail. Posted by mms  7 Mar 2017 11:28OK, solved this problem but get a RE on test#6. | 
 | 
|