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
|
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <stdexcept>
using namespace std;
void flood_fill(vector<string>& file, int x, int y, char newchar, int x_len, int y_len, char oldchar = 0)
{
if(!oldchar) oldchar = file[y][x];
if(file[y][x] != oldchar)
{
return;
}
oldchar = file[y][x];
file[y][x] = newchar;
if(x > 0) flood_fill(file, x-1,y,newchar, x_len,y_len,oldchar); //left
if(y > 0) flood_fill(file, x, y -1, newchar,x_len,y_len, oldchar); //up
if(x < x_len-1) flood_fill(file, x+1, y, newchar,x_len,y_len, oldchar); //right
if(y < y_len-1) flood_fill(file, x, y+1, newchar,x_len,y_len,oldchar); //down
}
int main(int argc, char* argv[])
{
if(argc != 2) {std::cerr << "The syntax is program.exe file"; return -1;}
ifstream file;
string content;
string input;
vector<string> entire_file;
vector<string> params;
file.open(argv[1]);
if(!file){ std::cout << "The inputed file is not available."; return -1;}
while(getline(file,content))if(!content.empty())entire_file.push_back(content);
while(true)
{
std::cout << "Enter x, y, and filler (-1 to quit): ";
std::getline(std::cin, input);
if(input == "-1") break;
std::stringstream a (input);
while(getline(a, input, ' '))
params.push_back(input);
try
{
std::stoi(params[0]);
std::stoi(params[1]);
if(params[2].size() != 1 || std::stoi(params[0]) < 0 || std::stoi(params[0]) > entire_file[0].size() || std::stoi(params[1]) > entire_file.size() || std::stoi(params[1]) < 0) throw std::invalid_argument("");
}
catch(...)
{
std::cout << "The syntax is x y filler. Make sure x and y are in limits.\n\n";
return -2;
}
file.close();
std::cout << "Filling ("<<params[0]<<","<<params[1]<<")\'"<<params[2]<<"\'\n";
flood_fill(entire_file, std::stoi(params[0]), std::stoi(params[1]), params[2][0], entire_file[0].size(), entire_file.size());
ofstream file_;
file_.open(argv[1], ios_base::trunc);
for(auto a : entire_file)
{
file_ << a << "\n";
}
break;
}
return 0;
}
|