Wednesday, May 10, 2017

URI Solution 1162 Train Swapping | Data Structures

URI Solution 1162 Train Swapping | Data Structures


URI Online Judge Solution 1162 Train Swapping | Data structures
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1162

Problem Name: URI Problem 1162 Train Swapping
Problem Number : URI Problem 1162 Train Swapping Solution
Online Judge : URI Online Judge Solution
Level: Data structures
Solution Language : C, C plus plus

URI Solution 1162 Train Swapping | Data Structures

URI Solution 1162 Train Swapping  Code in C/CPP:


#include <cstdio>
#include <algorithm>

using namespace std;

int train[51];

int lookup(int l, int x)
{
    for (int i = 0; i < l; ++i)
        if(train[i] == x)
            return i;
 
    return -1;
}

int main(int argc, char const *argv[])
{
 int n, l, x, i, c, tmp;

 scanf("%d", &n);

 while(n--)
 {
  scanf("%d", &l);
  for (i = 0; i < l; ++i)
   scanf("%d", &train[i]);

  i = 0; c = 0;

  while(i < l)
  {
   while(train[i] != (i + 1))
   {
    tmp = lookup(l, i + 1);
    swap(train[tmp - 1], train[tmp]);
    c++;
   }
   i++;
  }

  printf("Optimal train swapping takes %d swaps.\n", c);
 }

 return 0;
}


Tag: Uri solve 1162 Train Swapping, Uri solution 1162 Train Swapping, URI oj Solve 1162 Train Swapping

No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.