-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17142.cpp
95 lines (83 loc) · 1.62 KB
/
17142.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdio.h>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
int _x[4]={-1,1,0,0};
int _y[4]={0,0,-1,1};
using namespace std;
int map[50][50];
vector<pair<int,int> >v;
vector<pair<int,int> >pick;
queue<tuple<pair<int,int>,int> >q;
int m,n;
int visit[50][50];
int max;
int Empty;
int tmp;
int Min=999999;
void back(int x,int start){
if(x==n){
// for(int i=0;i<pick.size();i++){
// printf("%d %d ",pick[i].first,pick[i].second);
// }
// printf("\n");
memcpy(visit,map,sizeof(map));
int empty_tmp=Empty;
while(!q.empty()){
q.pop();
}
for(int i=0;i<pick.size();i++){
q.push(make_pair(pick[i],0));
}
while(!q.empty()){
if(empty_tmp==0){
break;
}
for(int i=0;i<4;i++){
int xx=_x[i]+q.front().first.first;
int yy=_y[i]+q.front().first.second;
if(xx>=0&&xx<m&&yy>=0&&yy<m&&(visit[xx][yy]==0||visit[xx][yy]==999999)){
if(visit[xx][yy]!=999999){
empty_tmp--;
tmp=q.front().second+1;
}
visit[xx][yy]=q.front().second+1;
q.push(make_pair(make_pair(xx,yy),q.front().second+1));
}
}
q.pop();
}
if(empty_tmp==0&&tmp<Min){
Min=tmp;
}
}else{
if(start<v.size()){
pick.push_back(v[start]);
back(x+1,start+1);
pick.pop_back();
back(x,start+1);
}
}
}
int main(){
scanf("%d %d",&m,&n);
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==2){
v.push_back(make_pair(i,j));
map[i][j]=999999;
}else if(map[i][j]==0){
Empty++;
}
}
}
back(0,0);
if(Min==999999){
printf("-1");
}else{
printf("%d\n",Min);
}
}