-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix_as_graph.cpp
More file actions
67 lines (58 loc) · 1.58 KB
/
matrix_as_graph.cpp
File metadata and controls
67 lines (58 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
using namespace std;
typedef long long ll;
typedef long double ld;
#define endl "\n"
#define debug(args...) cout << (#args) << " = " << (args) << endl
#define MOD 1000000007
#define vi vector<int>
#define fl forward_list
#define pb push_back
#define pf push_front
#define read(st) getline(cin, st)
#define FOR(i, a, b) for (int i = a; i < b; i++)
int n;
bool is_cell_valid(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= n)
return false;
return true;
}
int main()
{
fastio;
cin >> n;
int mat[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> mat[i][j];
vector<int> adj[(n * n) + 1];
int cont = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (is_cell_valid(i + 1, j)) // Checa se podemos ir para o sul
adj[cont].push_back(cont + 4);
if (is_cell_valid(i, j + 1)) // Checa se podemos ir para o leste
adj[cont].push_back(cont + 1);
if (is_cell_valid(i - 1, j)) // Checa se podemos ir para o norte
adj[cont].push_back(cont - 4);
if (is_cell_valid(i, j - 1)) // Checa se podemos ir para o oeste
adj[cont].push_back(cont - 1);
cont++;
}
}
cout<<endl<<endl;
for (int i = 0; i < n * n; i++)
{
cout<<i<<" ";
for (auto u : adj[i])
cout << u << " ";
cout << endl;
}
return 0;
}