Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ff first
- #define ss second
- using namespace std;
- typedef pair<int,int> pii;
- int g[102][102] = {};
- int dis[102][102] = {};
- inline int ABS(int a){return a>0?a:-a;}
- signed main(){
- ios::sync_with_stdio(false);
- cin.tie(0);
- int q,m,n;
- cin >> q;
- while(q--){
- cin >> m >> n;
- for(int i = 1;i <= m;i++)
- for(int j = 1;j <= n;j++){
- cin >> g[i][j]; dis[i][j] = 1<<30;
- g[0][j] = g[m+1][j] = dis[0][j] = dis[m+1][j] = 1<<30;
- g[i][0] = g[i][n+1] = dis[i][0] = dis[i][n+1] = 1<<30;
- }
- queue<pii> qu;
- qu.push({1,1}); dis[1][1] = 0;
- while(!qu.empty()){
- int x = qu.front().ff, y = qu.front().ss; qu.pop();
- int adj[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
- for(int i = 0;i < 4;i++){
- int nx = x+adj[i][0],ny = y+adj[i][1];
- if(ABS(g[nx][ny]-g[x][y])<=5 && dis[nx][ny] > dis[x][y]+1){
- dis[nx][ny] = dis[x][y]+1;
- qu.push({nx,ny});
- }
- }
- }
- cout << dis[m][n] << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement