-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow.cpp
75 lines (66 loc) · 1.57 KB
/
flow.cpp
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
68
69
70
71
72
73
74
75
#include<iostream>
#include<vector>
#include<cstring>
#include<string>
using namespace std ;
string g[50];
string usd[ 50 ];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
int N;
bool poda = false;
int k ;
vector< vector<pair<int,int> > > pos( 50 );
void back( int x, int y, int T, int cur ){
if ( poda ) return;
if ( T == 0 && cur == k/2 ) {
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ )
cout << usd[i][j] <<" ";
cout << endl;
}
cout << endl;
poda = true;
return;
}
int num ;
if ( isdigit( usd[x][y] ) ) num = usd[x][y] - '0';
else num = usd[x][y] - 'A' + 10;
if ( cur == num && x == pos[cur][1].first && y == pos[cur][1].second ) {
back( pos[ cur+1 ][0].first, pos[ cur+1 ][0].second, T , cur + 1 );
return ;
}
for( int i = 0; i < 4; i++){
int nx = x +dx[i];
int ny = y +dy[i];
if ( nx < 0 || ny < 0 || nx >= N || ny >= N ) continue;
if ( (nx == pos[cur][1].first && ny == pos[cur][1].second ) || usd[nx][ny] == 'X' ) {
int r = (usd[nx][ny] == 'X');
char p ;
if ( isdigit( usd[x][y] ) ) p = cur + '0';
else p = cur + 'A' - 10;
usd[nx][ny] = p;
back( nx, ny, T - r , cur);
if ( r )
usd[nx][ny] = 'X';
}
}
}
int main(){
cin >> N;
k = 0;
for(int i = 0; i < N; i++) {
cin >> usd[i];
for( int j = 0; j < usd[i].size() ; j++){
if ( usd[i][j] != 'X' ){
if ( isdigit( usd[i][j] ) )
pos[ usd[i][j] - '0' ].push_back( make_pair( i , j ) );
else
pos[ usd[i][j] - 'A' + 10 ].push_back( make_pair( i , j ) );
k++;
}
}
}
back(pos[1][0].first,pos[1][0].second,N*N-k, 1);
return 0;
}