zoj3890搜索

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

import com.sun.corba.se.pept.transport.ContactInfo;
import com.sun.org.apache.regexp.internal.recompile;


public class Main {

        public static void main(String[] args) throws IOException{
                StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); 
                InputReader in = new InputReader(System.in)  ;
                PrintWriter out = new PrintWriter(System.out) ;


                int t = in.nextInt() ;
                while(t-- > 0){
                         new Task().solve(in, out)  ;   //out.flush() ;
                }

               out.flush() ;

        }

}

class Task{

          static class E implements Comparable<E>{
                     int x ;
                     int y ;
                     int state ;
                     int money ;
                     public E(int x , int y , int state , int money){
                                 this.x  = x ;
                                 this.y = y ;
                                 this.state = state ;
                                 this.money = money ;
                     }
                    @Override
                    public int compareTo(E o) {
                        // TODO Auto-generated method stub
                            return o.money - money ;
                    }
          } 

          static  int[][] dir= new int[][]{{1,0},{0,1},{-1,0},{0,-1}} ;

          static  boolean[][] wall = new boolean[20][20] ;
          int n ;
          int gx , gy ;

          boolean can(int x , int y){
                          return 0 <= x && x < n && 0 <= y && y < n ;
          }

          static boolean  vis[][][] = new boolean[20][20][8] ;

          int    guai(int x , int y){
                   int s = Math.abs(x - y) ;

                   return Math.min(s , 4 - s ) ;
          }

          int     spfa(){
                       for(int x = 0 ; x < n ; x++){
                             for(int y = 0 ; y < n ; y++){
                                   for(int k = 0 ; k < 8 ; k++) vis[x][y][k] = false  ;
                             }
                       }

                       PriorityQueue<E> q = new PriorityQueue<E>() ;
                       q.offer(new E(0 , 0 ,0 , 0 )) ;
                       vis[0][0][0] = true ;
                       while(! q.isEmpty()){
                              E e = q.poll() ;    
                             // if(++kk >= 100) return 0  ;
                              //System.out.println(e.x + "  " + e.y +"  " +e.state +"  " +e.money) ;
                          //   System.out.println(e.x + "  " + e.y + "  "  + e.money + " :" + ( e.state &1) );

                              int d = e.state >> 1 ;
                              if(e.x == 0 && e.y == 0 && ((e.state & 1) > 0)){
                                         return e.money - 10 ;
                              }

                              for(int i = 0 ; i < 4 ; i++){
                                       int x = e.x + dir[i][0] ;
                                       int y = e.y + dir[i][1] ;
                                       if(! can(x , y)) continue ;
                                       if(! wall[x][y]) continue ;
                                       int nd = i ;
                                       int money = e.money - 10  ;
                                       money -= guai(d , i) * 10 ;
                                       int nstate = (i << 1) ;
                                       nstate |= (e.state & 1) ;
                                       if(x == gx && y == gy){
                                                nstate |= 1 ;
                                                money -= 10 ;
                                                money += 1000 ;
                                       }
                                       if(! vis[x][y][nstate]){
                                                vis[x][y][nstate] = true ;  
                                                q.offer(new E(x , y , nstate , money)) ;
                                       }
                              }
                       }


                       return - 1 ;
          }





          public void solve(InputReader  in , PrintWriter out) throws IOException{
                       n = in.nextInt() ;
                       for(int i = 0 ; i < n ; i++) Arrays.fill(wall[i], true) ;

                       int k , x , y ;
                       for(;;){
                               k = in.nextInt() ;
                               x = in.nextInt() ;
                               y = in.nextInt() ;
                               if(k == -1 && x == -1 && y == -1) break ;
                               if(k == 3){
                                        gx = x ;
                                        gy = y ;
                               }
                               else wall[x][y] = false ;
                       }

                       if(! wall[0][0]){
                               out.println(-1) ;
                               return ;
                       }

                       out.println(spfa()) ;

          }

}


class InputReader{
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream){
           reader = new BufferedReader(new InputStreamReader(stream), 32768);
           tokenizer = null;
    }

    public String next(){
        while(tokenizer == null || !tokenizer.hasMoreTokens()){
            try{
                tokenizer = new StringTokenizer(reader.readLine());
            }catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值