Toj 1198


http://acm.tju.edu.cn/toj/showp1198.html

这几天在写BFS,有点想吐的感觉。这个题是一个三维的bfs,除了有个特殊情况要考虑,其他的都比较中规中矩。这个特殊情况是,如果存在被含有acm的格子包围的、不含有acm的格子该怎么办。
我的办法就是把全体格子外边加一层空格子,然后从000开始bfs外围的格子。
还遇到一个情况就是:在有注释的那个if判断里头,最开始将nn>=0写成了nn<=0,结果第一次可以得出答案,第二次输入同样的数据结果就是零了,不知道为什么,比较诡异。
 1 #include <cstdlib>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 struct point
 7 {
 8     int n,m,k;
 9 }que[238330],ext[6];
10 
11 int bfs();
12 
13 int n,m,k,l,temp,map[62][62][62];
14 
15 
16 int main()
17 {
18     ext[0].n=1;ext[0].m=0;ext[0].k=0;
19     ext[1].n=-1;ext[1].m=0;ext[1].k=0;
20     ext[2].n=0;ext[2].m=1;ext[2].k=0;
21     ext[3].n=0;ext[3].m=-1;ext[3].k=0;
22     ext[4].n=0;ext[4].m=0;ext[4].k=1;
23     ext[5].n=0;ext[5].m=0;ext[5].k=-1;
24     int nn,nk,nm;
25     while(scanf("%d%d%d%d",&n,&m,&k,&l),n!=0||m!=0||k!=0)
26     {
27         memset(map,0,sizeof(map));
28         for(int t=0;t<l;t++)
29         {
30            scanf("%d",&temp);    
31            nk=temp/(m*n);
32            nm=(temp-m*n*nk)/n;
33            nn=temp-m*n*nk-n*nm;
34            nn++;
35            nm++;
36            nk++;
37            map[nn][nm][nk]=1;
38            //printf("%d %d %d\n",nn,nm,nk);
39         }
40         int re=bfs();
41         printf("The number of faces needing shielding is %d.\n",re);
42     }
43 
44     //system("PAUSE");
45     return 0;
46 }
47 
48 int bfs()
49 {
50     int top=0;
51     int re=0;
52     int nn,nk,nm;
53     que[top].n=que[top].m=que[top].k=0;
54     top++;
55     for(int t=0;t<top;t++)
56     for(int tt=0;tt<6;tt++)
57     {
58         nn=que[t].n+ext[tt].n;
59         nm=que[t].m+ext[tt].m;
60         nk=que[t].k+ext[tt].k;
61         printf("*");
62         if(nn>=0 && nn<=n+1 && nm>=0 && nm<=m+1 && nk>=0 && nk<=k+1//这里
63         {
64             if(map[nn][nm][nk]==1)
65                 re++;
66             else if(map[nn][nm][nk]==0)
67             {
68                 que[top].n=nn;
69                 que[top].m=nm;
70                 que[top].k=nk;
71                 top++;   
72                 map[nn][nm][nk]=-1;
73             }
74         }
75     }
76     return re;
77 }
 

posted on 2008-07-21 22:15 dosXP 阅读(102) 评论(0)  编辑 收藏 引用


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


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

导航

统计

公告

研究中...

常用链接

留言簿(1)

随笔档案(2)

文章档案(10)

搜索

最新评论

  • 1. re: 激情
  • 一起加油~~
    哈哈~

    今晚发现了好多人的blog
  • --mgy

阅读排行榜

评论排行榜