1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <string>
#include <vector>
#include <limits>
#include <sstream>
using namespace std;
using vs = vector<string>;
enum direction_t { UP, LEFT, DOWN, RIGHT };
direction_t getCode(char ch) {
if ( ch == 'U' ) return UP;
if ( ch == 'L' ) return LEFT;
if ( ch == 'D' ) return DOWN;
if ( ch == 'R' ) return RIGHT;
}
// Fill a row from a point to one end of the line or the other.
void fill_row(vs &map, int row, int col, int col_direction) {
if ( row < 0 || row > map.size() ) return;
int c = col + col_direction;
while ( c >= 0 && c < map[row].size() ) {
map[row][c] = 'X';
c += col_direction;
}
}
// Fan out a triangle of X from the given start position.
void filler(vs &map, int row, int col, direction_t direction_code ) {
static struct {
int delta_r;
int delta_c;
} position_offsets[] = {
{ -1, 0 }, // up
{ 0, -1 }, // left
{ +1, 0 }, // down
{ 0, +1 }, // right
};
if ( row < 0 || row > map.size() ) return;
if ( position_offsets[direction_code].delta_r == 0 ) {
fill_row(map, row, col, position_offsets[direction_code].delta_c);
// either side
for ( int i = 1 ; i < 3 ; i++ ) {
fill_row(map, row-i, col, position_offsets[direction_code].delta_c);
fill_row(map, row+i, col, position_offsets[direction_code].delta_c);
col += position_offsets[direction_code].delta_c;
}
} else {
// you do up/down
}
}
void fill_from(vs &map, direction_t direction_code, char where) {
for ( size_t r = 0 ; r < map.size() ; r++ ) {
size_t c = map[r].find(where);
if ( c != string::npos ) {
filler(map,r,c,direction_code);
}
}
}
void read_input(vs &map, direction_t &direction_code) {
int n_rows, n_cols;
string direction;
cin >> n_rows >> n_cols >> direction;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
direction_code = getCode(direction[0]);
for ( int r = 0 ; r < n_rows ; r++ ) {
string line;
getline(cin,line);
if( line.length() != n_cols ) {
cout << "Not a row:" << line << endl;
} else {
map.push_back(line);
}
}
}
int main ( ) {
direction_t direction_code;
vs map;
read_input(map, direction_code);
fill_from(map, direction_code, 'P');
for ( auto &&s : map ) {
cout << s << endl;
}
}
|