Alaska uva judge Solution

This is an easy problem from uva judge http://uva.onlinejudge.org/external/118/11850.html

How I did it

  • I only use sorting stl and 1d array manipulation
  • Save the information about station positions in an array
  • Sort the array in non decreasing order
  • Iterate through the array and check if all adjacents  array positions are less or equal to 200
  • Check the energy and distance for the last station. (The car must go to mille 1422 and return again). for this you can use this formula: 2*(1422 – vec[station-1])>200.
  • Print the result

Hints

  • Always the car began the travel with enough energy. In the position 0 has the first charge.
  • Check the last station distance. Keep in mind that the car must travel to the mille 1422 and return again. Use the formula
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int station, position;
bool flag;
while(scanf("%d", &station)){
if(!station)
    break;
vector<int> vec;
    for(int a=1; a<=station;a++)
    {
        scanf("%d", &position);
        vec.push_back(position);
    }
    sort(vec.begin(), vec.end());

            flag =  true;
            int a,b;
            for(a=0, b=1; b<vec.size(); a++,b++)
            {
                if(vec[b] -  vec[a] > 200)
                {
                    flag = false;
                    break;
                }
            }

            if(flag == true)
            {
                if(2*(1422 - vec[station-1])>200)
                    printf("IMPOSSIBLE\n");
                else
                    printf("POSSIBLE\n");
            }
            else
                printf("IMPOSSIBLE\n");

}
return 0;
}

Mine Sweeper uva solution

This is an easy problem from http://uva.onlinejudge.org/external/102/10279.html

Is an 2d array manipulation

How I did it

  • Read the number of cell is a matrix n x n
  • Init three matrix. a) the matrix with the bombs, b) tha matrix with the touched spots c) a result matrix (Initilly with -1 all the grid)
  • Check the bombs for each cell

Hints

  • You need to print one blank line between the outputs, the last line doesn’t need it.
  • If a bomb is touched you need to show all the bombs in the output.
  • Be careful with the limit of a grid.
  • A grid can have maximum 8 bombs

#include <cstdio>
#include <iostream>
#define MAX 12
using namespace std;

char grid[MAX][MAX], game[MAX][MAX];
int result[MAX][MAX];

void setGrid(int cell){

string tmp = "", cad;
for(int x=0; x<cell; x++)
{
    cin>>cad;
    tmp+=cad;
}
int index = 0;

for(int a=0; a<cell; a++)
    for(int b=0; b<cell; b++,index++)
       grid[a][b] = tmp[index];

}
void setGame(int cell){

string tmp = "", cad;
for(int x=0; x<cell; x++)
{
    cin>>cad;
    tmp+=cad;
}
int index = 0;

for(int a=0; a<cell; a++)
    for(int b=0; b<cell; b++,index++)
       game[a][b] = tmp[index];

}

void init(int cell){
for(int a=0; a<cell; a++)
    for(int b=0; b<cell; b++)
         result[a][b] =  -1;
}

void print(int cell){
    for(int a=0; a<cell; a++)
    {
        for(int b=0; b<cell; b++)
        {
           if(result[a][b] == 12)
              printf("*");
           else if(result[a][b] == -1)
                  printf(".");
           else
                  printf("%d", result[a][b]);
        }
        printf("\n");
    }
}

void getBombs(int r, int c, int limit){
int tot = 0;
    if(r-1 >= 0)
    {
        if(grid[r-1][c] == '*')
           tot++;
        if(c+1 < limit)
           if(grid[r-1][c+1] == '*')
              tot++;
        if(c-1 >= 0)
           if(grid[r-1][c-1] == '*')
              tot++;
    }
    if(r+1 < limit)
    {
        if(c+1 < limit)
           if(grid[r+1][c+1] == '*')
              tot++;
        if(c-1 >= 0)
           if(grid[r+1][c-1] == '*')
              tot++;
        if(grid[r+1][c] == '*')
           tot++;
    }
    // same row differetn column
    if(c+1 < limit)
       if(grid[r][c+1] == '*')
          tot++;
    if(c-1 >= 0)
       if(grid[r][c-1] == '*')
          tot++;
    result[r][c] = tot;
}

void showBombs(int cells){
for(int g=0; g<cells; g++)
    for(int h=0; h<cells; h++)
       if(grid[g][h] == '*')
          result[g][h] = 12;
}

int main(){

int casos, cells;
scanf("%d", &casos);

while(casos--){
scanf("%d", &cells);
setGrid(cells);
setGame(cells);
init(cells);
// init the algorithm for
for(int a=0; a<cells; a++)
{
    for(int b=0; b<cells; b++)
    {
        if(game[a][b] == 'x')
        {
            if(grid[a][b] == '.')
               getBombs(a,b, cells);
            else if(grid[a][b] == '*')
                    showBombs(cells);
        }
    }
}
print(cells);
if(casos)
   printf("*\n");
}

return 0;
}

Uva 10126 – Zipf’s Law

This is an easy problem in Uva judge.

http://uva.onlinejudge.org/external/101/10126.html

How I did it:

  • Get n. (Number of occurrences that the problem asks validate)
  • I read line per line
  • I get the words from the read line
  • Put the words int a stl map<string,int>
  • Iterate in the map and if the int is equal to n , print the word.
  • If no word appears n times print “There is no such word.”

Hints:

  • Print a blank line between  cases. The last case doesn’t have a blank line.
  • If no word appears n times print “There is no such word.”
  • The words must be sorted.

Code:

In my github : https://github.com/Fabho/algorithmsSolved/blob/master/10126.cpp

#include <iostream>
#include <cstdio>
#include <cctype>
#include <map>
using namespace std;
map<string,int> myMap;

void getWords(string cad){
string tmp = "";

for(int x=0; x<cad.size(); x++)
   {
     if(isalpha(cad[x]))
        tmp+=tolower(cad[x]);
     else{
           if(tmp.size() > 0)
           {
             myMap[tmp]++;
             tmp = "";
           }

         }

   }
 if(tmp.size() > 0)
    myMap[tmp]++;
 tmp = "";
}

int main(){
int n, casos = 0;
string cad;

while(scanf("%d",&n) != EOF){
    getline(cin,cad);//trash
    while(getline(cin,cad)){
    if(cad == "EndOfText")
       break;
    getWords(cad);
    }
    bool flag = false;

    if(casos == 0)
       casos = 1;
    else
        printf("\n");
    for(map<string,int>::iterator it = myMap.begin(); it != myMap.end(); it++)
    {
        if(it->second == n)
           {
            printf("%s\n", it->first.c_str());
            flag = true;
           }
    }
    if(flag == false)
       printf("There is no such word.\n");
    myMap.clear();
}
return 0;
}