Skip to content

Instantly share code, notes, and snippets.

@jason790228
Created October 10, 2017 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason790228/e0c132c6939be259ae2f2e96b4da47e2 to your computer and use it in GitHub Desktop.
Save jason790228/e0c132c6939be259ae2f2e96b4da47e2 to your computer and use it in GitHub Desktop.
11518
#include "Dominos.h"
#include <iostream>
using namespace std;
int main()
{
int test_case_number;
cin >> test_case_number;
for (int i = 0; i < test_case_number; i++)
{
int n, m, l;
cin >> n >> m >> l;
Dominos test_case(n);
for (int j = 0; j < m; j++)
{
int x, y;
cin >> x >> y;
test_case.add_rule(x, y);
}
for (int k = 0; k < l; k++)
{
int z;
cin >> z;
test_case.add_action(z);
}
cout << test_case.paly() << endl;
}
return 0;
}
#include "Dominos.h"
using namespace std;
int Dominos::paly()
{
for (size_t i = 0; i < m_action.size(); i++) go(m_action[i]);
int result(0);
for (size_t i = 1; i < m_tile_status.size(); i++)
{
if (!m_tile_status[i]) result++;
}
return result;
}
void Dominos::go(const int &tile_index)
{
m_tile_status[tile_index] = false;
for (size_t i = 0; i < m_rule[tile_index].size(); i++)
{
if (m_tile_status[m_rule[tile_index][i]]) go(m_rule[tile_index][i]);
}
}
#include <vector>
class Dominos
{
public:
Dominos(const int &tile_number)
{
m_rule.resize(tile_number + 1);
m_tile_status.resize(tile_number + 1, true);
};
void add_rule(const int &index, const int &data) { m_rule[index].push_back(data); };
void add_action(const int &data) { m_action.push_back(data); };
int paly();
private:
void go(const int &tile_index);
private:
std::vector<std::vector<int>> m_rule;
std::vector<int> m_action;
std::vector<bool> m_tile_status;
};
@yipo
Copy link

yipo commented Oct 11, 2017

  • paly (adj.) 蒼白的、發青的。應該是 play?
  • m_rulem_tile_status 應使用 initialization list 初始化
  • m_tile_status 的 status 意義較模糊 (falled or stand)
  • 選擇正確的容器:第二維度使用 set 較符合定義。(順序無關,元素不應重複)
  • action、go 意義也很模糊
  • m_tile_status[m_rule[tile_index][i]] 很長,建議改用 ranged for-loop。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment