Created
September 18, 2016 18:23
-
-
Save blogdarkspot/8e26eb27b228a6b0b1cd12c92b532fd4 to your computer and use it in GitHub Desktop.
problema da antenna
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <queue> | |
#include <string> | |
using namespace std; | |
struct door{ | |
int _x; | |
int _y; | |
bool operator !=(const door &comp) const { | |
if (this->_x == comp._x && this->_y == comp._y) | |
return false; | |
return true; | |
} | |
}; | |
typedef door door; | |
/** | |
* Entrada de dados | |
*/ | |
bool input_data(vector< vector< vector<char> > > &building, queue<door> &start) { | |
int _floors, _antenna, _vertical, _horizontal; | |
string _line; | |
cin >> _floors; | |
if(_floors == 0) return false; | |
cin >> _antenna >> _vertical >> _horizontal; | |
cin.ignore(); | |
queue<door> _start; | |
building.clear(); | |
building.resize(_floors); | |
for (int z = 0; z < _floors; z++) { | |
building[z].resize(_vertical); | |
for(int y = 0; y < _vertical; y++) { | |
building[z][y].resize(_horizontal); | |
getline(cin, _line); | |
for(int x=0; x < _horizontal; x++) { | |
building[z][y][x] = _line[x]; | |
switch (_line[x]) { | |
case 'T' : | |
case 'D' : { | |
door tmp; | |
tmp._x = x; | |
tmp._y = y; | |
_start.push(tmp); | |
break; | |
} | |
} | |
} | |
} | |
} | |
start = _start; | |
return true; | |
} | |
/** | |
* retorna a nova posição | |
*/ | |
door new_position(door current, const int &y, const int &x) { | |
door _tmp; | |
_tmp._x = current._x + x; | |
_tmp._y = current._y + y; | |
return _tmp; | |
} | |
/** | |
* Algoritmo de busca | |
*/ | |
door search_exit(vector< vector<char> > &floor, door &entry) { | |
queue<door> _list; | |
_list.push(entry); | |
door _current; | |
while(!_list.empty()) { | |
_current = _list.front(); | |
_list.pop(); | |
//cout << "{" << _current._y << "," << _current._x << "}" << endl; | |
if(floor[_current._y][_current._x] == 'S' || floor[_current._y][_current._x] == 'A'){ | |
return _current; | |
} | |
floor[_current._y][_current._x] = '#'; | |
if(_current._y > 0 && floor[_current._y - 1][_current._x] != '#') { | |
_list.push(new_position(_current, -1, 0)); | |
} | |
if(_current._y < floor.size() - 1 && floor[_current._y + 1][_current._x] != '#') { | |
_list.push(new_position(_current, 1, 0)); | |
} | |
if(_current._x > 0 && floor[_current._y][_current._x - 1] != '#') { | |
_list.push(new_position(_current, 0,-1)); | |
}set_zero | |
if(_current._x < floor[0].size() - 1 && floor[_current._y][_current._x + 1] != '#') { | |
_list.push(new_position(_current, 0, 1)); | |
} | |
} | |
return _current; | |
} | |
int main(int argc, char **argv) { | |
vector< vector< vector<char> > > building; queue<door> start; | |
while (input_data(building, start)) { | |
door _exit; | |
bool _find_antenna = false; | |
for(auto &floor : building) { | |
_exit = search_exit(floor, start.front()); | |
start.pop(); | |
if (floor[_exit._y][_exit._x] == 'A') { | |
_find_antenna = true; | |
break; | |
} | |
if(_exit != start.front()) { | |
break; | |
} | |
} | |
if (_find_antenna) | |
cout << "viavel" << endl; | |
else | |
cout << "inviavel" << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment