A Za, A Za, Fighting...

坚信:勤能补拙

PKU 1154 LETTERS

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

思路:
教科书式的DFS,一次AC

代码:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_LEN 21
 5 #define is_valid(x, y) (x>=0&&x<R&&y>=0&&y<C)
 6 const int dx[] = {-1100};
 7 const int dy[] = {00-11};
 8 int R, C;
 9 int rt, hash[26]; /* A-Z */
10 char map[MAX_LEN][MAX_LEN];
11 
12 void
13 dfs(int x, int y, int cur)
14 {
15     int i, mark, next_x, next_y;
16     mark = 0;
17     hash[map[x][y]] = 1;
18     for(i=0; i<4; i++) {
19         next_x = x + dx[i];
20         next_y = y + dy[i];
21         if(is_valid(next_x, next_y) && !hash[map[next_x][next_y]]) {
22             mark = 1;
23             hash[map[next_x][next_y]] = 1;
24             dfs(next_x, next_y, cur+1);
25             hash[map[next_x][next_y]] = 0;
26         }
27     }
28     if(!mark)
29         rt = cur>rt ? cur : rt;
30 }
31 
32 int
33 main(int argc, char **argv)
34 {
35     int i;
36     while(scanf("%d %d"&R, &C) != EOF) {
37         for(i=0; i<R; i++)
38             scanf("%s", map[i]);
39         rt = -1;
40         memset(hash, 0sizeof(hash));
41         dfs(001);
42         printf("%d\n", rt);
43     }
44 }

posted on 2010-10-17 01:19 simplyzhao 阅读(180) 评论(0)  编辑 收藏 引用 所属分类: B_搜索


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


导航

<2011年8月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜