UVa: 10443 (Rock, Scissors, Paper)



// http://uva.onlinejudge.org/external/104/10443.html
// Runtime: 0.775s
// Tag: Adhoc 

//
//  main.cpp
//  UVa
//
//  Created by Shahab Uddin on 10/17/13.
//  Copyright (c) 2013 Shahab Uddin. All rights reserved.
//

#include <iostream>
#include <cstdio>

using namespace std;

char original [100 + 2] [100 + 2];

char update [100 + 2] [100 + 2];

void copy (int r, int c)
{
    for ( int i = 0; i < r; i++ ) {
        for ( int j = 0; j < c; j++ )
            original [i] [j] = update [i] [j];
    }
}

void takeInput (int r, int c)
{
    for ( int i = 0; i < r; i++ ) {
        scanf ("%s", update [i]);
    }
    
    // original and update array should match from the beginning
    // in case we don't need to change anything
    copy(r, c);
}

/**
 * check for invalid index
 */
bool isValid(int nx, int ny, int r, int c)
{
    if (nx < 0 || nx >= r || ny < 0 || ny >= c) return false;
    return true;
}

void simulateFight(int cx, int cy, int nx, int ny)
{
    if (original [cx] [cy] == original [nx] [ny]) return;
    
    switch (original [cx] [cy]) {
            
            case 'R':
            if (original [nx] [ny] == 'S') {
                update [nx] [ny] = 'R';
            } else {    // P
                update [cx] [cy] = 'P';
            }
            break;
            
            case 'S':
            if (original [nx] [ny] == 'R') {
                update [cx] [cy] = 'R';
            } else {    // P
                update [nx] [ny] = 'S';
            }
            break;
            
            case 'P':
            if (original [nx] [ny] == 'R') {
                update [nx] [ny] = 'P';
            } else {    // S
                update [cx] [cy] = 'S';
            }
            break;
    }
 }

void doTheFight(int r, int c)
{
    int dr [] = {-1, 0, 1, 0};
    int dc [] = {0, 1, 0, -1};
    
    for ( int i = 0; i < r; i++ ) {
        for ( int j = 0; j < c; j++ ) {
            
            int nx;
            int ny;
            
            for ( int k = 0; k < 4; k++ ) {
                
                nx = i + dr [k];
                ny = j + dc [k];
                
                if (isValid(nx, ny, r, c)) {
                    
                    simulateFight(i, j, nx, ny);
                }
                
            }
        }
    }
}

void print(int r, int c)
{
    for ( int i = 0; i < r; i++ ) {
        for ( int j = 0; j < c; j++ ) {
            printf ("%c", original [i] [j]);
        }
        
        printf ("\n");
    }
}

int main(int argc, const char * argv[])
{

    int testCases;
    
    scanf ("%d", &testCases);
    
    bool blank = false;
    
    while ( testCases-- ) {
        
        int r;
        int c;
        int n;
        
        scanf ("%d %d %d", &r, &c, &n);
        
        takeInput(r, c);
        
        while (n--) {
            
            doTheFight(r, c);
            
            copy(r, c);
            
        }
        
        if (blank) printf ("\n");
        blank = true;
        
        print(r, c);

    }
    
    return 0;
}

One thought on “UVa: 10443 (Rock, Scissors, Paper)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s