2488

#include <iostream>

using namespace std;

struct Step //记录每一步的坐标
{
 int x;
 int y;
};

bool over=false; //是否遍历的标记值
int col=0;        //棋盘的大小
int row=0;
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int chessBoard[50][50];
int Count=0;
Step step[100];

void print(int);
void travel(int,int);

void print(int n)
{
 cout << "Scenario #" << n << ":" << endl;
 if(over)
 {
  int i=0;
  while(i<col*row)
  {
   cout << char(step[i].x+64) << step[i].y;
   i++;
  }
  cout << endl;//提交的时候注意这里有两个回车
  cout << endl;
 }
 else
 {
  cout << "impossible" << endl << endl;
 }
}

void travel(int y,int x)
{
 if(Count==(col*row))
 { 
  over=true;
 }
 if(!over)
 {
  for(int i=0;i < 8 && !over;i++)
  {
   if((x+dir[i][0] <= col)&&((x+dir[i][0]) >= 1)&&((y+dir[i][1]) >= 1)&&((y+dir[i][1]) <= row)&&(!chessBoard[y+dir[i][1]][x+dir[i][0]]))
   {
    chessBoard[y+dir[i][1]][x+dir[i][0]]=true;
    step[Count].x=x+dir[i][0];
    step[Count].y=y+dir[i][1];
    Count++;
    travel(y+dir[i][1],x+dir[i][0]);
    Count--;
    //step[Count].x=0;
    //step[Count].y=0;
    chessBoard[y+dir[i][1]][x+dir[i][0]]=false;
   }
   }
 }
}
int main()
{
 int n;
 int k=1;
 int i, j;
 cin >> n;

 while(k<=n)
 { 
  over=false;
  Count=0; 
  for(i=0;i<50;i++)
  {
   for(j=0;j < 50;j++)
    chessBoard[i][j]=false;
  }
  for(i = 0;i < 100;i++)
  {
   step[i].x=0;
   step[i].y=0;
  }
  cin >> row >> col;
  step[Count].x=1;
  step[Count].y=1;
  chessBoard[1][1]=true;
  Count++;
  travel(1,1);
     print(k++);
 }

 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值