Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. //This program gets data from file "input.txt" and return queue of correct order
  2. //of the courses or NIE if it's impossible to reach all course
  3.  
  4. //If it's possible, it return queue and code 0. If it's not possible,
  5. //return NIE and code 5.
  6.  
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <queue>
  11.  
  12. using namespace std;
  13.  
  14. //Checking the course
  15. //It's checking new requirement to see if after adding,
  16. //we can reach all course. If it's not possible, end this program.
  17.  
  18. void requirement(bool Tab[],int n, int Where, int KeyWord){
  19. for(int i=Where*n;i<Where*n+n;i++){
  20. if(Tab[i]){
  21. if (i%n==KeyWord){
  22. cout<<"NIE";
  23. exit(3);
  24. }
  25. requirement(Tab,n,i%n,KeyWord);
  26. }
  27. }
  28. }
  29.  
  30. //Push to queue result
  31. //It looks deeper into the connections and adds them to the
  32. //queue from the deepest course.
  33. void makeQueue(queue<int> *Res,bool Tab[],int n,int CurrentElement){
  34.  
  35. for(int i=CurrentElement*n;i<CurrentElement*n+n;i++)
  36. if(Tab[i])
  37. makeQueue(Res,Tab,n,i%n);
  38.  
  39. for(int i=0;i<n*n;i+=n)
  40. Tab[CurrentElement+i]=false;
  41.  
  42. Res->push(CurrentElement);
  43. }
  44.  
  45. int main() {
  46. fstream OFile;
  47. OFile.open("input.txt", ios::in);
  48. if(OFile.good()){
  49. //Getting n and m
  50. int n, m;
  51. OFile>>n;
  52. OFile>>m;
  53. //Making table for all connections between courses.
  54. bool Table [n*n];
  55. for(int i=0;i<n*n;i++)
  56. Table[i]=false;
  57.  
  58.  
  59. //Getting all connections
  60. for(int i=0,temp_1,temp_2;OFile.good();i++){
  61. OFile>>temp_1;
  62. OFile>>temp_2;
  63. if(temp_1>n-1 || temp_2>n-1 || i>m*2){
  64. cout<<"\n\nCorrupted file!\n\n";
  65. return 2;
  66. }
  67. if(temp_1==temp_2){
  68. cout<<"NIE";
  69. return 3;
  70. }else{
  71. requirement(Table,n,temp_2,temp_1);
  72. Table[temp_1*n+temp_2]=true;
  73. }
  74. }
  75. OFile.close();
  76.  
  77. queue<int> Result;
  78.  
  79. //Adding all courses in order.
  80. for (int i=0;Result.size()!=n;i++){
  81. makeQueue(&Result,Table,n,i);
  82. }
  83. cout<<"-----------\n";
  84. for(int i=0;i<n;i++){
  85. cout<<Result.front()<<" ";
  86. Result.pop();
  87. }
  88. }else{
  89. cout<<"\n\ninput.txt error!\n\n";
  90. return 1;
  91. }
  92. return 0;
  93. }
  94.  
  95. /*Output
  96.  
  97. 0 - all correct,
  98. 1 - missing files,
  99. 2 - file "input.txt" have incorrect data,
  100. 3 - it's impossible to reach all courses,
  101. 4 - Do adding not addicting :p (ExplosmEntertainment).
  102.  
  103.  
  104. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement