#include #include #include #include #include using namespace std; pair getArrowVector(int arrowType) { switch (arrowType) { case 0: return make_pair(-1, 0); case 1: return make_pair(-1, 1); case 2: return make_pair(0, 1); case 3: return make_pair(1, 1); case 4: return make_pair(1, 0); case 5: return make_pair(1, -1); case 6: return make_pair(0, -1); case 7: return make_pair(-1, -1); } return make_pair(0, 0); } bool isValidCell(pair newCell, int C, int R) { return newCell.first >= 0 && newCell.first < R && newCell.second >= 0 && newCell.second < C; } int main() { ifstream infile("flowfield.in"); int C, R, S; infile >> C >> R >> S; vector>>> table(R, vector>>(C)); int arrow; for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { infile >> arrow; table[r][c] = make_pair(arrow, make_pair(1, 0)); } } bool flip = false; for (int s = 0; s < S; s++) { for (int r = 0; r < R; r++) { for (int c = 0; c < C; c++) { pair arrowVector = getArrowVector(table[r][c].first); pair newCell = make_pair(r + arrowVector.first, c + arrowVector.second); if (isValidCell(newCell, C, R)) { if (flip) { table[newCell.first][newCell.second].second.first += table[r][c].second.second; } else { table[newCell.first][newCell.second].second.second += table[r][c].second.first; } } if (flip) { table[r][c].second.second = 0; } else { table[r][c].second.first = 0; } } } flip = !flip; } ofstream outfile("flowfield.out"); int r = 0; while (r < R) { for (int c = 0; c < C; c++) { int value = flip ? table[r][c].second.second : table[r][c].second.first; outfile << value; if (c < C - 1) outfile << " "; } r++; outfile << endl; } return 0; }