Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <cstdlib>
- using namespace std;
- typedef struct Punct {
- int l;
- int c;
- } Punct;
- int n, **a, x, altcr;
- Punct *coada, pct;
- int dl[] = {-1, 0, 1 , 0},
- dc[] = {0, 1, 0 , -1};
- void bordare (int **a) {
- for (int i = 0; i < n+2; i++) {
- a[i][0] = 0;
- a[i][n+1] = 0;
- a[0][i] = 0;
- a[n+1][i] = 0;
- }
- }
- void afisare (int **a) {
- for (int i = 0; i <= n+1; i++) {
- for (int j = 0; j <= n+1; j++) {
- printf("%d ", a[i][j]);
- }
- printf ("\n");
- }
- printf ("\n");
- }
- void lee () {
- int p = 0, u = 0;
- bool exit = false;
- coada[p].l = 1;
- coada[p].c = 1;
- while (p <= u && !exit) {
- if (a[coada[p].l][coada[p].c] == x) {
- pct.l = coada[p].l;
- pct.c = coada[p].c;
- exit = true;
- break;
- }
- for (int i = 0; i < 4; i++) {
- if (a[coada[p].l + dl[i]][coada[p].c + dc[i]] > a[coada[p].l][coada[p].c] && a[coada[p].l + dl[i]][coada[p].c + dc[i]] <= x && a[coada[p].l +dl[i]][coada[p].c + dc[i]] > altcr) {
- u++;
- if(a[coada[p].l + dl[i]][coada[p].c + dc[i]] > altcr && a[coada[p].l + dl[i]][coada[p].c + dc[i]] <= x) {
- altcr = a[coada[p].l + dl[i]][coada[p].c + dc[i]];
- }
- coada[u].l = coada[p].l + dl[i];
- coada[u].c = coada[p].c + dc[i];
- }
- }
- //afisare(a);
- p++;
- }
- }
- int main()
- {
- freopen("munte1.in", "r", stdin);
- freopen("munte1.out", "w", stdout);
- /* INPUT HERE:
- 4
- 1 3 9 10
- 2 4 11 13
- 5 6 12 14
- 7 15 16 19
- 13
- */
- scanf("%d", &n);
- //--
- a = (int **) malloc (sizeof(int *) * (n + 2));
- for (int i = 0; i < n+2; i++) {
- a[i] = (int *) malloc (sizeof(int) * (n + 2));
- }
- coada = (Punct *) malloc (sizeof(Punct) * n * n);
- bordare(a);
- //--
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= n; j++) {
- scanf("%d", &a[i][j]);
- }
- }
- scanf("%d", &x);
- //afisare(a);
- lee();
- printf("%d %d", pct.l, pct.c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment