UVA Solution 331 - Mapping the Swaps - Solution in C, C++ | Volume 3 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Friday, May 5, 2017

UVA Solution 331 - Mapping the Swaps - Solution in C, C++ | Volume 3

UVA Solution 331 - Mapping the Swaps - Solution in C, C++ | Volume 3



UVA Online Judge Solution 331 - Mapping the Swaps | Volume 3
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=267

Problem Name: 331 - Mapping the Swaps
Problem Number : UVA - 331 - Mapping the Swaps
Online Judge : UVA Online Judge Solution
Volume: 3
Solution Language : C, C plus plus

UVA Solution 331 - Mapping the Swaps - Solution in C, C++ | Volume 3

UVA Solution 331 - Mapping the Swaps Code in C, CPP:


#include <stdio.h>
int A[10], ans, cnt;
int dfs(int n, int l) {
    int flag = 0, i, t;
    for(i = 1; i < n; i++) {
        if(A[i] < A[i-1]) {
            t = A[i], A[i] = A[i-1], A[i-1] = t;
            dfs(n, l+1);
            t = A[i], A[i] = A[i-1], A[i-1] = t;
            flag = 1;
        }
    }
    if(flag == 0) {
        if(ans > l)
            ans = l, cnt = 0;
        if(ans == l)
            cnt++;
    }
}
int main() {
    int n, cases = 0, i;
    while(scanf("%d", &n) == 1 && n) {
        for(i = 0; i < n; i++)
            scanf("%d", &A[i]);
        ans = 0xffffff, cnt = 0;
        dfs(n, 0);
        if(ans == 0)    cnt = 0;
        printf("There are %d swap maps for input data set %d.\n", cnt, ++cases);
    }
    return 0;
}

No comments:

Post a Comment