Exemplo n.º 1
0
vector<int> searchRange(vector<int>& nums, int target) {
	vector<int> result;
	int begin = 0, end = nums.size() - 1;
	int first = searchNumber(nums, target, begin, end);

	int temp = searchNumber(nums, target, begin, first - 1);
	int left = temp;
	while (temp != -1) {
		left = temp;
		temp = searchNumber(nums, target, begin, left - 1);
	}

	if (left != -1) {
		result.push_back(left);
	}
	else {
		result.push_back(first);
	}

	temp = searchNumber(nums, target, first + 1, end);
	int right = temp;
	while (temp != -1) {
		right = temp;
		temp = searchNumber(nums, target, right + 1, end);
	}
	if (right != -1) {
		result.push_back(right);
	}
	else {
		result.push_back(first);
	}

	return result;
}
Exemplo n.º 2
0
Arquivo: D.c Projeto: jpbat/aed2011
void worker(char* word)
{
	nodePtr aux;
	int i, size;

	size = strlen(word);

	for (i=0 ; i<size ; i++)
		word[i]=tolower(word[i]);
	
	if ( searchNumber(word) == 0 )
	{
		aux = createNode(word, randX());
		addNode(list, aux, n_lvl);
	}
}
Exemplo n.º 3
0
int main(int argc, char **argv){
  int amountForFirst;
  int amountFound;
  unsigned amountNumbers;
  int amountPcs;
  int amountPerPC;
  int counter;
  int rank;
  int target;
  int totalFound;

  char *content;
  int *founds;
  int **numbers;
  int *slaveNumbers;
  MPI_Status status;

  if(argc != 3) goto ERROR;

  MPI_Init(&argc, &argv);
    amountForFirst = 0;
    amountFound = 0;
    amountNumbers = 0;
    amountPcs = 0;
    amountPerPC = 0;
    counter = 0;
    rank = 0;
    target = 0;
    totalFound = 0;

    content = NULL;
    founds = NULL;
    numbers = NULL;
    slaveNumbers = NULL;
    memset(&status, 0, sizeof(MPI_Status));


    MPI_Comm_size(MPI_COMM_WORLD, &amountPcs);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    target = atoi(argv[2]);

    if(rank == 0){
      if(getContent(argv[1], &content)){
        amountNumbers = splitNumbers(content, amountPcs, &amountForFirst, &amountPerPC, &numbers);
        if(amountNumbers){
          for(counter = 1; counter < amountPcs; counter++){
            MPI_Send(&amountPerPC, 1, MPI_INT, counter, 0, MPI_COMM_WORLD);
            MPI_Send(numbers[counter], amountPerPC, MPI_INT, counter, 0, MPI_COMM_WORLD);
          }

          /* Search and save in node 0 */
          if(searchNumber(numbers[0], amountForFirst, target, &founds, &amountFound)){
            if(!savePositions(founds, amountFound, 0)) goto ERROR;
            printf("Rank %d found %d\n", rank, amountFound);
          }else{
            goto ERROR;
          }
          totalFound += amountFound;

          /* Receive founds in other nodes and save. */
          for(counter = 1; counter < amountPcs; counter++){
            amountFound = 0;
            free(founds);
            MPI_Recv(&amountFound, 1, MPI_INT, counter, 0, MPI_COMM_WORLD, &status);
            totalFound += amountFound;
            founds = (int*) calloc(amountFound, sizeof(int));
            if(!founds) goto ERROR;
            MPI_Recv(founds, amountFound, MPI_INT, counter, 0, MPI_COMM_WORLD, &status);
            if(!savePositions(founds, amountFound, amountForFirst + (counter * amountPerPC))) goto ERROR;
            printf("Rank %d found %d\n", status.MPI_SOURCE, amountFound);
          }
          printf("Total found: %d\n", totalFound);
        }else{
          goto ERROR;
        }
      }else{
        goto ERROR;
      }
    }else{
      MPI_Recv(&amountPerPC, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
      slaveNumbers = (int*) calloc(amountPerPC, sizeof(int));
      if(!slaveNumbers) goto ERROR;
      MPI_Recv(slaveNumbers, amountPerPC, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
      if(searchNumber(slaveNumbers, amountPerPC, target, &founds, &amountFound)){
        MPI_Send(&amountFound, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
        MPI_Send(slaveNumbers, amountFound, MPI_INT, 0, 0, MPI_COMM_WORLD);
      }else{
        goto ERROR;
      }
    }


    goto END;

    ERROR:
      printf("%d - Deu ruim!!!\n", rank);

    END:
      if(content) free(content);
      if(founds) free(founds);
      if(slaveNumbers) free(slaveNumbers);
      memset(&status, 0, sizeof(MPI_Status));
      for(counter = 0; counter < amountPcs; counter++){
        if(numbers){
          if(numbers[counter]) free(numbers[counter]);
        }
      }
      if(numbers) free(numbers);
  MPI_Finalize();

  return 0;
}
Exemplo n.º 4
0
void moveLeft2Numbers(Puzzle *puzzle, Table fixes, const int x, const int y, int n) {
	int m = n;
	Position pos1 = makePosition(x, y);
	Position pos2 = makePosition(x, y + 1);
	Position pos3 = makePosition(x + 1, y);
	Position pos4 = makePosition(x + 1, y + 1);
	n += X_SIZE;
	dcode(printf("moveLeft2Numbers x=%d y=%d n=%d\n", x, y, m));
	for (;;) {
		Table nextmap;
		int p1 = puzzle->table[y][x];
		int p2 = puzzle->table[y + 1][x];
		int p3 = puzzle->table[y][x + 1];
		int p4 = puzzle->table[y + 1][x + 1];
		dputs("loop head");
		if (p1 == m && p2 == n) {
			dputs("OK");
			break;
		} else
		if (p1 != m && p1 != n && p2 != m && p2 != n) {
			Table tempM, tempN;
			Position posM = searchNumber(puzzle, m);
			Position posN = searchNumber(puzzle, n);
			int rM = searchRoot(fixes, tempM, posM, pos2);
			int rN = searchRoot(fixes, tempN, posN, pos1);
			dputs("not found case");
			if (rM >= 0 && rM < rN) {
				if (moveNumber(puzzle, fixes, m, pos2) < 0) {
					printPuzzle(puzzle);
					puts("error");
					return;
				}
			} else if (rN >= 0) {
				if (moveNumber(puzzle, fixes, n, pos1) < 0) {
					printPuzzle(puzzle);
					puts("error2");
					return;
				}
			} else {
				printPuzzle(puzzle);
				puts("error3");
				return;
			}
			continue;
		} else
		if ((p1 == m && p2 == 0 && p4 == n) || (p1 == 0 && p2 == n && p3 == m)) {
			dputs("rare case");
			moveRightSpace(puzzle);
			dputs("OK2");
			break;
		} else
		if (p1 == n && p3 == m) {
			int f3 = fixes[pos3.y][pos3.x];
			dputs("p1==n&&p3==m");
			fixes[pos1.y][pos1.x] = FIXED;
			fixes[pos3.y][pos3.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos2) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos3.y][pos3.x] = f3;
			moveSpace(puzzle, nextmap, pos2);
			moveUpSpace(puzzle);
			moveRightSpace(puzzle);
			dputs("OK3");
			break;
		} else
		if (p2 == m && p4 == n) {
			int f4 = fixes[pos4.y][pos4.x];
			dputs("p2==m&&p4==n");
			fixes[pos2.y][pos2.x] = FIXED;
			fixes[pos4.y][pos4.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos1) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos4.y][pos4.x] = f4;
			moveSpace(puzzle, nextmap, pos1);
			moveDownSpace(puzzle);
			moveRightSpace(puzzle);
			dputs("OK4");
			break;
		} else
		if (p1 == 0 && p2 == m && p3 == n) {
			moveDownSpace(puzzle);
			continue;
		} else
		if (p1 == n && p2 == 0 && p4 == m) {
			moveRightSpace(puzzle);
			moveUpSpace(puzzle);
			moveLeftSpace(puzzle);
			moveDownSpace(puzzle);
			continue;
		} else
		if (p1 == n && p2 == m) {
			searchSpaceRoot(puzzle, fixes, nextmap, pos3);
			moveSpace(puzzle, nextmap, pos3);
			moveLeftSpace(puzzle);
			moveDownSpace(puzzle);
			continue;
		} else
		if (p2 == n && p4 == m) {
			if (p1 == 0) {
				moveDownSpace(puzzle);
				moveRightSpace(puzzle);
				continue;
			} else
			if (p3 == 0) {
				moveLeftSpace(puzzle);
				moveDownSpace(puzzle);
				moveRightSpace(puzzle);
				continue;
			} else {
				Position pos = makePosition(x + 2, y);
				searchSpaceRoot(puzzle, fixes, nextmap, pos);
				moveSpace(puzzle, nextmap, pos);
				moveLeftSpace(puzzle);
				moveLeftSpace(puzzle);
				moveDownSpace(puzzle);
				moveRightSpace(puzzle);
				continue;
			}
		} else
		if (p1 == m && p3 == n) {
			Position pos = makePosition(x + 2, y + 1);
			int f = fixes[pos.y][pos.x];
			dputs("wrong case (move n)");
			dcode(printTable("fixes", fixes));
			if (moveNumber(puzzle, fixes, n, pos) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): n pos %d\n", m);
				return;
			}
			dputs("wrong case (move m)");
			dcode(printTable("fixes", fixes));
			fixes[pos.y][pos.x] = FIXED;
			if (moveNumber(puzzle, fixes, m, pos2) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): m pos2 %d\n", m);
				return;
			}
			fixes[pos.y][pos.x] = f;
			continue;
		} else
		if (p1 == m) {
			int f1 = fixes[pos1.y][pos1.x];
			dputs("p1==m");
			fixes[pos1.y][pos1.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos2) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos1.y][pos1.x] = f1;
			moveSpace(puzzle, nextmap, pos2);
			moveUpSpace(puzzle);
			continue;
		} else
		if (p2 == n) {
			int f2 = fixes[pos2.y][pos2.x];
			dputs("p2==n");
			fixes[pos2.y][pos2.x] = FIXED;
			if (searchSpaceRoot(puzzle, fixes, nextmap, pos1) == INT_MAX) {
				printPuzzle(puzzle);
				puts("error");
				return;
			}
			fixes[pos2.y][pos2.x] = f2;
			moveSpace(puzzle, nextmap, pos1);
			moveDownSpace(puzzle);
			continue;
		} else
		if (p1 == n) {
			int f1 = fixes[pos1.y][pos1.x];
			dputs("p1==n");
			fixes[pos1.y][pos1.x] = FIXED;
			if (moveNumber(puzzle, fixes, m, pos3) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): p1==n %d\n", m);
				return;
			}
			fixes[pos1.y][pos1.x] = f1;
			continue;
		} else
		if (p2 == m) {
			int f2 = fixes[pos2.y][pos2.x];
			dputs("p2==m");
			fixes[pos2.y][pos2.x] = FIXED;
			if (moveNumber(puzzle, fixes, n, pos4) < 0) {
				printPuzzle(puzzle);
				printf("error in moveLeft2Numbers(): p2==m %d\n", m);
				return;
			}
			fixes[pos2.y][pos2.x] = f2;
			continue;
		} else {
			printPuzzle(puzzle);
			puts("error dayo");
			return;
		}
	}
	fixes[pos1.y][pos1.x] = FIXED;
	fixes[pos2.y][pos2.x] = FIXED;
}
Exemplo n.º 5
0
int moveNumber(Puzzle *puzzle, Table fixes, const int number, const Position pos1) {
	if (puzzle->table[pos1.y][pos1.x] == number) {
		return 0;
	}
	dputs("moveNumber");
	{
		Position pos2 = searchNumber(puzzle, number), pos3;
		int x = pos2.x;
		int y = pos2.y;
		Table rootmap, temp1, temp2, temp3, temp4;
		int r = searchRoot(fixes, rootmap, pos2, pos1) - 1;
		int z, mv, zz, f;
		if (r < 0) {
			return -1;
		}
		while (x != pos1.x || y != pos1.y) {
			dputs("loop head");
			zz = INT_MAX;
			mv = 0;
			f = fixes[y][x];
			fixes[y][x] = FIXED;
			if (rootmap[y][x - 1] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp2, pos2 = makePosition(x - 1, y));
				if (z < INT_MAX) {
					mv = MOVE_RIGHT;
					zz = z;
					pos3 = pos2;
				}
			}
			if (rootmap[y][x + 1] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp1, pos2 = makePosition(x + 1, y));
				if (z < INT_MAX && (z < zz)) {
					mv = MOVE_LEFT;
					zz = z;
					pos3 = pos2;
				}
			}
			if (rootmap[y - 1][x] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp4, pos2 = makePosition(x, y - 1));
				if (z < INT_MAX && (z < zz)) {
					mv = MOVE_DOWN;
					zz = z;
					pos3 = pos2;
				}
			}
			if (rootmap[y + 1][x] == r) {
				z = searchSpaceRoot(puzzle, fixes, temp3, pos2 = makePosition(x, y + 1));
				if (z < INT_MAX && (z < zz)) {
					mv = MOVE_UP;
					pos3 = pos2;
				}
			}
			fixes[y][x] = f;
			dcode(printTable("rootmap", rootmap));
			dcode(printf("number=%d x=%d y=%d x3=%d y3=%d mv=%d r=%d\n", number, x, y, pos3.x, pos3.y, mv, r));
			switch (mv) {
			case MOVE_LEFT:
				moveSpace(puzzle, temp1, pos3);
				moveLeftSpace(puzzle);
				break;
			case MOVE_RIGHT:
				moveSpace(puzzle, temp2, pos3);
				moveRightSpace(puzzle);
				break;
			case MOVE_UP:
				moveSpace(puzzle, temp3, pos3);
				moveUpSpace(puzzle);
				break;
			case MOVE_DOWN:
				moveSpace(puzzle, temp4, pos3);
				moveDownSpace(puzzle);
				break;
			default:
				/* error */
				return -3;
			}
			x = pos3.x;
			y = pos3.y;
			r--;
		}
	}
	return 1;
}