Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:怎么做啊?

Posted by dddpppbox at 2005-09-14 22:43:33 on Problem 1096
In Reply To:Re:怎么做啊? Posted by:dddpppbox at 2005-09-14 22:43:08
#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了

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator