Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int abs(int x){
- if (x>0) return x;
- else return -x;
- }
- int cauta(int a[4][5], int n, int m, int x, int y){
- x--;
- y--;
- int px=x, py=y;
- while(1)
- {
- int nx,ny,ndelta=2000000000;
- if (x>0 && abs(a[x-1][y]-a[x][y])<ndelta)
- {
- nx = x-1;
- ny = y;
- ndelta = abs(a[x-1][y]-a[x][y]);
- }
- if (y>0 && (abs(a[x][y-1]-a[x][y])<ndelta || abs(a[x][y-1]-a[x][y])==ndelta && a[x][y-1]<a[nx][ny]))
- {
- nx = x;
- ny = y-1;
- ndelta = abs(a[x][y-1]-a[x][y]);
- }
- if (x<m-1 && (abs(a[x+1][y]-a[x][y])<ndelta || abs(a[x+1][y]-a[x][y])==ndelta && a[x+1][y]<a[nx][ny]))
- {
- nx = x+1;
- ny = y;
- ndelta = abs(a[x+1][y]-a[x][y]);
- }
- if (y<n-1 && (abs(a[x][y+1]-a[x][y])<ndelta || abs(a[x][y+1]-a[x][y])==ndelta && a[x][y+1]<a[nx][ny]))
- {
- nx = x;
- ny = y+1;
- ndelta = abs(a[x][y+1]-a[x][y]);
- }
- if (nx==px && ny==py) {
- if (a[x][y]<a[nx][ny]) return a[x][y];
- else return a[nx][ny];
- }
- px=x;
- py=y;
- x=nx;
- y=ny;
- }
- }
- int main()
- {
- int m[4][5] = {{57, 51, 70, 72, 75},
- {56, 58, 60, 66, 77},
- {59, 54, 93, 90, 76},
- {88, 52, 61, 79, 68}};
- cout << cauta(m,4,5,2,4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement