Don't like ads? PRO users don't see any ads ;-)
Guest

knight try

By: a guest on May 8th, 2012  |  syntax: C++  |  size: 1.45 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <cstdio>
  2. #include <queue>
  3. #include <vector>
  4. #define MAX_NODES
  5. #define MAX_EDGES
  6.  
  7. using namespace std;
  8.  
  9. int p1 [2] = {-1, 1};
  10. int p2 [2] = {-2, 2};
  11.  
  12. struct Horse {
  13.         int x;
  14.         int y;
  15.         int count;
  16.  
  17.         Horse(int x = 0, int y = 0) : x(x), y(y) {};
  18.  
  19. };
  20.  
  21.  
  22. bool visited [1000][1000];
  23.  
  24. int converter (char ch) {
  25.         return ch - 97;
  26. }
  27.  
  28. queue <Horse> fila;
  29.  
  30. int main () {
  31.         int resposta;
  32.         char pos1 [3] ;
  33.         char pos2 [3] ;
  34.         scanf("%s %s", &pos1, &pos2);
  35.         int xi = converter(pos1[0]);
  36.         int yi = pos1[1];
  37.         int xf = converter (pos2[0]);
  38.         int yf = pos2[1];
  39.         fila.push(Horse (xi, yi));
  40.         while (!fila.empty()) {
  41.                 Horse aux = fila.front();
  42.                 printf("%d %d \n", aux.x, aux.y);
  43.                 fila.pop();
  44.                 if (visited[aux.x][aux.y]) continue;
  45.                 if (aux.x == xf && aux.y == yf) {
  46.                         resposta = aux.count;
  47.                         break;
  48.                 }
  49.                 printf("teste2\n");
  50.                 visited[aux.x][aux.y] = true;
  51.                 for (int i = 0; i < 2; i++) {
  52.                         for (int j = 0; j < 2; j++) {
  53.                                 printf("teste1\n");
  54.                                 int xNovo = aux.x+p1[i];
  55.                                 int yNovo = aux.y+p2[j];
  56.                                 if (xNovo >= 0 && yNovo >= 0 && !visited[xNovo][yNovo]) {
  57.                                         Horse horse1 = Horse(xNovo, yNovo);
  58.                                         horse1.count += aux.count;
  59.                                         fila.push(horse1);
  60.                                 }
  61.                                 xNovo = aux.x+p1[i];
  62.                                 yNovo = aux.y+p2[j];
  63.                                 if (xNovo >= 0 && yNovo >= 0 && !visited[xNovo][yNovo]) {
  64.                                         Horse horse1 = Horse(xNovo, yNovo);
  65.                                         horse1.count += aux.count;
  66.                                         fila.push(horse1);
  67.                                 }
  68.                         }
  69.                 }
  70.         }
  71.         printf("%d", resposta);
  72.         return 0;
  73. }