-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJP and Plan.cpp
99 lines (70 loc) · 1.38 KB
/
JP and Plan.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
99
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int x[]={-1,0,1,0 } ;
int y[]={ 0,1,0,-1 } ;
int **visited;
int n,m;
ll **mat ;
vector<pair<int,int>>ans;
int isValid(int dx,int dy)
{
if(dx>=0&&dx<n&&dy>=0&&dy<m&&visited[dx][dy]==0)
{return 1 ;
}
return 0 ;
}
int dfs(int dx,int dy,int j)
{
visited[dx][dy]=1 ;
ans.push_back(make_pair(dx,dy)) ;
if(dx==0||dy==0||dx==n-1||dy==m-1)
return 1 ;
for(int i=0;i<4;i++)
{
if(isValid(dx+x[i],dy+y[i]))
{
if( ( mat[dx][dy] - mat[dx+x[i]][dy+y[i]] >=0 ) && ( mat[dx][dy] - mat[dx+x[i]][dy+y[i]] <=j ) &&dfs(dx+x[i],dy+y[i],j) )
{
return 1 ;
}
}
}
ans.pop_back() ;
visited[dx][dy]=0 ;
return 0 ;
}
int main()
{
cin>>n>>m;
mat=new ll*[n] ;
visited=new int*[n] ;
for(int j=0;j<n;j++)
visited[j]=new int[m]() ;
for(int i=0;i<n;i++)
mat[i]=new ll[m] ;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>mat[i][j];
}
}
int dx,dy,j ;
cin>>dx>>dy>>j ;
dx-- ;
dy-- ;
if(dfs(dx,dy,j))
{
cout<<"YES"<<endl;
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
{
cout<<ans[i].first+1<<" "<<ans[i].second+1<<endl;
}
}
else{
cout<<"NO"<<endl;
}
return 0;
}