广搜二

<span style="color:#330099;">/*
C - 广搜 基础
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit

Status
Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. 

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
By Grant Yuan
2014.7.12

*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
char a[9]={'0','a','b','c','d','e','f','g','h'};
char b[9]={'0','1','2','3','4','5','6','7','8'};
bool flag[9][9];
int next[8][2]={1,2,2,1,-1,2,2,-1,-2,-1,-1,-2,-2,1,1,-2};
char s1[5],s2[2];
char s[10];
int x1,x2,f1,f2;
int best=100;
typedef struct{
  int x;
  int y;
  int sum;
}node;
node q[100000];
bool can(int x,int y)
{
    if(x<=8&&x>0&&y<=8&&y>0&&flag[x][y]==0)
      return 1;
    return 0;
}
int ree;
int top,base;
void slove()
{int xx,yy,count,m,n;
     node q1;

    while(top>=base){
        if(q[base].x==x2&&q[base].y==f2){
            ree=q[base].sum;
            break;
            }

      else{
          m=q[base].x;
        n=q[base].y;
        count=q[base].sum;
        for(int i=0;i<8;i++)
          { xx=m+next[i][0];
            yy=n+next[i][1];
            if(can(xx,yy)){
               // cout<<xx<<" "<<yy<<endl;
                flag[xx][yy]=1;
                 q[++top].x=xx;
                 q[top].y=yy;
                 q[top].sum=count+1;              }
        }
        }base++;
      }
}

int main()
{node q1;
   while(scanf("%s%s",&s1,&s2)!=EOF){
       //puts(s1);
       //puts(s2);
      // for(int i=0;i<=1;i++)
        // s2[i]=s1[i+2];
       /*for(int i=0;i<=1;i++)
          s2[i]=s[i+2];*/
         // puts(s1);
         // puts(s2);
       s1[2]='\0';
     //  puts(s1);
       top=-1;base=0;
       memset(flag,0,sizeof(flag));
       for(int i=1;i<=8;i++)
         {
            if(s1[0]==a[i])
               x1=i;
            if(s1[1]==b[i])
               f1=i;
            if(s2[0]==a[i])
               x2=i;
            if(s2[1]==b[i])
               f2=i;
         }
      //cout<<x1<<" "<<f1<<" "<<x2<<" "<<f2<<endl;
         flag[x1][f1]=1;
         q[++top].x=x1;
         q[top].y=f1;
         q[top].sum=0;
        slove();
        printf("To get from %s to %s takes %d knight moves.\n",s1,s2,ree);
       }
        return 0;

}
</span>

广搜二

时间: 2024-10-04 02:45:32

广搜二的相关文章

广搜深搜二

<span style="color:#330099;">/* C - 广搜 基础 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!

poj1111 Image Perimeters 广搜

题目大意: 输入一个矩阵,再输入其中一个“X”的位置(从1开始).从该位置向八个方向扩展,如果是“X”就可以并在一起.问最后得到的模块的周长是多少. 解题思路: 按照广搜的思路来做.用一个二维的数组标记每一个点,-1代表着该点不能被搜索了(可能原本就是“.”,也可以该点已经出队列了):0代表着该点还没被搜到:1代表着该点已经被搜到,但是还在队列中. 初始周长为4,代表只有一个X时的周长.对于每一个点,如果是“X”,就初始化为标记为0:如果是"."就初始化为-1. 对于当前进行搜索的X的

poj 3984:迷宫问题(广搜,入门题)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

算法学习笔记 二叉树和图遍历—深搜 DFS 与广搜 BFS

图的深搜与广搜 马上又要秋招了,赶紧复习下基础知识.这里复习下二叉树.图的深搜与广搜.从图的遍历说起,图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其经典应用走迷宫.N皇后.二叉树遍历等.遍历即按某种顺序访问"图"中所有的节点,顺序分为: 深度优先(优先往深处走),用的数据结构是栈, 主要是递归实现: 广度优先(优先走最近的),用的数据结构是队列,主要是迭代实现: 对于深搜,由于递归往往可以方便的利

22..广搜:被围住的面积

.广搜:被围住的面积 编程计算由“*”号围成的下列图形的面积.面积计算方法是统计*号所围成的闭合曲线中水平线和垂直线交点的数目.如下图所示,在10*10的二维数组中,有“*”围住了15个点,因此面积为15. [样例输入]area.in 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 1

HDU 1401 Solitaire (双向广搜)

题意:在二维8*8的方格,给定4个初始点和4个最终点,问在8步内是否能从初始点走到最终点, 规则:每个点能上下左右移动,若4个方向已经有点则可以跳到下一个点. 双向广搜:同时对初始点和最终点广搜4步,对每一步记录状态,初始点为'1',最终点为'2', 若在限定时间内初始点的状态能到达'2',或最终点的状态能到达'1',则为YES!要记得排序.. #include<stdio.h> #include<string.h> #include<queue> #include&l

一道广搜寻路题

同样是在qq群里看到的题目,想了好久算法,实现也用了很久. 关于题目首先看图: 总的来说,就是一个二维迷宫的寻路,迷宫中有对应的钥匙和刺,每走一步会消耗1点Hp,当走到刺上时会额外消耗100点hp,持有对应颜色的钥匙通过刺时不用额外消耗Hp. 给予起点和终点的坐标,,输出移动方式,让人物抵达终点所消耗的Hp尽可能的小. 例子: 3 3 1..a##A...1 13 3这个是输入数据 第一个代表高 第二个宽 第三个是钥匙和陷阱的对数 .代表平地 #代表墙 小写字母是钥匙 大写字母是对应的陷阱输出为

nyoj 999——师傅又被妖怪抓走了——————【双广搜】

师傅又被妖怪抓走了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 话说唐僧复得了孙行者,师徒们一心同体,共诣西方.自宝象国救了公主,承君臣送出城西,沿路饥餐渴饮,悟空便为师傅去化斋,等悟空回来,悟净慌慌张张的对悟空说:“不好了,不好了”,还没等悟净说完,悟空说:“师傅又被妖怪抓走了”,悟净:“NO!” ,悟空一脸茫然,悟净:“师傅和二师兄都被妖怪抓走了”.悟空(晕!).为了防止悟空救人,妖怪先把唐憎和八戒分别藏起来,如果悟空在T分钟之后还没找到人,那必定是被妖怪吃