广搜深搜二

<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<stdio.h>
#include<string.h>
#include<queue>
#include<cstdio>
using namespace std;
bool flag[21][21];
char a[21][21];
int n,m;
int s1,f1;
typedef struct{
   int x;
   int y;
}node;
int next[4][2]={1,0,0,1,-1,0,0,-1};
int sum;
node q[10000];
int top,base;
bool can(int cc,int dd)
{
    if(cc>=0&&cc<m&&dd>=0&&dd<n&&flag[cc][dd]==0)
      return 1;
    return 0;
}

void slove()
{  int c,d,cc,dd;
   node q1;
    while(top>=base){
    // c=q.front().x;d=q.front().y;
    c=q[base].x; d=q[base].y;
      for(int i=0;i<4;i++)
        {
            cc=c+next[i][0];
            dd=d+next[i][1];
            if(can(cc,dd))
              {   q1.x=cc;
                  q1.y=dd;
                 // cout<<cc<<" "<<dd<<endl;
                  //q.push(q1);
                  q[++top]=q1;
                  flag[cc][dd]=1;
                  sum++;
              }
        }
        //q.pop();
        base++;
       }
}

int main()
{  node q1;
    while(1){
        sum=1;
        top=-1;base=0;
     memset(flag,0,sizeof(flag));
     cin>>n>>m;
     if(n==0&&m==0)
        break;
     for(int i=0;i<m;i++){
        // cout<<"zhang"<<endl;
       scanf("%s",&a[i]);}
      //  for(int i=0;i<m;i++)
       // puts(a[i]);
        /*for(int i=0;i<m;i++)
         puts(a[i]);*/
     for(int i=0;i<m;i++)
       for(int j=0;j<n;j++)
         {if(a[i][j]=='#')
             flag[i][j]=1;

         if(a[i][j]=='@')
               s1=i,f1=j;
         }
        q1.x=s1;
        q1.y=f1;
        flag[s1][f1]=1;
       // for(int i=0;i<m;i++)
      // for(int j=0;j<n;j++){
      // cout<<flag[i][j];
      // if(j==n-1) cout<<endl;}
        //q.push(q1);
        q[++top]=q1;
        slove();
        cout<<sum<<endl;
         }
         return 0;
}
</span>

广搜深搜二

时间: 2024-08-03 16:38:22

广搜深搜二的相关文章

B广搜深搜

<span style="color:#330099;">/* B - 广搜/深搜 基础 Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submit Status Description There is a rectangular room, covered with square tiles. Each tile is colored either red or blac

BZOJ_1615_[Usaco2008_Mar]_The Loathesome_Hay Baler_麻烦的干草打包机_(模拟+宽搜/深搜)

描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1615 一个主动轮带着一些轮子转,轮子带着轮子转,轮子带着轮子转...一个非主动轮只会被一个轮子带着转.求从主动轮到某一个轮子的路上所有轮子的转速的绝对值之和. 分析 从起点开始,枚举相接触的轮子,只要不是之前路上的(带着当前轮子转的)轮子,就继续往下走.宽搜深搜都可以. 注意: 1.%.0lf是会四舍五入的!所以要强制转化成int. 宽搜: 1 #include <bits/stdc++.h

广搜+深搜(搜索最短路径+最短步数)

#include <iostream> #include <string.h> #include <stack> #include <queue> #include <algorithm> using namespace std; const int maxn=10; struct P { int x,y; }point[maxn]; struct PP { int fx,fy; }path[maxn][maxn]; int num[maxn][

深搜和广搜

先说说这个神秘的深搜: 前几天我心中的大牛给我讲了深搜感觉还是挺简单的,可大牛告诉我深搜深搜深的让你不知道怎么搜 我们也是从一道题来看:n的全排列 比如说3的全排列 123 213 321 这就是全排列   在没学过深搜的时候我们会用for循环来做这道题 但是如果n很大的时候就不行了 所以我们就一定要深搜了 其实在我看来深搜和递归是亲兄弟 深搜就是递归的加强版 我先写一个简单的dfs(深搜)函数的代码 #include<stdio.h> int dfs(int x) { if(x > n

深搜 ,广搜,队列 nyoj 27 水池数目

水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池. 输入 第一行输入一个整数N,表示共有N组测试数据 每一组数据都是先输入该地图的行数m(0<m<100)与列数n(0<n<100),然后,输入接下来的m行每行输入n个数,表示此处有水还是没水(1表示此处是水

图的广搜与深搜模板

题目传送门 深搜 深搜,顾名思义就是往深了搜.每次,只要你发现下一个点可以走,你马上走上去 就行啦!!就这样不断递归,直到遇到你的边界条件,撞到了南墙,你再也走不下去了,哼哼,我回头退回去!! 可以跟大家看下样例::(自己去题面看图) 样例是从1出发,先走2,然后走5,发现走不下去,退回来. 站在2这里,我们又往6走,发现又走不下去了,退回2.此时2没有点可走,退回1,去往3. 在3点,我们前往7,然后再往8,无路可走,退回7->3——>1.最后走4. 广搜和深搜略有不同. 广搜就是先站在目前

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

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

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

图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其经典应用走迷宫.N皇后.二叉树遍历等.遍历即按某种顺序訪问"图"中全部的节点,顺序分为: 深度优先(优先往深处走),用的数据结构是栈, 主要是递归实现. 广度优先(优先走近期的).用的数据结构是队列.主要是迭代实现. 对于深搜.因为递归往往能够方便的利用系统栈,不须要自己维护栈.所以通常实

7.23 深搜广搜

深搜(DFS)  关键词:回溯 栈实现,(递归本质和栈一样)一直走到底再回溯,时间复杂度高,空间低 #include<iostream> #include<cstring> using namespace std; int R,C; char maps[40][40]; int dp[40][40]; int dir[2][4]={{1,0,0,-1},{0,1,-1,0}}; int ans=1<<30; void DFS(int x,int y,int step){