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());
}
}
zoj3890搜索
最新推荐文章于 2019-04-15 08:49:57 发布