poj 2488 深搜

23 篇文章 0 订阅

 

 

如题:http://poj.org/problem?id=2488

 

开始搜索专题的系统学习,注意搜索结束标志,以及搜索状态,别忘记回溯 。

 

 

题目要求从任意一点走日字,是否可以走完整个棋盘,改动的地方在于字典序,横坐标是从A开始的字母,纵坐标是从1开始的数字,q是数字的个数,p是字母的个数。

 

还要注意这一题答案应该不是唯一的才对,为什么没有special judge呢? 原来是搜索方向的数组也要按字典序,纵坐标从向右,横坐标从下到上。

我因为vis数组wa了半天,原因是初始化vis数组时p,q写颠倒。反正只有26*26,干脆直接memset是最好的。

 

#include<cstdio>
#include<cstring>

#define N 30

int p,q;//数字,字母
int map[N][N];
//int dir[][2]={{-1,2},{1,2},{-2,1},{2,1},{-1,-2},{1,-2},{-2,-1},{2,-1}};
int dir[][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};
int vis[N][N];
int flg=0;


int DFS(int x,int y,int step)
{
 int i;
 if(step==p*q)
 {
  for(i=0;i<step;i++)
  printf("%c%d",map[i][0]+'A',map[i][1]+1);
  printf("\n");
  flg=1;
 }

 for(i=0;i<8;i++)
 {
  int tx=x+dir[i][0];
  int ty=y+dir[i][1];
  if(tx>=0&&tx<q&&ty>=0&&ty<p)
  if(!vis[tx][ty]&&flg==0)
  {
   vis[tx][ty]=1;
   map[step][0]=tx;
   map[step][1]=ty;
   DFS(tx,ty,step+1);
   vis[tx][ty]=0;
  }
 }
 return 0;
}

int main()
{
 //freopen("C:\\1.txt","r",stdin);
 int n;
 scanf("%d",&n);
 int i,j;
 int count=0;
 while(n--)
 {
  flg=0;
  count++;
  scanf("%d%d",&p,&q);
  for(i=0;i<p;i++)
   for(j=0;j<q;j++)
    vis[i][j]=0;
  map[0][0]=0; //A
  map[0][1]=0; //1
  vis[0][0]=1;
  printf("Scenario #%d:\n",count);
  DFS(0,0,1);
  if(!flg)
   printf("impossible\n");
  printf("\n");
 }
 return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值