Skip to content

Instantly share code, notes, and snippets.

@alexa984
Created February 18, 2019 00:17
Show Gist options
  • Save alexa984/e4614fe77bd0e5d6f80c0762eaaaf3bb to your computer and use it in GitHub Desktop.
Save alexa984/e4614fe77bd0e5d6f80c0762eaaaf3bb to your computer and use it in GitHub Desktop.
python-application-task_3
#include <iostream>
#include <vector>
#include <unordered_map>
#include<unordered_set>
using namespace std;
void read_command(unordered_map<char, pair<int, int>>&, unordered_set<char>&);
int configurations(vector<vector<char>>&, int, int, unordered_map<char, pair<int, int>>&, unordered_set<char>&);
int main()
{
int rows, cols;
cin >> rows >> cols;
vector<vector<char>> cinema(rows, vector<char>(cols));
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) cin >> cinema[i][j];
int num_friends;
cin >> num_friends;
num_friends++;
char central;
cin >> central;
//coordinates from the central
unordered_map<char, pair<int, int>> coordinates;
coordinates.insert({ central, make_pair(0, 0) });
unordered_set<char> friends; //the friends' names
friends.insert(central);
for (int i = 1; i < num_friends; i++)
{
read_command(coordinates, friends);
}
cout << configurations(cinema, rows, cols, coordinates, friends) << endl;
}
void read_command(unordered_map<char, pair<int, int>>& coordinates, unordered_set<char> & friends)
{
char person, direction, source;
cin >> person >> direction >> source;
friends.insert(person);
//calculate the coordinates of this person
if (direction == 'A') //above
{
coordinates.insert({ person, make_pair(coordinates[source].first, coordinates[source].second - 1) });
}
else if (direction == 'B') //below
{
coordinates.insert({ person, make_pair(coordinates[source].first, coordinates[source].second + 1) });
}
else if (direction == 'R') //right
{
coordinates.insert({ person, make_pair(coordinates[source].first + 1, coordinates[source].second) });
}
else if (direction == 'L') //left
{
coordinates.insert({ person, make_pair(coordinates[source].first - 1, coordinates[source].second) });
}
}
int configurations(vector<vector<char>>& cinema, int n, int m, unordered_map<char, pair<int, int>>&coordinates, unordered_set<char>& friends)
{
int num_configurations = 0;
for (int y = 0; y < n; y++)
{
for (int x = 0; x < m; x++)
{
if (cinema[y][x] == '.') //the central place is free
{
bool can_fit = true;
vector<vector<char>>temp = cinema; //temp matrix with the friends placed there
unordered_set<char>::iterator i;
for (i = friends.begin(); i != friends.end(); ++i)
{
unordered_map<char, pair<int, int>>::iterator friend_coord;
friend_coord = coordinates.find(*i);
int friend_x = x + (*friend_coord).second.first;
int friend_y = y + (*friend_coord).second.second;
if (friend_x >= 0 && friend_x < m && friend_y >= 0 && friend_y < n //it doesn't go out of the boundaries
&& cinema[friend_y][friend_x] == '.') //it is not occupied
{
temp[friend_y][friend_x] = *i;
}
else
{
can_fit = false;
break;
}
}
if (can_fit)
{
num_configurations++;
//print temp
cout << " ";
for (int it2 = 0; it2 < m; it2++) cout << it2;
cout << endl;
for (int it1 = 0; it1 < n; it1++)
{
cout << it1;
for (int it2 = 0; it2 < m; it2++) cout << temp[it1][it2];
cout << endl;
}
for (int it2 = 0; it2 < m; it2++) cout << "-";
cout << endl;
}
}
}
}
return num_configurations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment