| ||||||||||
| 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 | |||||||||
帮助偶 用数据测测哦!#include <iostream>
using namespace std;
int flag[30][30],k,row,col;
int step_x[30],step_y[30];
int dfs(int ,int);
const int dir_y[]={-2,-2,-1,-1,1,1,2,2};
const int dir_x[]={-1,1,-2,2,-2,2,-1,1};
int main()
{
int Case,i;
cin>>Case;
for(i=1;i<=Case;i++)
{
cin>>row>>col;
cout<<"Scenario #"<<i<<":"<<endl;
memset(flag,0,sizeof(flag));
flag[1][1]=1;
step_x[0]=step_y[0]=1;
k=1;
if(dfs(1,1))
for(i=0;i<k;i++)
cout<<char(step_y[i]+64)<<step_x[i];
else
cout<<"impossible";
cout<<endl<<endl;
}
return 0;
}
int dfs(int x,int y)
{
if(k==row*col)
return 1;
int i;
for(i=0;i<8;i++)
{
int g=x+dir_x[i];
int h=y+dir_y[i];
if(flag[g][h]==0&&g>0&&g<=row&&h>0&&h<=col)
{
flag[g][h]=1;
step_x[k]=g;
step_y[k]=h;
k++;
if(dfs(g,h))
return 1;
else
{
flag[g][h]=0;
k--;
}
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator