晓天动漫

Coding yy life……

Timus 1033. Labyrinth

 1 /* 
 2  * File:   Timus 1033. Labyrinth
 3  * Author: xiaotian @ hnu
 4  * Created on 2010年9月29日, 上午10:30
 5  * 题解:bfs数墙面个数,最后乘以 9 。
 6  */
 7 
 8 #include<stdio.h>
 9 #include<iostream>
10 #include<string.h>
11 #include<queue>
12 #include<set>
13 #include<math.h>
14 using namespace std;
15 #define N 40
16 #define inf 0x7ffffff
17 const int dx[4= {010-1};
18 const int dy[4= {10-10};
19 char map[N][N];
20 int vis[N][N][4];
21 int num[N][N];
22 int n;
23 
24 struct point {
25     int x, y;
26     point(int xx = 0int yy = 0) : x(xx), y(yy) {
27     }
28 };
29 
30 inline int ok(int x, int y) {
31     return x > 0 && x <= n && y > 0 && y <= n;
32 }
33 
34 int bfs(point s) {
35     int ans = 0;
36     queue<point>q;
37     q.push(s);
38     num[s.x][s.y] = 1;
39     while (!q.empty()) {
40         point p = q.front();
41         q.pop();
42         for (int i = 0; i < 4; i++) {
43             int x = p.x + dx[i], y = p.y + dy[i];
44             if ((x == 0 || x == n + 1 || y == 0 || y == n + 1 || map[x][y] == '#'&& vis[p.x][p.y][i] == 0 && vis[x][y][(i + 2% 4== 0) {
45                 ans++;
46                 vis[p.x][p.y][i] = 1;
47                 vis[x][y][(i + 2% 4= 1;
48             }
49             if (ok(x, y) && map[x][y] == '.' && num[x][y] == 0) {
50                 num[x][y] = 1;
51                 q.push(point(x, y));
52             }
53         }
54     }
55     return ans;
56 }
57 
58 int main() {
59     while (scanf("%d\n"&n) != EOF) {
60         for (int i = 1; i <= n; i++)
61             gets(map[i] + 1);
62         for (int i = 0; i <= n + 2; i++)
63             for (int j = 0; j <= n + 2; j++) {
64                 num[i][j] = 0;
65                 for (int k = 0; k < 4; k++)
66                     vis[i][j][k] = 0;
67             }
68         int ans=bfs(point(1,1))+bfs(point(n,n));
69         printf("%d\n", ans*9-36);
70     }
71     return 0;
72 }

posted on 2010-09-29 11:23 晓天_xiaotian 阅读(182) 评论(0)  编辑 收藏 引用 所属分类: 搜索专版


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


导航

<2010年9月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

统计

常用链接

留言簿

随笔分类

随笔档案

相册

收藏夹

ACM Online Judge

搜索

最新评论

阅读排行榜

评论排行榜