-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16236.cpp
98 lines (87 loc) · 1.64 KB
/
16236.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
96
97
98
#include <stdio.h>
#include <queue>
#include <tuple>
#include <stdlib.h>
#include <string.h>
using namespace std;
int _x[4]={-1,0,0,1};
int _y[4]={0,-1,1,0};
int num;
queue<tuple<int,int,int> >q;
int map[20][20];
int visit[20][20];
int size=2;
int point =0;
int step=0;
void print(){
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
printf("%d ",map[i][j]);
}
printf(" ");
for(int j=0;j<num;j++){
printf("%d ",visit[i][j]);
}
printf("\n");
}
printf("\n");
}
int main(){
scanf("%d",&num);
int posx,posy;
for(int i=0;i<num;i++){
for(int j=0;j<num;j++){
scanf("%d",&map[i][j]);
if(map[i][j]==9){
posx=i;
posy=j;
map[i][j]=0;
}
}
}
visit[posx][posy]=1;
q.push(make_tuple(posx,posy,0));
while(!q.empty()){
int time =get<2>(q.front());
print();
printf("%d\n",step);
int x=get<0>(q.front());
int y = get<1>(q.front());
visit[xx][yy]=1;
if(map[x][y]!=0){
if(map[x][y]<size){
map[x][y]=0;
step+=time;
point++;
if(point==size){
size++;
point=0;
}
memset(visit,0,sizeof(visit));
visit[xx][yy]=1;
while(!q.empty()){
q.pop();
}
q.push(make_tuple(x,y,0));
q.push(make_tuple(x,y,0));
break;
}else if(map[x][y]==size){
visit[x][y]=1;
q.push(make_tuple(x,y,time));
}
}else{
visit[x][y]=1;
q.push(make_tuple(xx,yy,time+);
}
for(int i=0;i<4;i++){
int xx= _x[i]+get<0>(q.front());
int yy= _y[i]+get<1>(q.front());
if(xx>=0&&xx<num&&yy>=0&&yy<num&&visit[xx][yy]==0){
// printf("%d %d %d\n",xx,yy,map[xx][yy]);
q.push(make_tuple(xx,yy,time+1));
}
}
q.pop();
}
printf("%d",step);
}