#include #include #include #include #include using namespace std; const int MAXN = 105; int n, m; int a[MAXN][MAXN]; bool used[MAXN][MAXN]; pair num2Jump[10]; vector > path[MAXN][MAXN], cycle[MAXN][MAXN]; int answer[MAXN][MAXN]; void DFS(pair x, pair start) { if(x.first<1 || x.first>n || x.second<1 || x.second>m) { cycle[start.first][start.second].push_back(x); return; } used[x.first][x.second] = true; path[start.first][start.second].push_back(x); pair y = {x.first+num2Jump[ a[x.first][x.second] ].first, x.second+num2Jump[ a[x.first][x.second] ].second}; if(used[y.first][y.second]==false) { DFS(y, start); } else { while(path[start.first][start.second].empty()==false && path[start.first][start.second].back()!=y) { cycle[start.first][start.second].push_back(path[start.first][start.second].back()); path[start.first][start.second].pop_back(); } if(path[start.first][start.second].empty()==false) { cycle[start.first][start.second].push_back(path[start.first][start.second].back()); path[start.first][start.second].pop_back(); } reverse(cycle[start.first][start.second].begin(), cycle[start.first][start.second].end()); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); ifstream cin("flowfield.in"); ofstream cout("flowfield.out"); long long S; num2Jump[0] = {-1, 0}; num2Jump[1] = {-1, 1}; num2Jump[2] = {0, 1}; num2Jump[3] = {1, 1}; num2Jump[4] = {1, 0}; num2Jump[5] = {1, -1}; num2Jump[6] = {0, -1}; num2Jump[7] = {-1, -1}; cin >> m >> n >> S; for(int i = 1;i<=n;i++) { for(int j = 1;j<=m;j++) { cin >> a[i][j]; } } for(int i = 1;i<=n;i++) { for(int j = 1;j<=m;j++) { memset(used, false, sizeof(used)); DFS({i, j}, {i, j}); /* cout << "path" << "\n"; for(pair x: path[i][j]) cout << x.first << " " << x.second << '\n'; cout << "cycle" << "\n"; for(pair x: cycle[i][j]) cout << x.first << " " << x.second << '\n'; */ } } for(int i = 1;i<=n;i++) { for(int j = 1;j<=m;j++) { int pathSize = path[i][j].size(); if(S