POJ 1096 Space Station Shielding _BFS

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #define sc(x) scanf("%d",&x)
 6 #define CL(x, y) memset(x,y,sizeof(x))
 7 using namespace std;
 8 const int MAX = 66;
 9 int A, B, C, D, i, num;
10 int used[MAX][MAX][MAX];
11 int tower[MAX][MAX][MAX];
12 int Move[6][3]= {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
13 void BFS(int a, int b, int c);
14 bool check(int x, int y, int z);
15 struct node
16 {
17     int x, y, z;
18     int time;
19 };
20 queue <node> Q;
21 void BFS();
22 int main()
23 {
24     while(scanf("%d%d%d%d",&A,&B,&C,&D)!=EOF)
25     {
26         if(!A && !B && !C && !D) break;
27         CL(tower, 0);
28         CL(used, 0);
29         int temp;
30         for(i = 0; i < D; i++)
31         {
32             sc(temp);
33             tower[temp%(A*B)%A+1][temp%(A*B)/A+1][temp/(A*B)+1] = 1;//=1是外面包了一层
34         }
35         BFS(0, 0, 0);
36         printf("The number of faces needing shielding is %d.\n", num);
37
38     }
39     return 0;
40 }
41 void BFS(int a, int b, int c)
42 {
43     int xx, yy, zz;
44     num = 0;
45     while(!Q.empty())
46         Q.pop();
47     node fir, cur, next;
48     fir.x = a;
49     fir.y = b;
50     fir.z = c;
51     fir.time = 0;
52     used[a][b][c] = 1;
53     Q.push(fir);
54     while(!Q.empty())
55     {
56         cur = Q.front();
57         Q.pop();
58         for(i = 0; i < 6; i++)
59         {
60             xx = cur.x+Move[i][0];
61             yy = cur.y+Move[i][1];
62             zz = cur.z+Move[i][2];
63             if(check(xx, yy, zz))//在范围内 && !used[xx][yy][zz] && )//哥哥,0代表路,请看清题目!tower[xx][yy][zz]
64             {
65                 if(!tower[xx][yy][zz])//有路
66                 {
67                     if(!used[xx][yy][zz])//没走过
68                     {
69                         next.x = xx;
70                         next.y = yy;
71                         next.z = zz;
72                         next.time = cur.time+1;
73                         used[xx][yy][zz] = 1;
74                         Q.push(next);
75                     }
76                 }
77                 else
78                     num++;//反之则加1, 想反了
79             }
80         }
81     }
82     return ;
83 }
84 bool check(int x, int y, int z)
85 {
86     if(x>=0 && x<=A+1 && y>=0 && y<=B+1 && z>=0 && z<=C+1)//应该是A+1,肯定是加2
87         return true;
88     return false;
89 }

错在立方体的坐标,但是解题思路也很关键

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <queue>
  5 #define sc(x) scanf("%d",&x)
  6 #define CL(x, y) memset(x,y,sizeof(x))
  7 using namespace std;
  8 const int MAX = 66;
  9 int A, B, C, D, num;
 10 int used[MAX][MAX][MAX];
 11 int tower[MAX][MAX][MAX];
 12 int Move[6][3]= {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
 13 bool check(int x, int y, int z);
 14 struct node
 15 {
 16     int x, y, z;
 17 //    int time;
 18 };
 19 void BFS();
 20 queue <node> Q;
 21 node madeQueue(int xx,int yy,int zz)
 22 {
 23     node rear;
 24     rear.x = xx;
 25     rear.y = yy;
 26     rear.z = zz;
 27     return rear;
 28 }
 29 int main()
 30 {
 31     while(scanf("%d%d%d%d",&A,&B,&C,&D)!=EOF)
 32     {
 33         if(!A && !B && !C && !D) break;
 34         CL(tower, 0);
 35         CL(used, 0);
 36         int temp;
 37         for(int j = 0; j < D; j++)
 38         {
 39             sc(temp);
 40             tower[temp%(B*A)%A+1][temp%(B*A)/A+1][temp/(A*B)+1] = 1;//=1是外面包了一层
 41         }
 42         BFS();
 43         cout<<"The number of faces needing shielding is "<< num <<".\n";
 44     }
 45     return 0;
 46 }
 47 void BFS()
 48 {
 49 //    int xx, yy, zz;
 50     num = 0;
 51 //    node fir, cur, next;
 52 //    fir.x = 0;
 53 //    fir.y = 0;
 54 //    fir.z = 0;
 55 //    fir.time = 0;
 56 //    used[0][0][0] = 1;
 57 //    Q.push(fir);
 58     Q.push(madeQueue(0,0,0));
 59     while(!Q.empty())
 60     {
 61         node cur = Q.front();
 62         Q.pop();
 63 //        for(i = 0; i < 6; i++)
 64 //        {
 65 //            xx = cur.x+Move[i][0];
 66 //            yy = cur.y+Move[i][1];
 67 //            zz = cur.z+Move[i][2];
 68 //            if(check(xx, yy, zz))//在范围内 && !used[xx][yy][zz] && )//哥哥,0代表路,请看清题目!tower[xx][yy][zz]
 69 //            {
 70 //                if(!tower[xx][yy][zz])//有路
 71 //                {
 72 //                    if(!used[xx][yy][zz])//没走过
 73 //                    {
 74 //                        next.x = xx;
 75 //                        next.y = yy;
 76 //                        next.z = zz;
 77 ////                        next.time = cur.time+1;
 78 ////                num++;
 79 //                        used[xx][yy][zz] = 1;
 80 //                        Q.push(next);
 81 //                    }
 82 //                }
 83 //                else
 84 //                    num++;//反之则加1, 想反了
 85 //            }
 86 //        }
 87         if(used[cur.x][cur.y][cur.z])continue;
 88         used[cur.x][cur.y][cur.z] = 1;
 89         if(cur.x>=1)
 90         {
 91             if(!tower[cur.x-1][cur.y][cur.z])
 92             {
 93                 if(!used[cur.x-1][cur.y][cur.z])
 94                 {
 95 //                    next.x = cur.x-1;
 96 //                    next.y = cur.y;
 97 //                    next.z = cur.z;
 98 //                    Q.push(next);
 99                     Q.push(madeQueue(cur.x-1,cur.y,cur.z));
100                 }
101             }
102             else num++;
103         }//左走一格
104         if(cur.x<=A)
105         {
106             if(!tower[cur.x+1][cur.y][cur.z])
107             {
108                 if(!used[cur.x+1][cur.y][cur.z])
109                 {
110 //                    next.x = cur.x+1;
111 //                    next.y = cur.y;
112 //                    next.z = cur.z;
113 //                    Q.push(next);
114                     Q.push(madeQueue(cur.x+1,cur.y,cur.z));
115                 }
116             }
117             else num++;
118         }//右走一格
119         if(cur.y>=1)
120         {
121             if(!tower[cur.x][cur.y-1][cur.z])
122             {
123                 if(!used[cur.x][cur.y-1][cur.z])
124                 {
125 //                    next.x = cur.x;
126 //                    next.y = cur.y-1;
127 //                    next.z = cur.z;
128 //                    Q.push(next);
129                     Q.push(madeQueue(cur.x,cur.y-1,cur.z));
130                 }
131             }
132             else num++;
133         }//后走一格
134         if(cur.y<=B)
135         {
136             if(!tower[cur.x][cur.y+1][cur.z])
137             {
138                 if(!used[cur.x][cur.y+1][cur.z])
139                 {
140 //                    next.x = cur.x;
141 //                    next.y = cur.y+1;
142 //                    next.z = cur.z;
143 //                    Q.push(next);
144                     Q.push(madeQueue(cur.x,cur.y+1,cur.z));
145                 }
146             }
147             else num++;
148         }//前走一格
149         if(cur.z>=1)
150         {
151             if(!tower[cur.x][cur.y][cur.z-1])
152             {
153                 if(!used[cur.x][cur.y][cur.z-1])
154                 {
155 //                    next.x = cur.x;
156 //                    next.y = cur.y;
157 //                    next.z = cur.z-1;
158 //                    Q.push(next);
159                     Q.push(madeQueue(cur.x,cur.y,cur.z-1));
160                 }
161             }
162             else num++;
163         }//下走一格
164         if(cur.z<=C)
165         {
166             if(!tower[cur.x][cur.y][cur.z+1])
167             {
168                 if(!used[cur.x][cur.y][cur.z+1])
169                 {
170 //                    next.x = cur.x;
171 //                    next.y = cur.y;
172 //                    next.z = cur.z+1;
173 //                    Q.push(next);
174                     Q.push(madeQueue(cur.x,cur.y,cur.z+1));
175                 }
176             }
177             else num++;
178         }//上走一格
179     }
180 //    printf("The number of faces needing shielding is %d.\n", num);
181     while(!Q.empty())
182         Q.pop();
183 }
184 bool check(int x, int y, int z)
185 {
186     if(x>=0 && x<=A+1 && y>=0 && y<=B+1 && z>=0 && z<=C+1)//应该是A+1,肯定是加2
187         return true;
188     return false;
189 }

时间: 2024-10-29 05:26:40

POJ 1096 Space Station Shielding _BFS的相关文章

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

[2016-04-14][POJ][203][Building a Space Station]

时间:2016-04-14 21:43:30 星期四 题目编号:[2016-04-14][POJ][203][Building a Space Station] 题目大意:给定n个球体,每个球体可能重合,可能包含,可能分离,问把每个球体连接起来(重合和包含看做已经连接),至少需要多长的路 分析:最小生成树,边权为 max(0,disij?ri?rj)max(0,disij?ri?rj),即重合和内含,边权为0 #include<cstdio> #include<cstring> #

poj 2031 Building a Space Station 【最小生成树 Prim】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5778   Accepted: 2874 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

Building a Space Station POJ 2031 【最小生成树 prim】

http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space statio

POJ 2031 Building a Space Station

Building a Space Station Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 2031 64-bit integer IO format: %lld      Java class name: Main You are a member of the space station engineering team, and are assigned

poj 2931 Building a Space Station &amp;lt;克鲁斯卡尔&amp;gt;

Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2910 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are ex

POJ 2031 Building a Space Station (最小生成树)

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepted: 2614 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

POJ 2031 &amp;&amp; ZOJ 1718--Building a Space Station【最小生成树 &amp;&amp; kurskal &amp;&amp; 水题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5655   Accepted: 2848 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar