Mai intai trebuie sa te autentifici.
Cod sursa(job #798579)
Utilizator | Data | 16 octombrie 2012 19:34:22 | |
---|---|---|---|
Problema | Kdrum | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 3.54 kb |
#include <iostream>
#include <fstream>
#include <queue>
#include <cstring>
using namespace std;
int N,M,K,config_;
pair<int,int> st,fn;
int num[10],div[10];
int a[55][55],maxp=1;
int p[55][55][1000];
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
struct stu
{
int first,second,conf;
};
queue <stu> Q;
inline stu make_stu(int a,int b,int c)
{
stu ret;
ret.first=a;
ret.second=b;
ret.conf=c;
return ret;
}
inline int sum(int nr,int nw) // or a kind of..
{
int v1[10],v2[10],v3[10];
int ret=0;
memset(v1,0,sizeof(v1));
memset(v2,0,sizeof(v2));
memset(v3,0,sizeof(v3));
//cout<<"SUM "<<nr<<' '<<nw<<'\n';
//cout<<"NUM "<<num[2]<<' '<<num[1]<<'\n';
for(int i=div[0];i>=1;--i)
{
//cout<<"v1 v2 v3: ";
if(nr>=1) v1[i]=nr%(num[i]+1);
//cout<<"v1["<<i<<"]="<<v1[i];
//cout<<" ";
nr/=(num[i]+1);
//cout<<"v2["<<i<<"]="<<v2[i]<<'\n';
if(nw>=1) v2[i]=nw%(num[i]+1);
nw/=(num[i]+1);
v3[i]=min(num[i],v1[i]+v2[i]);
//cout<<v1[i]<<' '<<v2[i]<<' '<<v3[i]<<'\n';
}
int pro=1;
for(int i=div[0];i>=1;--i)
{
//cout<<v3[i]<<' ';
ret=ret+pro*(v3[i]);
pro=pro*(num[i]+1);
}
//cout<<"RET in SUM "<<ret<<'\n';
//cout<<"\nATAT\n";
//cout<<"RET SUM!!! "<<ret<<'\n';
return ret;
}
inline int take_config(int dub) // from number to config
{
int config_=0,prod=1;
for(int i=div[0];i>=1;--i)
{
int abc=0;
while(dub%div[i]==0 && dub>1)
{
dub=dub/div[i];
++abc;
}
//cout<<"ab c";
config_+=abc*(prod);
prod=prod*(num[i]+1);
}
return config_;
}
int main()
{
ifstream in("kdrum.in");
ofstream out("kdrum.out");
in>>N>>M>>K;
in>>st.first>>st.second>>fn.first>>fn.second;
for(int i=1;i<=N;++i)
{
for(int j=1;j<=M;++j)
{
in>>a[i][j];
}
}
for(int k=K,d=2;k>1;++d)
{
int ndiv=0;
while(k%d==0)
{
k=k/d;
ndiv++;
}
if(ndiv>0)
{
div[++div[0]]=d;
num[++num[0]]=ndiv;
}
maxp=maxp*(ndiv+1);
}
maxp--;
//cout<<div[0]<<'\n';
//cout<<div[1]<<' '<<num[1]<<'\n';
//cout<<div[2]<<' '<<num[2]<<'\n';
int dub=a[st.first][st.second];
int dbg=0;
Q.push(make_stu(st.first,st.second,take_config(a[st.first][st.second])));
p[st.first][st.second][take_config(a[st.first][st.second])]=1;
int step=0;
for(;!Q.empty();Q.pop())
{
//cout<<"pasul "<<step++<<'\n';
stu ret=Q.front();
//cout<<"RET "<<ret.first<<' '<<ret.second<<' '<<ret.conf<<'\n';
for(int k=0;k<4;++k)
{
//cout<<"amplc ";
//cout<<"take it here";
stu acm=ret;
acm.first+=dx[k];
acm.second+=dy[k];
if(a[acm.first][acm.second]==0 && p[acm.first][acm.second][maxp]==0)
{
if(acm.first>0 && acm.first<=N && acm.second>0 && acm.second<=M)
{
//cout<<"ZERO "<<acm.first<<' '<<acm.second<<' '<<acm.conf<<'\n';
p[acm.first][acm.second][maxp]=p[ret.first][ret.second][ret.conf]+1;
acm.conf=maxp;
Q.push(acm);
}
}
else
if(acm.first>0 && acm.first<=N && acm.second>0 && acm.second<=M)
{
//cout<<"XYZ "<<acm.first<<' '<<acm.second<<' '<<a[acm.first][acm.second]<<'\n';
//cout<<"amplc ";
acm.conf=sum(acm.conf,take_config(a[acm.first][acm.second]));
if(p[acm.first][acm.second][acm.conf]==0)
{
//cout<<"fsdgs ";
//cout<<""<<acm.first<<' '<<acm.second<<' '<<acm.conf<<'\n';
p[acm.first][acm.second][acm.conf]=p[ret.first][ret.second][ret.conf]+1;
//cout<<"ACM "<<acm.first<<' '<<acm.second<<' '<<acm.conf<<'\n';
Q.push(acm);
}
}
}
}
//cout<<"STOP";
if(p[fn.first][fn.second]==0)
{
out<<p[fn.first][fn.second][0]<<'\n';
}
else
{
out<<p[fn.first][fn.second][maxp]<<'\n';
}
out.close();
return 0;
}