Showing posts with label UVA Solution Volume-1. Show all posts
Showing posts with label UVA Solution Volume-1. Show all posts

Saturday, April 29, 2017

UVA Online Judge Solution - 101 - The blocks problem in C plus plus

UVA Online Judge Solution - 101 - The blocks problem in C plus plus 


Problem PDF is :
Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies.
For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot
arm performed tasks involving the manipulation of blocks.
In this problem you will model a simple block world under certain rules and constraints. Rather
than determine how to achieve a specified state, you will “program” a robotic arm to respond to a
limited set of commands.
The problem is to parse a series of commands that instruct a robot arm in how to manipulate blocks
that lie on a flat table. Initially there are n blocks on the table (numbered from 0 to n􀀀1) with block
bi adjacent to block bi+1 for all 0 i < n 􀀀 1 as shown in the diagram below:
Initial Blocks World
The valid commands for the robot arm that manipulates blocks are:
• move a onto b
where a and b are block numbers, puts block a onto block b after returning any blocks that are
stacked on top of blocks a and b to their initial positions.
• move a over b
where a and b are block numbers, puts block a onto the top of the stack containing block b, after
returning any blocks that are stacked on top of block a to their initial positions.
• pile a onto b
where a and b are block numbers, moves the pile of blocks consisting of block a, and any blocks
that are stacked above block a, onto block b. All blocks on top of block b are moved to their
initial positions prior to the pile taking place. The blocks stacked above block a retain their order
when moved.
• pile a over b
where a and b are block numbers, puts the pile of blocks consisting of block a, and any blocks
that are stacked above block a, onto the top of the stack containing block b. The blocks stacked
above block a retain their original order when moved.
• quit
terminates manipulations in the block world.
Any command in which a = b or in which a and b are in the same stack of blocks is an illegal
command. All illegal commands should be ignored and should have no affect on the configuration of
blocks.
Input
The input begins with an integer n on a line by itself representing the number of blocks in the block
world. You may assume that 0 < n < 25.
The number of blocks is followed by a sequence of block commands, one command per line. Your
program should process all commands until the quit command is encountered.
You may assume that all commands will be of the form specified above. There will be no syntactically
incorrect commands.
Output
The output should consist of the final state of the blocks world. Each original block position numbered
i (0 i < n where n is the number of blocks) should appear followed immediately by a colon. If there
is at least a block on it, the colon must be followed by one space, followed by a list of blocks that appear
stacked in that position with each block number separated from other block numbers by a space. Don’t
put any trailing spaces on a line.
There should be one line of output for each block position (i.e., n lines of output where n is the
integer on the first line of input).
Sample Input
10
move 9 onto 1
move 8 over 1
move 7 over 1
move 6 over 1
pile 8 over 6
pile 8 over 5
move 2 over 1
move 4 over 9
quit
Sample Output
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:

UVA Online Judge Solution - 101 - The blocks problem in C plus plus Code:

#include <iostream>
#include <cstdio>
using namespace std;
 
int a [30] [30];
int n;
int first_x;
int first_y;
int second_x;
int second_y;
 
void output ()
{
    for ( int i = 0; i < n; i++ ) {
        printf ("%d:", i);
        for ( int j = 0; a [i] [j] != -1; j++ )
            printf (" %d", a [i] [j]);
        printf ("\n");
    }
 
    //printf ("\n");
}
 
void constructor ()
{
    for ( int i = 0; i < n; i++ ) {
        a [i] [0] = i;
        a [i] [1] = -1;
    }
}
 
bool validity (int first, int second)
{
    if ( first == second )
    return false;
 
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; a [i] [j] != -1; j++ ) {
            if ( first == a [i] [j] ) {
                for ( int k = 0; a [i] [k] != -1; k++ ) {
                    if ( second == a [i] [k] )
                        return false;
                }
            }
        }
    }
 
    return true;
}
 
 
void find (int first, int second)
{
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; a [i] [j] != -1; j++ ) {
            if ( a [i] [j] == first ) {
                first_x = i;
                first_y = j;
            }
 
            else if ( a [i] [j] == second ) {
                second_x = i;
                second_y = j;
            }
        }
    }
}
 
void move_onto_method (int first, int second)
{
    if ( !validity (first, second) )
    return;
 
    find (first, second);
 
    //output ();
 
    for ( int i = first_y + 1; a [first_x] [i] != -1; i++ ) {
        a [a [first_x] [i]] [0] = a [first_x] [i];
        a [a [first_x] [i]] [1] = -1;
    }
 
    a [first_x] [first_y] = -1;
 
    for ( int i = second_y + 1; a [second_x] [i] != -1; i++ ) {
        a [a [second_x] [i]] [0] = a [second_x] [i];
        a [a [second_x] [i]] [1] = -1;
    }
 
    a [second_x] [second_y + 1] = first;
    a [second_x] [second_y + 2] = -1;
 
}
 
 
void move_over_method (int first, int second)
{
    if ( !validity (first, second) )
    return;
 
    find (first, second);
 
    for ( int i = first_y + 1; a [first_x] [i] != -1; i++ ) {
        a [a [first_x] [i]] [0] = a [first_x] [i];
        a [a [first_x] [i]] [1] = -1;
    }
 
    a [first_x] [first_y] = -1;
 
    second_y++;
    while ( a [second_x] [second_y] != -1 )
        second_y++;
 
    a [second_x] [second_y] = first;
    a [second_x] [++second_y] = -1;
}
 
 
void pile_onto_method (int first, int second)
{
    if ( !validity (first, second) )
    return;
 
    find (first, second);
 
    for ( int i = second_y + 1; a [second_x] [i] != -1; i++ ) {
        a [a [second_x] [i]] [0] = a [second_x] [i];
        a [a [second_x] [i]] [1] = -1;
    }
 
    for ( int i = first_y; a [first_x] [i] != -1; i++ )
        a [second_x] [++second_y] = a [first_x] [i];
 
    a [second_x] [++second_y] = -1;
    a [first_x] [first_y] = -1;
 
}
 
 
void pile_over_method (int first, int second)
{
    if ( !validity (first, second) )
    return;
 
    find (first, second);
 
    second_y++;
    while ( a [second_x] [second_y] != -1 )
        second_y++;
 
    for ( int i = first_y; a [first_x] [i] != -1; i++ )
        a [second_x] [second_y++] = a [first_x] [i];
 
    a [second_x] [second_y] = -1;
    a [first_x] [first_y] = -1;
}
 

int main ()
{
    
    while ( cin >> n ) {
 
        constructor ();
 
        
 
        string move_pile;
        int first;
        string onto_over;
        int second;
 
        while ( cin >> move_pile ) {
 
            if ( move_pile == "quit" )
                break;
            cin >> first;
            cin >> onto_over;
            cin >> second;
 
            if ( move_pile == "move" && onto_over == "onto" ) {
                move_onto_method (first, second);
            }
 
            else if ( move_pile == "move" && onto_over == "over" ) {
                move_over_method (first, second);
            }
 
            else if ( move_pile == "pile" && onto_over == "onto" ) {
                pile_onto_method (first, second);
            }
 
            else if ( move_pile == "pile" && onto_over == "over" ) {
                pile_over_method (first, second);
            }
 
            //output ();
        }
 
        output ();
    }
 
    return 0;
}
Read More

UVA Online Judge Solution 100 - 3n + 1 Problem solution in C plus plus

UVA Online Judge Solution 100 - 3n + 1 Problem solution in C plus plus 

UVA Online Judge Solution 100 - 3n + 1 Problem solution in C plus plus Problem is:

Problem PDF:

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g.,
NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose
classification is not known for all possible inputs.
Consider the following algorithm:
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n  􀀀 3n + 1
5. else n  􀀀 n/2
6. GOTO 2
Given the input 22, the following sequence of numbers will be printed
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input
value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has
been verified, however, for all integers n such that 0 < n < 1; 000; 000 (and, in fact, for many more
numbers than this.)
Given an input n, it is possible to determine the number of numbers printed before and including
the 1 is printed. For a given n this is called the cycle-length of n. In the example above, the cycle
length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers
between and including both i and j.
Input
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers
will be less than 10,000 and greater than 0.
You should process all pairs of integers and for each pair determine the maximum cycle length over
all integers between and including i and j.
You can assume that no operation overflows a 32-bit integer.
Output
For each pair of input integers i and j you should output i, j, and the maximum cycle length for
integers between and including i and j. These three numbers should be separated by at least one space
with all three numbers on one line and with one line of output for each line of input. The integers i
and j must appear in the output in the same order in which they appeared in the input and should be
followed by the maximum cycle length (on the same line).
Sample Input
1 10
100 200
201 210
900 1000
Sample Output
1 10 20
100 200 125
201 210 89
900 1000 174


UVA Online Judge Solution 100 - 3n + 1 Problem solution in C plus plus 

#include <stdio.h>
#include <string.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define swap(x, y) {int tmp; tmp = x, x = y, y = tmp;}
#define N 1000000
#define node 1048576
int A[N+1], tree[node<<1];
void setTree(int l, int r, int k) {
    if(l == r)
        tree[k] = A[l];
    if(l < r) {
        int m = (l+r) >> 1;
        setTree(l, m, k<<1);
        setTree(m+1, r, (k<<1)+1);
        tree[k] = max(tree[k<<1], tree[(k<<1)+1]);
    }
}
int getTree(int l, int r, int k, int i, int j) {
    if(l == i && r == j)
        return tree[k];
    if(l < r) {
        int m = (l+r) >> 1;
        if(m >= j)
            return getTree(l, m, k<<1, i, j);
        else if(m < i)
            return getTree(m+1, r, (k<<1)+1, i, j);
        else
            return max(getTree(l, m, k<<1, i, m), getTree(m+1, r, (k<<1)+1, m+1, j));
    }
}
void build() {
    long long n;
    int i, len;
    memset(A, 0, sizeof(A));
    memset(tree, 0, sizeof(tree));
    A[1] = 1;
    for(i = 2; i <= N; i++) {
        n = i, len = 1;
        while(n != 1) {
            if(n&1) {
                n = n*3 + 1;
            } else {
                n >>= 1;
            }
            if(n < N && A[n] != 0) {
                len += A[n];
                break;
            }
            len ++;
        }
        A[i] = len;
    }
    setTree(1, N, 1);
}
int main() {
    build();
    int i, j;
    while(scanf("%d %d", &i, &j) == 2) {
        printf("%d %d ", i, j);
        if(i > j)
            swap(i, j);
        printf("%d\n", getTree(1, N, 1, i, j));
    }
    return 0;
}

Read More

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.