Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "graphics.hpp"
- #include <vector>
- #include <fstream>
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- using namespace genv;
- typedef vector<vector<int> > mat_t;
- int XX;
- int YY;
- void beolvas(mat_t &v, string fnev="terkep.txt")
- {
- ifstream fin(fnev);
- if(!fin.good()){
- cerr << fnev << " nem talalhato\n";
- exit(1);
- }
- fin >> XX >> YY;
- v.resize(XX);
- for(size_t i = 0; i < XX; ++i)
- {
- v[i].resize(YY);
- for(size_t j = 0; j < YY; ++j)
- {
- fin >> v[i][j];
- }
- }
- }
- void kirajzol(const mat_t &v, canvas& c)
- {
- for(size_t i = 0; i < v.size(); ++i)
- {
- for(size_t j = 0; j < v[i].size(); ++j)
- {
- int g,b;
- int norm_h = 127+v[i][j];
- g = norm_h;
- if(v[i][j] < 0) b = 255+v[i][j];
- else b = 0;
- c << move_to(i,j) << color(0,g,b) << dot;
- }
- }
- c << refresh;
- }
- void tr(canvas& out, canvas& background){
- out<<stamp(background,0,0);
- }
- struct koord{
- int x,y;
- koord():x(0),y(0){}
- koord(int _x, int _y):x(_x), y(_y){}
- };
- void szigetbejaras(mat_t &terkep, int startx, int starty, int celx, int cely){ /// & többinél kell ?
- vector<vector<koord>> honnan(XX, vector<koord> (YY,koord(-1,-1))); ///létrehoz egy térkép méretű (-1,-1) koordinátákból álló vektort
- mat_t latogatottak(XX,vector<int>(YY,0));
- vector<koord> sor(1,koord(startx,starty));
- while(!sor.empty()) {
- koord aktualis=sor.front();
- int x=aktualis.x, y=aktualis.y;
- sor.erase(sor.begin());
- if(x>=0 && x<XX && y>=0 && y<YY && terkep[x][y]>0) {
- latogatottak[x][y]=1;
- if(x-1>0 && x-1<XX && terkep[x-1][y]>0 && latogatottak[x-1][y]==0) {
- sor.push_back(koord(x-1,y));
- latogatottak[x-1][y]=1;
- honnan[x-1][y]=koord(x,y);
- }
- if(x+1>0 && x+1<XX && terkep[x+1][y]>0 && latogatottak[x+1][y]==0) {
- sor.push_back(koord(x+1,y));
- latogatottak[x+1][y]=1;
- honnan[x+1][y]=koord(x,y);
- }
- if(y-1>0 && y-1<YY && terkep[x][y-1]>0 && latogatottak[x][y-1]==0) {
- sor.push_back(koord(x,y-1));
- latogatottak[x][y-1]=1;
- honnan[x][y-1]=koord(x,y);
- }
- if(y+1>0 && y+1<YY && terkep[x][y+1]>0 && latogatottak[x][y+1]==0) {
- sor.push_back(koord(x,y+1));
- latogatottak[x][y+1]=1;
- honnan[x][y+1]=koord(x,y);
- }
- }
- }
- int X=celx,Y=cely;
- while(!(X==startx && Y==starty) || sor.size()==0) {
- gout << move_to(X,Y) << color(255,0,0) << dot << refresh;
- X=honnan[X][Y].x;
- Y=honnan[X][Y].y;
- }
- }
- int main() {
- mat_t terkep;
- beolvas(terkep);
- gout.open(XX, YY);
- canvas hatter(XX,YY);
- kirajzol(terkep,hatter);
- tr(gout, hatter);
- gout << refresh;
- event ev;
- int startx=-1;
- int starty=-1;
- int celx=-1;
- int cely=-1;
- while (genv::gin >> ev && ev.keycode != key_escape) {
- if(ev.type == ev_mouse && ev.button == btn_left && terkep[ev.pos_x][ev.pos_y]>0) {
- gout << move_to(ev.pos_x,ev.pos_y) << color(255,0,0) << dot <<refresh;
- startx=ev.pos_x;
- starty=ev.pos_y;
- }
- if(startx!=-1 && ev.type == ev_mouse && ev.button == btn_right && terkep[ev.pos_x][ev.pos_y]>0){
- gout << move_to(ev.pos_x,ev.pos_y) << color(255,0,0) << dot <<refresh;
- celx=ev.pos_x;
- cely=ev.pos_y;
- szigetbejaras(terkep, startx, starty, celx, cely);
- gout << refresh;
- }
- }
- cout << celx << ',' << cely << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement