A Za, A Za, Fighting...

坚信:勤能补拙

PKU 2312 Battle City

问题:
http://poj.org/problem?id=2312

思路:
这题纠结了...一上午都没写出来,原以为很简单的(其实,很多人都说是简单题,艾)
首先想到的是广搜,因为是求从源点到目的地的最小步数嘛,典型的BFS,结果却始终不知道如何表示每个状态以及状态间的判重
既然广搜不会,那就深搜吧,恩,很快就写出代码,结果TLE...
无奈,只好在网上找解法,发现一种相当不错的BFS代码:

队列的状态只需要保存当前位置即可,另外用数组best[][]表示目前从源点到达每个点的最小步数
在从当前点扩展的时候,只将有最小步数更新的点加入队列
这样当BFS搜索结束时,每个best[i][j]都保存了从源点到点[i][j]的最短步数
原来还有这样写BFS的方法,又学习了(*^__^*) 嘻嘻……

上述算法有点类似于求最短路径呵呵
而,事实上,这题是可以很容易转化成图论求单源最短路径的

代码:
 1 /* 32MS */
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #define MAX_LEN 301
 6 #define QUEUE_SIZE 90001
 7 #define INF 0x7FFFFFFF
 8 #define is_valid(x, y) ((x)>=0 && (x)<M && (y)>=0 && (y)<N)
 9 char map[MAX_LEN][MAX_LEN];
10 const int dx[] = {00-11};
11 const int dy[] = {-1100};
12 int M, N;
13 int you_x, you_y, target_x, target_y;
14 int best[MAX_LEN][MAX_LEN]; /* important, best[i][j] stores the current 'best solution' at map[i][j] */
15 struct Node {
16     int x, y;
17 } queue[QUEUE_SIZE];
18 
19 int
20 bfs()
21 {
22     int i, next_x, next_y, cur, head, tail;
23     struct Node node;
24     head = -1;
25     tail = 0;
26     queue[tail].x = you_x;
27     queue[tail].y = you_y;
28     best[you_x][you_y] = 0;
29     while(head < tail) {
30         ++head;
31         cur = best[queue[head].x][queue[head].y];
32         for(i=0; i<4; i++) {
33             node.x = queue[head].x + dx[i];
34             node.y = queue[head].y + dy[i];
35             if(is_valid(node.x, node.y)) {
36                 /* excellent, only push node which can be updated in 'best' into the queue */
37                 if(map[node.x][node.y] == 'E' || map[node.x][node.y] == 'T') {
38                     if(best[node.x][node.y] > cur+1) {
39                         best[node.x][node.y] = cur+1;
40                         ++tail;
41                         queue[tail] = node;
42                     }
43                 } else if(map[node.x][node.y] == 'B') {
44                     if(best[node.x][node.y] > cur+2) {
45                         best[node.x][node.y] = cur+2;
46                         ++tail;
47                         queue[tail] = node;
48                     }
49                 }
50             }
51         }
52     }
53 }
54 
55 int
56 main(int argc, char **argv)
57 {
58     int i, j;
59     while(scanf("%d %d"&M, &N)!=EOF && M && N) {
60         for(i=0; i<M; i++) {
61             scanf("%s", map[i]);
62             for(j=0; j<N; j++) {
63                 if(map[i][j] == 'Y') {
64                     you_x = i;
65                     you_y = j;
66                 } else if(map[i][j] == 'T') {
67                     target_x = i; 
68                     target_y = j;
69                 }
70                 best[i][j] = INF;
71             }
72         }
73         bfs();
74         printf("%d\n", best[target_x][target_y]==INF ? -1 : best[target_x][target_y]);
75     }
76 }


posted on 2010-10-23 13:33 simplyzhao 阅读(204) 评论(0)  编辑 收藏 引用 所属分类: B_搜索


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年10月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜