Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program gets data from file "input.txt" and return queue of correct order
- //of the courses or NIE if it's impossible to reach all course
- //If it's possible, it return queue and code 0. If it's not possible,
- //return NIE and code 5.
- #include <iostream>
- #include <fstream>
- #include <queue>
- using namespace std;
- //Checking the course
- //It's checking new requirement to see if after adding,
- //we can reach all course. If it's not possible, end this program.
- void requirement(bool Tab[],int n, int Where, int KeyWord){
- for(int i=Where*n;i<Where*n+n;i++){
- if(Tab[i]){
- if (i%n==KeyWord){
- cout<<"NIE";
- exit(3);
- }
- requirement(Tab,n,i%n,KeyWord);
- }
- }
- }
- //Push to queue result
- //It looks deeper into the connections and adds them to the
- //queue from the deepest course.
- void makeQueue(queue<int> *Res,bool Tab[],int n,int CurrentElement){
- for(int i=CurrentElement*n;i<CurrentElement*n+n;i++)
- if(Tab[i])
- makeQueue(Res,Tab,n,i%n);
- for(int i=0;i<n*n;i+=n)
- Tab[CurrentElement+i]=false;
- Res->push(CurrentElement);
- }
- int main() {
- fstream OFile;
- OFile.open("input.txt", ios::in);
- if(OFile.good()){
- //Getting n and m
- int n, m;
- OFile>>n;
- OFile>>m;
- //Making table for all connections between courses.
- bool Table [n*n];
- for(int i=0;i<n*n;i++)
- Table[i]=false;
- //Getting all connections
- for(int i=0,temp_1,temp_2;OFile.good();i++){
- OFile>>temp_1;
- OFile>>temp_2;
- if(temp_1>n-1 || temp_2>n-1 || i>m*2){
- cout<<"\n\nCorrupted file!\n\n";
- return 2;
- }
- if(temp_1==temp_2){
- cout<<"NIE";
- return 3;
- }else{
- requirement(Table,n,temp_2,temp_1);
- Table[temp_1*n+temp_2]=true;
- }
- }
- OFile.close();
- queue<int> Result;
- //Adding all courses in order.
- for (int i=0;Result.size()!=n;i++){
- makeQueue(&Result,Table,n,i);
- }
- cout<<"-----------\n";
- for(int i=0;i<n;i++){
- cout<<Result.front()<<" ";
- Result.pop();
- }
- }else{
- cout<<"\n\ninput.txt error!\n\n";
- return 1;
- }
- return 0;
- }
- /*Output
- 0 - all correct,
- 1 - missing files,
- 2 - file "input.txt" have incorrect data,
- 3 - it's impossible to reach all courses,
- 4 - Do adding not addicting :p (ExplosmEntertainment).
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement