A Za, A Za, Fighting...

坚信:勤能补拙

PKU 3051 Satellite Photographs

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

思路:
还是教科书式的DFS典型应用,简单题

代码:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_W 83
 5 #define MAX_H 1001
 6 #define is_valid(x,y) (x>=0 && x<H && y>=0 && y<W)
 7 const int dx[] = {-1100};
 8 const int dy[] = {00-11};
 9 char photo[MAX_H][MAX_W];
10 int hash[MAX_H][MAX_W];
11 int W, H;
12 
13 int
14 dfs(int x, int y)
15 {
16     int i, nx, ny, rt = 1;
17     hash[x][y] = 1;
18     for(i=0; i<4; i++) {
19         nx = x+dx[i];
20         ny = y+dy[i];
21         if(is_valid(nx, ny) && photo[nx][ny]=='*' && !hash[nx][ny]) 
22             rt += dfs(nx, ny);
23     }
24     return rt;
25 }
26 
27 int
28 solve()
29 {
30     int i, j, tmp, value = -1;
31     for(i=0; i<H; i++)
32         for(j=0; j<W; j++) {
33             if(photo[i][j]=='*' && !hash[i][j]) {
34                 tmp = dfs(i, j);
35                 value = tmp > value ? tmp : value;
36             }
37         }
38     return value;
39 }
40 
41 int
42 main(int argc, char **argv)
43 {
44     int i;
45     while(scanf("%d %d"&W, &H) != EOF) {
46         for(i=0; i<H; i++)
47             scanf("%s", photo[i]);
48         memset(hash, 0sizeof(hash));
49         printf("%d\n", solve());
50     }
51 }

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


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


导航

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜