-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuaxue.cpp
59 lines (59 loc) · 1.17 KB
/
huaxue.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
#include <iostream>
#include <cstring>
using namespace std;
int map[105][105];
bool vis[105][105];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int r,c;
int ans=0;
int findx(int n){
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(n==map[i][j]){
return i;
}
}
}
}
int findy(int n){
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(n==map[i][j]){
return j;
}
}
}
}
void dfs(int x,int y){
int min=10002;
int m=0;
int n=0;
if(x<1||y<1||x>r||y>c||vis[x][y]==true){
return;
}
vis[x][y]=true;
for(int i=0;i<4;i++){
if(map[x+dx[i]][y+dy[i]]<min&&vis[x+dx[i]][y+dy[i]]!=true&&map[x+dx[i]][y+dy[i]]>map[x][y]){
min=map[x+dx[i]][y+dy[i]];
m=x+dx[i];
n=y+dy[i];
}
}
ans++;
dfs(m,n);
}
int main(){
memset(map,10005, sizeof(map));
memset(vis,true,sizeof(vis));
cin>>r>>c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin>>map[i][j];
vis[i][j]= false;
}
}
dfs(findx(1),findy(1));
cout<<ans<<endl;
return 0;
}