2014校招 Google在线测试第二场题解 D

D.  Dragon Maze

题意简要是:在一个矩阵中,每个格子有一个整数,非负数表示能量值,-1表示不能去的格子。求起点到终点,最短路径中收集能量最多是多少?无法到达终点则输出"Mission Impossible."

最短路径,边长都是1的图中,可以用bfs求解。并保存到达该节点的最短路径值和能收集到的能力最大值。代码如下:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class HelloWorld {
    
    static int N, M;
    static int[][] table;
    static int sx,sy,ex,ey;
    static int[][][] dp;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, 1, -1};
    
    public static void main(String[] args) {
        try {
//            BufferedReader br = new BufferedReader(new FileReader("D:/codejam/a.txt"));
//            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
            
//            BufferedReader br = new BufferedReader(new FileReader("D:/codejam/D-small-attempt0.in"));
//            BufferedWriter bw = new BufferedWriter(new FileWriter("D:/codejam/D-small-result.txt"));
//          BufferedReader br = new BufferedReader(new FileReader("D:/codejam/A-small-attempt1.in"));
            
            BufferedReader br = new BufferedReader(new FileReader("D:/codejam/D-large-practice.in"));
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:/codejam/D-large-result.txt"));

            String line;
            
            line = br.readLine();
            int T = Integer.parseInt(line);
            
            for (int cas = 1; cas <= T; cas++) {
                line = br.readLine();
                String[] tmp = line.split(" ");
                N = Integer.parseInt(tmp[0]);
                M = Integer.parseInt(tmp[1]);
                table = new int[N][M];
                dp = new int[N][M][2];
                for (int i = 0; i < N; i++)
                    for (int j = 0; j < M;j++) {
                        dp[i][j][0] = dp[i][j][1] = -1;
                    }
                line = br.readLine();
                tmp = line.split(" ");
                
                sx = Integer.parseInt(tmp[0]);
                sy = Integer.parseInt(tmp[1]);
                ex = Integer.parseInt(tmp[2]);
                ey = Integer.parseInt(tmp[3]);
                for (int i = 0; i < N; i++) {
                    line = br.readLine();
                    tmp = line.split(" ");
                    for (int j = 0; j < M; j++) {
                        table[i][j] = Integer.parseInt(tmp[j]);
                    }
                }
                
                int[] queue = new int[N * M * 4];
                
                dp[sx][sy][0] = 0;
                dp[sx][sy][1] = table[sx][sy];
                int size = 0;
                queue[size++] = sx;
                queue[size++] = sy;
                for (int front = 0; front < size; front += 2) {
                    int x = queue[front];
                    int y = queue[front+1];
                    
                    for (int i = 0; i < 4; i++) {
                        int nx = x + dx[i];
                        int ny = y + dy[i];
                        if (!isOk(nx, ny)) continue;
                        
                        if (dp[nx][ny][0] == -1 ||
                                (dp[nx][ny][0] == (dp[x][y][0] + 1) && dp[nx][ny][1] < dp[x][y][1] + table[nx][ny]) ) {
                            if (dp[nx][ny][0] == -1) {
                                queue[size++] = nx;
                                queue[size++] = ny;
                            }
                            dp[nx][ny][0] = dp[x][y][0] + 1;
                            dp[nx][ny][1] = dp[x][y][1] + table[nx][ny];
                        }
                    }
                }
                    
                String ans = "Mission Impossible.";
                if (dp[ex][ey][1] != -1) {
                    ans = Integer.toString(dp[ex][ey][1]);
                }
                bw.write("Case #" + cas +": " + ans);
                bw.newLine();
                bw.flush();
            }
            
            br.close();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private static boolean isOk(int nx, int ny) {
        if (nx < 0 || nx >= N || ny < 0 || ny >= M || table[nx][ny] == -1) return false;
        return true;
    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值