赞
踩
3 4 4 2 2 100 200 **** *@A* *B<* **** 4 4 1 2 100 200 **** *@A* *B<* **** 12 5 13 2 100 200 ************ *B.........* *.********.* *@...A....<* ************
Case 1:
The best score is 200.
Case 2:
Impossible
Case 3:
The best score is 300.
BFS搜索所有情况 然后DFS
-
- import java.util.*;
-
- public class HDU1044 {
-
- /**
- * @param args
- */
- static int n, m;
- static int max = 0, sum = 0;
- static int totalTime, ex, ey;
- static int dirArr[][] = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 },
- { 0, -1 } };
- static char[][] map;
- static int[] value;
- static int[][] adj = new int[14][14];
- static boolean visit[];
- static boolean visadj[][];
- static Node[] nodes;
- static int num;
-
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int T = sc.nextInt();
- for (int i = 1; i <= T; i++) {
- max = 0;
- sum = 0;
- nodes = new Node[13];
- visit = new boolean[15];
- m = sc.nextInt();
- n = sc.nextInt();
- totalTime = sc.nextInt();
- num = sc.nextInt();
- value = new int[num];
- map = new char[n][m];
- int sx = 0, sy = 0;
- for (int k = 0; k < num; k++)
- value[k] = sc.nextInt();
- for (int k = 0; k < n; k++) {
- char[] arr = sc.next().toCharArray();
- for (int j = 0; j < m; j++) {
- char temp = arr[j];
- map[k][j] = temp;
- if (temp == '@') {
- sx = k;
- sy = j;
- } else if (temp == '<') {
- ex = k;
- ey = j;
- } else if (temp >= 'A' && temp <= 'J') {
- nodes[temp - 'A'] = new Node(k, j, 0);
- }
- }
- }
- InitArray();
- BFS(sx, sy, num);
- if (i > 1)
- System.out.println();
- System.out.println("Case " + i + ":");
- if (adj[num][num + 1] == -1 || adj[num][num + 1] > totalTime) {
- System.out.println("Impossible");
- continue;
- }
-
- for (int kk = 0; kk < num; kk++) {
- if (adj[kk][num] != -1)
- sum += value[kk];
- }
- BFS(ex, ey, num + 1);
- for (int k = 0; k < num; k++) {
- BFS(nodes[k].x, nodes[k].y, k);
- }
- visit[num] = true;
- DFS(num, 0, 0);
- System.out.println("The best score is " + max + ".");
-
- }
-
- }
-
- static void InitArray() {
- for (int i = 0; i < 14; i++)
- for (int j = 0; j < 14; j++)
- adj[i][j] = -1;
- }
-
- static void DFS(int i, int step, int val) {
- if (step > totalTime || max == sum)
- return;
- if (i == num + 1) {
- if (val > max)
- max = val;
- }
- if (i != num + 1 && step + adj[i][num + 1] > totalTime)
- return;
- for (int j = 0; j <= num + 1; j++) {
- if (adj[i][j] != -1) {
- if (visit[j])
- continue;
- visit[j] = true;
- if (j < num) {
- DFS(j, step + adj[i][j], val + value[j]);
- } else {
- DFS(j, step + adj[i][j], val);
- }
- visit[j] = false;
- }
- }
- }
-
- static void BFS(int x0, int y0, int k) {
- Queue<Node> queue = new ArrayDeque<Node>();
- queue.add(new Node(x0, y0, 0));
- visadj = new boolean[55][55];
- visadj[x0][y0] = true;
- while (!queue.isEmpty()) {
- Node pre = queue.poll();
- for (int i = 0; i < 4; i++) {
- Node curr = new Node(pre.x, pre.y, pre.step);
- curr.x += dirArr[i][0];
- curr.y += dirArr[i][1];
- int x = curr.x;
- int y = curr.y;
- curr.step++;
- if (x < 0 || x >= n || y < 0 || y >= m || map[x][y] == '*'
- || visadj[x][y])
- continue;
- visadj[x][y] = true;
- queue.add(curr);
- if (map[x][y] >= 'A' && map[x][y] <= 'J') {
- int index = map[x][y] - 'A';
- adj[index][k] = adj[k][index] = curr.step;
- } else if (map[x][y] == '@') {
- adj[k][num] = adj[num][k] = curr.step;
- } else if (map[x][y] == '<') {
- adj[k][num + 1] = adj[num + 1][k] = curr.step;
- }
-
- }
- }
- }
-
- static class Node {
- protected int x, y;
- protected int step;
- protected boolean isVisit;
-
- public Node(int x, int y, int step) {
- this.x = x;
- this.y = y;
- this.step = step;
-
- }
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。