Monday, October 31, 2016

UVa 445 - Marvelous Mazes

// UVa 445 - Marvelous Mazes
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define is_a_digit(ch) ('0' <= ch && ch <= '9')

int row, repetitions;
bool first_maze;
vector<string> maze;

void start_new_maze() {
	maze.clear();
	row = 0;
	maze.push_back("");
	repetitions = 0;
}

void start_new_row() {
	row++;
	maze.push_back("");
	repetitions = 0;
}

void feed(char ch) {
	if (is_a_digit(ch)) {
		repetitions += (ch - '0');
	} else if (ch != '!') {
		if (ch == 'b')
			ch = ' ';
		maze[row].append(repetitions, ch);
		repetitions = 0;
	} else {
		start_new_row();
	}
}

void print_maze() {
	if (first_maze) {
		first_maze = false;
	} else {
		cout << endl;
	}
	for (int i = 0; i < maze.size() - 1; i++) {
		cout << maze[i] << endl;
	}
}

int main() {
	string line;
	first_maze = true;
	start_new_maze();
	while (getline(cin, line)) {

		if (line.size() == 0) {
			print_maze();
			start_new_maze();
			continue;
		}

		for (int i = 0; i < line.length(); i++) {
			feed(line[i]);
		}
		feed('!');
	}
	print_maze();
	return 0;
}

No comments:

Post a Comment