#include <iostream>
#include <vector>
// #include <algorithm>
#include <bitset>
using namespace std;
int n,count,numpath,si,sj,ans,tmp,path;
char x;
char a[10][10];
int mod=20437;
vector<int> dst;
int flood(int i,int j,int dist){
if(i<0||j<0||i==n||j==n)
return 1e6;
if(dist>dst[i*n+j])
return 1e6;
dst[i*n+j]=dist;
if(a[i][j]==x){
if(dist>tmp)
return 1e6;
if(dist<tmp)
tmp=dist,path=1;
else if(dist==tmp)
path++;
si=i,sj=j;
return path;
}
if(a[i][j]!='.')
return 1e6;
dist++;
flood(i+1,j,dist),flood(i,j+1,dist),flood(i-1,j,dist),flood(i,j-1,dist);
return path;
}
int main(){
cin>>n;
int cs=1;
while(n){
if(!n)
return 0;
count=0;
for(int i=0;i<n;i++){
ws(cin);
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j]>='A'&&a[i][j]<='Z'){
count++;
if(a[i][j]=='A')
si=i,sj=j,a[i][j]='.';
}
}
}
ans=0,numpath=1;
for(int i=1;i<count;i++){
dst.assign(100,1e6);
tmp=1e6,path=0,x=char('A'+i);
int cur=flood(si,sj,0);
a[si][sj]='.';
// cout<<x<<' '<<cur<<endl;
if(cur==1e6){
numpath=0;
break;
}
numpath*=cur%mod;
numpath%=mod;
ans+=tmp;
if(!numpath)
break;
}
cout<<"Case "<<cs++<<": ";
if(numpath)
cout<<ans<<' '<<numpath<<endl;
else
cout<<"Impossible"<<endl;
cin>>n;
}
}
#include <vector>
// #include <algorithm>
#include <bitset>
using namespace std;
int n,count,numpath,si,sj,ans,tmp,path;
char x;
char a[10][10];
int mod=20437;
vector<int> dst;
int flood(int i,int j,int dist){
if(i<0||j<0||i==n||j==n)
return 1e6;
if(dist>dst[i*n+j])
return 1e6;
dst[i*n+j]=dist;
if(a[i][j]==x){
if(dist>tmp)
return 1e6;
if(dist<tmp)
tmp=dist,path=1;
else if(dist==tmp)
path++;
si=i,sj=j;
return path;
}
if(a[i][j]!='.')
return 1e6;
dist++;
flood(i+1,j,dist),flood(i,j+1,dist),flood(i-1,j,dist),flood(i,j-1,dist);
return path;
}
int main(){
cin>>n;
int cs=1;
while(n){
if(!n)
return 0;
count=0;
for(int i=0;i<n;i++){
ws(cin);
for(int j=0;j<n;j++){
cin>>a[i][j];
if(a[i][j]>='A'&&a[i][j]<='Z'){
count++;
if(a[i][j]=='A')
si=i,sj=j,a[i][j]='.';
}
}
}
ans=0,numpath=1;
for(int i=1;i<count;i++){
dst.assign(100,1e6);
tmp=1e6,path=0,x=char('A'+i);
int cur=flood(si,sj,0);
a[si][sj]='.';
// cout<<x<<' '<<cur<<endl;
if(cur==1e6){
numpath=0;
break;
}
numpath*=cur%mod;
numpath%=mod;
ans+=tmp;
if(!numpath)
break;
}
cout<<"Case "<<cs++<<": ";
if(numpath)
cout<<ans<<' '<<numpath<<endl;
else
cout<<"Impossible"<<endl;
cin>>n;
}
}
Comments
Post a Comment