Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <list>
- #include <queue>
- using namespace std;
- class Node{
- public:
- int value;
- int id;
- };
- class Graph
- {
- int V; // No. of vertices
- // Pointer to an array containing
- // adjacency lists
- list<int> *adj;
- // A recursive function used by DFS
- void DFSUtil(int v, bool visited[]);
- public:
- int suma = 0;
- Graph(int V); // Constructor
- // function to add an edge to graph
- void addEdge(int v, int w);
- // DFS traversal of the vertices
- // reachable from v
- void DFS(int v);
- };
- Graph::Graph(int V)
- {
- this->V = V;
- adj = new list<int>[V];
- }
- void Graph::addEdge(int v, int w)
- {
- adj[v].push_back(w); // Add w to vās list.
- }
- void Graph::DFSUtil(int v, bool visited[])
- {
- // Mark the current node as visited and
- // print it
- visited[v] = true;
- cout << v << " ";
- // Recur for all the vertices adjacent
- // to this vertex
- list<int>::iterator i;
- for (i = adj[v].begin(); i != adj[v].end(); ++i)
- if (!visited[*i]){
- suma++;
- DFSUtil(*i, visited);
- }
- }
- // DFS traversal of the vertices reachable from v.
- // It uses recursive DFSUtil()
- void Graph::DFS(int v)
- {
- // Mark all the vertices as not visited
- bool *visited = new bool[V];
- for (int i = 0; i < V; i++)
- visited[i] = false;
- // Call the recursive helper function
- // to print DFS traversal
- DFSUtil(v, visited);
- cout << "Suma: " << suma << endl;
- }
- int main(){
- int ile;
- cin >> ile;
- for(int i = 0; i < ile; i++){
- int width;
- int height;
- cin >> width >> height;
- Node tab[width][height];
- Graph graph(width * height);
- int counter = 0;
- for(int j = 0; j < height; j++){
- for(int p = 0; p < width; p++){
- Node temporary;
- temporary.value = 0;
- temporary.id = counter;
- counter++;
- char temp;
- cin >> temp;
- if(temp == '#'){
- temporary.value = 1;
- } else {
- temporary.value = 0;
- }
- tab[j][p] = temporary;
- }
- }
- vector <int> freeNodes;
- for(int j = 0; j < height; j++){
- for(int p = 0; p < width; p++){
- if(tab[j][p].value==0){
- freeNodes.push_back(tab[j][p].id);
- if(tab[j+1][p].value == 0){
- graph.addEdge(tab[j][p].id, tab[j+1][p].id);
- //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j+1][p].id << endl;
- }
- if(tab[j-1][p].value == 0){
- graph.addEdge(tab[j][p].id, tab[j-1][p].id);
- //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j-1][p].id << endl;
- }
- if(tab[j][p+1].value == 0){
- graph.addEdge(tab[j][p].id, tab[j][p+1].id);
- //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j][p+1].id << endl;
- }
- if(tab[j][p-1].value == 0){
- //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j][p-1].id << endl;
- graph.addEdge(tab[j][p].id, tab[j][p-1].id);
- }
- }
- }
- }
- for(int c = 0; c < freeNodes.size();c++){
- graph.suma = 0;
- graph.DFS(freeNodes.at(c));
- cout << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement