Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Fall 2011 -- CSC 2700 Section 02

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

4/n/11831AW.cpp

/*
 *  Author: Andre Wiggins
 *    Date: 9/12/2011
 * Purpose: Homework 2
 * Problem: 11831
 */

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string>

#define TRUE (1 == 1)
#define FALSE (1 != 1)

#define NORTH 'N'
#define SOUTH 'S'
#define EAST 'L'
#define WEST 'O'

#define RIGHT 'D'
#define LEFT 'E'
#define FORWARD 'F'

#define CELL '.'
#define STICKER '*'
#define PILLAR '#'

using namespace std;

class Board {
public:
    unsigned int rows, columns, stickercount;
    char** board;

    Board (unsigned int r, unsigned int c);
    ~Board();
    char lookupCell(unsigned int* cell);
    void setCell(unsigned int* cell, char value);
    bool isValidCell(unsigned int* cell);
    bool isStickerCell(unsigned int* cell);
    int pickupSticker(unsigned int* cell);
};

class Robot {
public:
    char orientation;
    unsigned int* pos;
    unsigned int stickercount;

    Robot (char o, unsigned int* p);
    ~Robot();
    void turn(char direction);
    bool moveForward(Board* board);
};

void printinfo(Robot* robot, Board* board);
int cvtDirToInt(char dir);
char cvtIntToDir(int dir);

int main() {

    /*
     * Declare variables
     */
    char tile, startorien;
    Board* board;
    Robot* robot;
    unsigned int *startpos = new unsigned int[2];
    unsigned int rows, columns, step, stepcount, i, j, stickercount = 0;
    string steps, tiles;

    while (1) {
        /*
         * Read in rows, columns and steps
         */
        cin>>rows>>columns>>stepcount;
        if (rows == 0 && columns == 0 && stepcount == 0)
            break;

        /*
         * Read in the Board and initialize Robot
         */
        board = new Board(rows, columns);
        for (i = 0; i < rows; i++) {
            cin>>tiles;
            for (j = 0; j < columns; j++) {
                tile = tiles[j];
                board->board[i][j] = tile;
                if (tile == NORTH || tile == SOUTH || tile == EAST || tile == WEST) {
                    startorien = tile;
                    startpos[0] = i;
                    startpos[1] = j;
                    //board->board[i][j] = CELL;
                } else if (tile == STICKER) {
                    stickercount += 1;
                }
            }
        }

        board->stickercount = stickercount;
        robot = new Robot(startorien, startpos);

        /*
         * Preform instructions collection stickers
         */
        cin>>steps;
        for (i = 0; i < stepcount; i++) {
            step = steps[i];
            if (step == LEFT || step == RIGHT)
                robot->turn(step);
            else if (step == FORWARD)
                robot->moveForward(board);

            //printinfo(robot, board);
            if (board->stickercount == 0)
                break;
        }

        /*
         * Print out result
         */
        cout<<robot->stickercount<<endl;

        /*
         * Clean up
         */
        stickercount = 0;
    }

    return 0;
}

void printinfo(Robot* robot, Board* board) {
    cout<<"("<<robot->pos[0]<<","<<robot->pos[1]<<") facing "<<robot->orientation<<endl;
    //cout<<"Robot Sticker Count: "<<robot->stickercount<<endl;
    //cout<<"Board Sticker Count: "<<board->stickercount<<endl;
}

Robot::Robot(char o, unsigned int* p) {
    orientation = o;
    pos = p;
    stickercount = 0;
}

Robot::~Robot() {
    delete pos;
}

bool Robot::moveForward(Board* board) {
    unsigned int newpos[2];
    newpos[0] = pos[0];
    newpos[1] = pos[1];
    if (orientation == NORTH)
        newpos[0] -= 1;
    else if (orientation == SOUTH)
        newpos[0] += 1;
    else if (orientation == EAST)
        newpos[1] += 1;
    else
        newpos[1] -= 1;

    if (board->isValidCell(newpos)) {
        pos[0] = newpos[0];
        pos[1] = newpos[1];
        if (board->isStickerCell(pos))
            stickercount += board->pickupSticker(pos);
        return true;
    } else {
        return false;
    }
}

void Robot::turn(char direction) {
    int orien = cvtDirToInt(orientation);
    if (direction == RIGHT)
        orientation = cvtIntToDir(((orien + 1) % 4));
    else
        orientation = cvtIntToDir(((orien - 1) % 4));
}

Board::Board(unsigned int r, unsigned int c) {
    rows = r; columns = c; stickercount = 0;
    board = new char*[rows];
    for (unsigned int i = 0; i < rows; ++i)
        board[i] = new char[columns];
}

Board::~Board() {
    for (unsigned int i = 0; i < rows; ++i)
        delete [] board[i];
    delete [] board;
}

char Board::lookupCell(unsigned int* cell) {
    return board[cell[0]][cell[1]];
}

bool Board::isValidCell(unsigned int* cell) {
    // Error line
    //if (cell[0] > rows-1 || cell[1] > columns)
    if (cell[0] > rows-1 || cell[1] > columns-1)
        return false;
    else if (cell[0] < 0 || cell[1] < 0)
        return false;
    else if (lookupCell(cell) == PILLAR)
        return false;
    else
        return true;
}

bool Board::isStickerCell(unsigned int* cell) {
    return board[cell[0]][cell[1]] == STICKER;
}

int Board::pickupSticker(unsigned int* cell) {
    if (isStickerCell(cell)) {
        setCell(cell, CELL);
        stickercount -= 1;
        return 1;
    } else
        return 0;
}

void Board::setCell(unsigned int* cell, char value) {
    board[cell[0]][cell[1]] = value;
}

char cvtIntToDir(int dir) {
    if (dir == 0)
        return NORTH;
    else if (dir == 1)
        return EAST;
    else if (dir == 2)
        return SOUTH;
    else
        return WEST;

}

int cvtDirToInt(char dir) {
    if (dir == NORTH)
        return 0;
    else if (dir == EAST)
        return 1;
    else if (dir == SOUTH)
        return 2;
    else
        return 3;
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Isaac Traxler