这道题啊 。 。不说啥了 ~数据量很小,暴力深搜,不需要啥剪枝就能过。
代码如下:
#include <iostream>
using namespace std;
const int xp[8]={-2,-2,-1,-1,1,1,2,2};
const int yp[8]={-1,1,-2,2,-2,2,-1,1};
struct node
{
int px,py;
};
int n,p,q,tot,t=0;
node d[27];
bool flag,map[26][26];
void dfs(int x,int y,int sum)
{
int tx,ty,i,k;
map[x][y]=true;
d[sum].px=x;
d[sum].py=y;
if(sum==p*q)
{
flag=true;
cout<<"Scenario #"<<t<<":"<<endl;
for(k=1;k<=tot;k++)
cout<<(char)(d[k].px+'A')<<d[k].py+1;
cout<<endl;
cout<<endl;
return;
}
else
{
for(i=0;i<8;i++)
{
tx=x+xp[i];
ty=y+yp[i];
if(tx<0||tx>=q)
continue;
if(ty<0||ty>=p)
continue;
if(map[tx][ty])
continue;
dfs(tx,ty,sum+1);
}
}
d[sum].px=0;
d[sum].py=0;
map[x][y]=false;
}
int main()
{
int i,j;
cin>>n;
while(n--)
{
t++;
cin>>p>>q;
tot=p*q;
memset(map,false,sizeof(map));
flag=false;
for(i=0;i<q;i++)
{
if(flag)
break;
for(j=0;j<p;j++)
{
memset(d,0,sizeof(d));
dfs(i,j,1);
if(flag)
break;
}
}
if(!flag)
{
cout<<"Scenario #"<<t<<":"<<endl;
cout<<"impossible"<<endl;
cout<<endl;
}
}
return 0;
}