-
Notifications
You must be signed in to change notification settings - Fork 3
/
dfs-construction
147 lines (141 loc) · 2.91 KB
/
dfs-construction
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
const int INF=1e9+7;
int n,m;
int field[1005][1005];
int vis[1005][1005];
int vis2[1005][1005];
int res;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int sx,sy,b;
double ans;
int flag;
void dfs(int x,int y)
{
if(field[x][y]==3)
{
vis[x][y]=res;
field[x][y]=4;
}
for(int i=0;i<4;i++){
int nx=x+dx[i],ny=y+dy[i];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&field[nx][ny]==3)
{
dfs(nx,ny);
}
}
}
void dfs2(int x,int y,double ress)
{
if(field[x][y]==0)
{
cout<<"救援人员没了!"<<endl;
return;
}
if(field[x][y]==4&&vis[x][y]==b){
ans=0;
return;
}
vis2[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dx[i],ny=y+dy[i];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&field[nx][ny]!=0&&!vis2[nx][ny])
{
if(field[nx][ny]==2)
{
dfs2(nx,ny,ress+1);
}
if(field[nx][ny]==1)
{
dfs2(nx,ny,ress+3);
}
if(field[nx][ny]==4&&vis[nx][ny]==b)
{
ans=min(ans,ress+1);
}
}
}
vis2[x][y]=0;
}
void dfs3(int x,int y,double ress)
{
if(flag==1)
return;
if(field[x][y]==4&&vis[x][y]==b&&ans==0)
{
cout<<x<<" "<<y<<endl;
return;
}
vis2[x][y]=1;
for(int i=0;i<4;i++){
int nx=x+dx[i],ny=y+dy[i];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&field[nx][ny]!=0&&!vis2[nx][ny])
{
if(field[nx][ny]==2)
{
dfs3(nx,ny,ress+1);
}
if(field[nx][ny]==1)
{
dfs3(nx,ny,ress+3);
}
if(field[nx][ny]==4&&vis[nx][ny]==b&&ans==ress+1)
{
cout<<nx<<" "<<ny<<endl;
flag=1;
}
}
}
vis2[x][y]=0;
if(flag)
{
cout<<x<<" "<<y<<endl;
}
}
int main()
{
freopen("/Users/apple/desktop/input.txt","r",stdin);
cin>>n>>m;
cout<<n<<" "<<m<<endl;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%1d",&field[i][j]);
}
memset(vis,0,sizeof(vis));
res=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(field[i][j]==3)
{
res++;
dfs(i,j);
}
}
cout<<"共有房区数:"<<res<<endl;
int k;
cin>>k;
while(k--)
{
ans=INF;
memset(vis2,0,sizeof(vis2));
cin>>sx>>sy>>b;
dfs2(sx,sy,0);
if(ans!=INF){
cout<<"最小花费:"<<ans<<endl;
cout<<"最短路径:"<<endl;
flag=0;
memset(vis2,0,sizeof(vis2));
dfs3(sx,sy,0);
}
else{
cout<<"没救了!"<<endl;
}
}
}