Click Here to go back to the homepage.

Bounding Robots Solution:


#include <bits/stdc++.h>

using namespace std;

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int width, length;
    cin >> width >> length;
    do{
        int curr_w = 0;
        int curr_l = 0;
        int think_w = 0;
        int think_l = 0;
        int n_directions = 0;
        cin >> n_directions;
        for(int i = 0; i < n_directions; i++){
            char direction;
            cin >> direction;
            int paces;
            cin >> paces;
            switch(direction){
                case 'u':   think_l += paces;
                            curr_l = curr_l + paces >= length ? length -1 : curr_l + paces;
                            break;
                case 'd':   think_l -= paces;
                            curr_l = curr_l - paces < 0 ? 0 : curr_l - paces;
                            break;
                case 'r':   think_w += paces;
                            curr_w = curr_w + paces >= width ? width -1 : curr_w + paces;
                            break;
                case 'l':   think_w -= paces;
                            curr_w = curr_w - paces < 0 ? 0 : curr_w - paces;
                            break;
                default:    cout << "this shouldn't ever run, something went wrong." << endl;
            }
        }
        cout << "Robot thinks " << think_w << " " << think_l << endl;
        cout << "Actually at " << curr_w << " " << curr_l << endl;

        cin >> width >> length;
    } while(width > 1);
    
    return 0;
}