| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:怎么做啊?In Reply To:Re:怎么做啊? Posted by:dddpppbox at 2005-09-14 22:43:33 > #include <iostream>
> #include <stdio.h>
> #include <set>
> #include <stack>
> #include <vector>
> #include <queue>
> using namespace std;
> int m,n,k,num;
> inline void kuo(int t,int cas,int &cnt);
> vector <bool> cube(216000);
> int op[216000];
> inline void input();
>
> inline int answer();
> inline void chu();
> inline int kk(int temp,int cas,int open[],queue <int> &t);
> inline bool tt(int ,int ,int);
> int main(int argc, char* argv[])
> {
> while(1)
> {
> int a;
> //init();
> cin>>n>>m>>k>>num;
> if(m==n && n==k && k==num && num==0) break;
> input();
> chu();
> a=answer();
> cout<<"The number of faces needing shielding is "
> <<a<<"."<<endl;
>
> }
> return 0;
> }
> inline int answer()
> {
> int cnt=0;
> for(int i=0;i<m*n*k;++i)
> {
> if(cube[i])
> {
> int mm,nn,kk;
> nn=i%n; mm=i/n%m; kk=i/(m*n)%k ;
> if(tt(nn-1,mm,kk)) cnt+=1;
> if(tt(nn+1,mm,kk)) cnt+=1;
> if(tt(nn,mm-1,kk)) cnt+=1;
> if(tt(nn,mm+1,kk)) cnt+=1;
> if(tt(nn,mm,kk-1)) cnt+=1;
> if(tt(nn,mm,kk+1)) cnt+=1;
> }
> }
> return cnt;
> }
> inline bool tt(int nn,int mm,int kk)
> {
> if(nn>=n || nn<0 || mm>=m || mm<0 || kk>=k || kk<0) {return true;}
> if(cube[kk*m*n+mm*n+nn]!=true) {return true;}
> return false;
> }
> inline void input()
> {
> int temp;
> for(int i=0;i<m*n*k;++i)
> cube[i]=false;
> for(int i=0;i<num;i+=1)
> {
> scanf("%d",&temp);
> cube[temp]=true;
> }
>
> }
> inline void chu()
> {
> queue <int> t;
> bool flag=false;
> int newvalue;
> for(int i=0;i<m*n*k;++i)
> {
> op[i]=0;
> if(cube[i])op[i]=1;
> }
> for(int i=0;i<m*n*k;++i)
> {
> if(op[i]==1 || op[i]==3) continue;
> while(!t.empty())
> t.pop();
> flag=false;
> op[i]=2;
> t.push(i);
> while(!t.empty()&& (!flag))
> {
> newvalue=t.front();t.pop();
> for(int i=0;i<6 && (!flag);++i){
> switch(kk(newvalue,i,op,t))
> {
> case 0: flag=true;break;
> case 1: break;
> case -1:break;
> }
> }
> }
> if(!flag)
> for(int i=0;i<m*n*k;++i)
> if(op[i]==2)
> { op[i]=1; cube[i]=true;}
> }
> }
> inline int kk(int temp,int cas,int open[],queue <int> &t)
> {
> int mm,nn,kk;
> bool flag=false;
> nn=temp%n;mm=temp/n%m;kk=temp/(m*n)%k;
> switch(cas)
> {
> case 0: kk-=1; break;
> case 1: kk+=1; break;
> case 2: mm+=1; break;
> case 3: mm-=1; break;
> case 4: nn+=1; break;
> case 5: nn-=1; break;
> }
> if(kk>=k || mm>=m || nn>=n || kk<0 || mm<0 || nn<0)
> {
> for(int i=0;i<m*n*k;++i)
> if(open[i]==2)
> open[i]=3;
> return 0;
> }
> temp=kk*m*n+mm*n+nn;
> if(open[temp]==1)
> return 1;
> else if(open[temp]==0)
> {
> open[temp]=2;
> t.push(temp);
> return -1;
> }
> else
> {
> open[temp]=3;
> for(int i=0;i<m*n*k;++i)
> if(open[i]==2)
> open[i]=3;
> }
> return 0;
> }
>
> /*inline int answer()
> {
> int nvalue,temp=0,cnt=0;
> while(!cube[temp])
> temp+=1;
> open.push(temp);
> close.insert(temp);
> while(!open.empty())
> {
> nvalue=open.top();open.pop();
> for(int i=0;i<6;++i)
> {
> kuo(nvalue,i,cnt);
> }
> }
> return cnt;
> }
> inline void kuo(int t,int cas,int &cnt)
> {
> int mm,nn,kk;
> bool flag=false;
> nn=t%n;mm=t/n%m;kk=t/(m*n)%k;
> switch(cas)
> {
> case 0: kk-=1; break;
> case 1: kk+=1; break;
> case 2: mm+=1; break;
> case 3: mm-=1; break;
> case 4: nn+=1; break;
> case 5: nn-=1; break;
> }
> if(kk>=k || mm>=m || nn>=n || kk<0 || mm<0 || nn<0)
> {
> cnt+=1;
> return;
> }
> t=kk*m*n+mm*n+nn;
> if(cube[t] )
> {
> if(close.count(t)) return;
> else
> {
> open.push(t);
> close.insert(t);
> }
> }
> else
> cnt+=1;
> return;
> }
> */
> 这是我的代码,不过TLE了
过了 153ms
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
#include <string>
using namespace std;
struct point
{
int x;
int y;
int z;
};
bool maze[65][65][65],vis[65][65][65][6],vis1[65][65][65],out[65][65][65];
int xx[]={0,0,-1,1,0,0},yy[]={0,0,0,0,-1,1},zz[]={1,-1,0,0,0,0};
int n,m,k,l,cnt;
point fir;
int judge(point a)
{
if((a.y ==0 || a.y ==(m-1)) && (a.x ==0 || a.x ==(n-1)))
return 0;
if((a.y ==0 || a.y ==(m-1)) || (a.x ==0 || a.x ==(n-1)))
return 0;
if((a.z ==0 || a.z ==(k-1)))
return 0;
return 1;
}
void bfs1(point c1)
{
queue<point> q;
point p=c1,cur,nt;
while(!q.empty())
q.pop() ;
q.push(p);
out[c1.z ][c1.y ][c1.x ]=1;
vis1[c1.z ][c1.y ][c1.x ]=1;
while(!q.empty() )
{
cur=q.front() ;q.pop() ;
for(int i=0;i<6;i++)
{
nt.z =cur.z +zz[i],nt.y =cur.y +yy[i],nt.x =cur.x +xx[i];
if(!vis1[nt.z ][nt.y ][nt.x ] && !maze[nt.z ][nt.y ][nt.x ] && nt.x >=0 && nt.x <n &&nt.y >=0 && nt.y <m && nt.z >=0 && nt.z <k)
vis1[nt.z ][nt.y ][nt.x ]=1,out[nt.z ][nt.y ][nt.x ]=1,q.push(nt);
}
}
}
void init()
{
point cur;
for(int i=0;i<k;i++)
{
for(int j=0;j<m;j++)
if(!maze[i][j][0] && !vis1[i][j][0])
cur.z =i,cur.y =j,cur.x =0,bfs1(cur);
for(int j=0;j<n;j++)
if(!maze[i][0][j] && !vis1[i][0][j])
cur.z =i,cur.y =0,cur.x =j,bfs1(cur);
for(int j=0;j<m;j++)
if(!maze[i][j][m-1] && !vis1[i][j][m-1])
cur.z =i,cur.y =j,cur.x =m-1,bfs1(cur);
for(int j=0;j<n;j++)
if(!maze[i][m-1][j] && !vis1[i][m-1][j])
cur.z =i,cur.y =m-1,cur.x =j,bfs1(cur);
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(!maze[0][i][j] && !vis1[0][i][j])
cur.z =0,cur.y =i,cur.x =j,bfs1(cur);
if(!maze[k-1][i][j] && !vis1[k-1][i][j])
cur.z =k-1,cur.y =i,cur.x =j,bfs1(cur);
}
}
void bfs()
{
int op;
queue<point> q;
point p,cur,next;
while(!q.empty() )
q.pop() ;
p.x =fir.x ,p.y =fir.y ,p.z =fir.z ;
q.push(p);
while(!q.empty() )
{
cur=q.front() ;q.pop() ;
for(int i=0;i<6;i++)
{
switch(i)
{
case 0:op=1;break;
case 1:op=0;break;
case 2:op=3;break;
case 3:op=2;break;
case 4:op=5;break;
case 5:op=4;break;
default:break;
}
next.x=cur.x +xx[i],next.y =cur.y +yy[i],next.z =cur.z +zz[i];
if(maze[next.z ][next.y ][next.x ] && !vis[next.z ][next.y ][next.x ][op] && next.x >=0 && next.x <n &&next.y >=0 && next.y <m && next.z >=0 && next.z <k)
cnt+=2,vis[next.z ][next.y ][next.x ][op]=1,vis[cur.z ][cur.y ][cur.x ][i]=1,q.push(next);
else if(!maze[next.z ][next.y ][next.x ] && !vis[next.z ][next.y ][next.x ][op]&& !out[next.z ][next.y ][next.x ] && next.x >=0 && next.x <n &&next.y >=0 && next.y <m && next.z >=0 && next.z <k)
vis[next.z ][next.y ][next.x ][op]=1,cnt++;
}
}
}
int main()
{
int x,y,z,nb;
while(~scanf("%d %d %d %d",&n,&m,&k,&l) && n)
{
cnt=0;
memset(vis,0,sizeof(vis));
memset(vis1,0,sizeof(vis1));
memset(out,0,sizeof(out));
memset(maze,0,sizeof(maze));
for(int i=0;i<l;i++)
{
scanf("%d",&nb);
z=nb/(n*m),y=(nb%(n*m))/n,x=(nb%(n*m))%n;
if(!i)
fir.z =z,fir.y=y,fir.x=x;
maze[z][y][x]=1;
}
init();
bfs();
printf("The number of faces needing shielding is %d.\n",l*6-cnt);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator