2410: The knight problem

2410: The knight problem


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K23093Standard

Can you play international chess?This problem has something about that,as you see ,on a normal 8*8 chessboard,there may be some barrier on some of the grids.If we know the knight's initial position and the final position,(naturally it can't reach the position where has barrier.)Daoyuanlee and STONE want to know the amount of the least steps it needs to get to the final position,but they can't solve it,so they need your help. Each grid of the normal 8*8 chessboard can be confirmed by the only number.This time we use 1~8 to express the row,and use 'a'~'h' to express the line. We know the knight in the chess game can move as the capital letter "L"(move two grids in one direction ,then move one grid in the vertical direction),and certaionly it can't move out of the chess board. For example ,in the picture you can see the initial and final and the barrier pisition,in which the 'n' stands for the initial position ,'N' stands for the final position,and 'b' stands for the barrier.So we find one way of the feasible methods is(a1->b3->a5->c6->e5->g4->h2->f1), the total steps it needs is 7.In fact int is the least steps.

Input

This problem consists of several test cases, each of which is described below: The first line of each test case contains one integer b(-1<=b<=62),which stands for the obstacle number of the grids in the chess board.b=-1 means the end of the input.The second line contains b different obstacles identified by there position. If b = 0, this line contains nothing. The third line is the knight's initial and final grid's number.which also seperated by space.the initial grid and final grid's number are different ,and both of them have no obstacle.

Output

For each case,the output format likes that: Board n:m moves n is the number of the case,(begins from 1),and m stands for the least steps of each case the moves needs.If the knight can't reach the final grid,ouput Board n: not reachable.

Sample Input

10
c1 d1 c5 c2 c3 c4 d2 d3 d4 d5
a1 f1
0

c1 b3
2
b3 c2
a1 b2
-1

Sample Output

Board 1: 7 moves
Board 2: 1 moves
Board 3: not reachable

 

Problem Source: STONE

 


This problem is used for contest: 86 

 

#include<iostream>
#include<cstring>
using namespace std;
const int inf=1<<20;
int map[9][9],vis[9][9];
int minc;bool flag;
int endi,endj,begini,beginj;
int xx[8]={1,2, 2, 1,-1,-2,-2,-1};
int yy[8]={2,1,-1,-2,-2,-1, 1, 2};
void dfs(int x,int y,int sum)
{
 vis[x][y]=sum;
 if(x==endi&&y==endj)
 {
  flag=true;
  if(sum<minc) minc=sum;
  return ;
 }
 int i;
 for(i=0;i<8;i++)
 {
  if(x+xx[i]<1||x+xx[i]>8||y+yy[i]<1||y+yy[i]>8||map[x+xx[i]][y+yy[i]]) continue;
  if(sum+1<vis[x+xx[i]][y+yy[i]]) dfs(x+xx[i],y+yy[i],sum+1);
 }
}
int main()
{
 int i,j,b,o;
 char ch; int ff=1;
 while(scanf("%d",&b)==1&&b!=-1)
 {
  memset(map,0,sizeof(map));
  for(i=1;i<=8;i++) for(j=1;j<=8;j++) vis[i][j]=inf;
  for(i=0;i<b;i++) {cin>>ch>>o;map[ch-'a'+1][o]=1;}
  cin>>ch>>o; begini=ch-'a'+1;beginj=o;
  cin>>ch>>o; endi=ch-'a'+1;endj=o;
  minc=inf;flag=false;
  dfs(begini,beginj,0);
  if(flag) printf("Board %d: %d moves/n",ff++,minc);
  else printf("Board %d: not reachable/n",ff++);
 }
 return 0;
}

 

 

 

 

#include<iostream>
#include<cstdio>
using namespace std;
const int INF = (1<<31)-1;
int tag=1,n,tmp,tmp2,c,c2,minc;
int dir[2][8]={{2,1,-1,-2,2,1,-1,-2},{1,2,2,1,-1,-2,-2,-1}},map[8][8];
void dfs(int x,int y,int step)
{
    if(map[x][y]=='D')
    {
        minc=step<minc?step:minc;
        return ;
    }
    if(step>minc) return;
    for(int i=0;i<8;i++)
    {
        if(x+dir[0][i]>7 || x+dir[0][i]<0 || y+dir[1][i]>7 || y+dir[1][i]<0 ) continue;
        if(map[x+dir[0][i]][y+dir[1][i]]=='b' ) continue;
        map[x][y]='b';
        dfs(x+dir[0][i],y+dir[1][i],step+1);
        map[x][y]='.';
    }
}
int main()
{
    while(scanf("%d",&n),n!=-1)
    {
        memset(map,'.',sizeof(map));
        for(int i=0;i<n;i++)
        {
            scanf(" %c%d",&tmp,&c);
            map[tmp-'a'][c-1]='b';
        }
        scanf(" %c%d %c%d",&tmp,&c,&tmp2,&c2);
        map[tmp2-'a'][c2-1]='D'; minc=INF;
        dfs(tmp-'a',c-1,0);
        if(minc < INF)printf("Board %d: %d moves/n",tag++,minc);
        else printf("Board %d: not reachable/n",tag++);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值