Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class pac{
  4.     int x, y;
  5.     char dir;
  6.     pac pai;
  7.    
  8.     pac(int x, int y, char dir){
  9.         this.x = x;
  10.         this.y = y;
  11.         this.dir = dir;
  12.     }
  13.     pac(int x, int y){
  14.         this.x = x;
  15.         this.y = y;
  16.         dir = 'N';
  17.     }
  18. }
  19.  
  20.  
  21. class marcos{
  22.    
  23.     public static void mover(char a){
  24.         System.out.println(a);
  25.     }
  26.    
  27.    
  28.     public static LinkedList<pac> tem_moeda(LinkedList<pac> fila, pac pacman, char[][] mapa){
  29.         if(mapa[pacman.x-1][pacman.y] == 'o' || mapa[pacman.x-1][pacman.y] == 'O'){
  30.             pacman.dir = 'L';
  31.             fila.addLast(pacman);
  32.         }
  33.         if(mapa[pacman.x+1][pacman.y] == 'o' || mapa[pacman.x+1][pacman.y] == 'O'){
  34.             pacman.dir = 'R';
  35.             fila.addLast(pacman);
  36.         }
  37.            
  38.         if(mapa[pacman.x][pacman.y+1] == 'o' || mapa[pacman.x][pacman.y+1] == 'O'){
  39.             pacman.dir = 'D';
  40.             fila.addLast(pacman);
  41.         }
  42.            
  43.         if(mapa[pacman.x][pacman.y-1] == 'o' || mapa[pacman.x][pacman.y-1] == 'O'){
  44.             pacman.dir = 'U';
  45.             fila.addLast(pacman);
  46.         }
  47.         return fila;
  48.     }
  49.    
  50.     public static void main(String args[]){
  51.        
  52.         //incializacao de variaveis
  53.         int x,y;
  54.         String linha;
  55.         LinkedList<pac> fila = new LinkedList<pac>();
  56.         LinkedList<pac> backtrack = new LinkedList<pac>();     
  57.         Random randomGenerator = new Random();
  58.         Scanner in = new Scanner(System.in);
  59.         //leitura do tamanho do mapa
  60.         y = in.nextInt();
  61.         x = in.nextInt();
  62.         //inicializacao do mapa
  63.         char[][] mapa = new char[x][y];
  64.         //saltar linha para ler o mapa
  65.         in.nextLine();
  66.         //leitura do mapa
  67.         for(int i = 0; i<y; i++){
  68.             linha = in.nextLine();
  69.             for(int c = 0; c<x; c++){
  70.                 mapa[c][i] = linha.charAt(c);
  71.             }
  72.         }
  73.  
  74.         y = in.nextInt();
  75.         x = in.nextInt();
  76.  
  77.         in.nextLine();
  78.         in.nextLine();
  79.         in.nextLine();
  80.         in.nextLine();
  81.         in.nextLine();
  82.        
  83.         linha = in.nextLine();
  84.         char direc = linha.charAt(0);
  85.         char direc2;
  86.  
  87.         pac pacman = new pac(x, y);
  88.  
  89.         boolean verdadeiro = true;
  90.         boolean marcador = false;
  91.         int profundidade;
  92.        
  93.  
  94.         while(verdadeiro){
  95.            
  96.             fila = tem_moeda(fila, pacman, mapa);
  97.             while(!fila.isEmpty()){
  98.                 pacman = fila.removeFirst();
  99.                 mover(pacman.dir);
  100.             }
  101.                 if(direc != 'N'){
  102.                     if(mapa[pacman.x--][pacman.y] == '.' && direc == 'L'){
  103.                         mover(direc);
  104.                     }
  105.                     if(mapa[pacman.x++][pacman.y] == '.' && direc == 'R'){
  106.                         mover(direc);
  107.                     }
  108.                     if(mapa[pacman.x][pacman.y--] == '.' && direc == 'U'){
  109.                         mover(direc);
  110.                     }
  111.                     if(mapa[pacman.x][pacman.y++] == '.' && direc == 'D'){
  112.                         mover(direc);
  113.                     }
  114.                    
  115.                 }
  116.                 int randomInt = randomGenerator.nextInt(3);
  117.                 if (randomInt == 0){
  118.                     direc2 = 'D';
  119.                     if(mapa[pacman.x][pacman.y++] == '.'){
  120.                         mover(direc2);
  121.                     }  
  122.                 }
  123.                 if (randomInt == 1){
  124.                     direc2 = 'U';
  125.                     if(mapa[pacman.x][pacman.y--] == '.'){
  126.                         mover(direc2);
  127.                     }  
  128.                 }
  129.                 if (randomInt == 2) {
  130.                     direc2 = 'R';
  131.                     if(mapa[pacman.x++][pacman.y] == '.'){
  132.                         mover(direc2);
  133.                     }  
  134.                 }
  135.                 if (randomInt == 3) {
  136.                     direc2 = 'L';  
  137.                     if(mapa[pacman.x--][pacman.y] == '.'){
  138.                         mover(direc2);
  139.                     }  
  140.                 }
  141.                        
  142.         }
  143.        
  144.     }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement