<span style="color:#330099;">/*
D - 广搜 基础
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1. Input
The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Output
For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.
Sample Input
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
Sample Output
5
28
0
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[302][302];
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,y1,y2;
//int best=100;
int res;
typedef struct{
int x;
int y;
int sum;
}node;
node q[100000];
int t;
int l;
bool can(int x,int y)
{
if(x<l&&x>=0&&y<l&&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==y2){
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;
cin>>t;
while(t--){
cin>>l;
cin>>x1>>y1;
cin>>x2>>y2;
//s1[2]='\0';
// puts(s1);
top=-1;base=0;
memset(flag,0,sizeof(flag));
flag[x1][y1]=1;
q[++top].x=x1;
q[top].y=y1;
q[top].sum=0;
slove();
printf("%d\n",ree);
}
return 0; }
</span>

D广搜的更多相关文章

  1. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  2. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  3. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

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

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  5. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  6. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  7. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  8. 双向广搜 codevs 3060 抓住那头奶牛

    codevs 3060 抓住那头奶牛 USACO  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold   题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...

  9. 双向广搜+hash+康托展开 codevs 1225 八数码难题

    codevs 1225 八数码难题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Yours和zero在研究A*启 ...

  10. UVa12726 one Friend at a Time (位 广搜)

    题目链接:UVa12726 是个PDF,不好复制进来. 大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友.共有N个人玩QQ,编号为1到N,1是男主 ...

随机推荐

  1. JavaSE-25 AWT

    学习要点 关于AWT AWT容器 布局管理器 AWT组件 事件处理 关于AWT java.awt包与子包 AWT软件包 说明 import  java.awt.*; 基本组件使用工具 import  ...

  2. luogu P4752

    给定一个数字 A ,这个 A 由 a1,a2,...,aN 相乘得到. 给定一个数字 B ,这个 B 由 b1,b2,⋯,bM 相乘得到. 如果 A/B​ 是一个质数,请输出YES,否则输出NO. 输 ...

  3. HDU-5253-链接的管道

    http://acm.hdu.edu.cn/showproblem.php?pid=5253 #include <iostream> #include <bits/stdc++.h& ...

  4. bzoj3336 Uva10572 Black and White

    题目描述: 数据范围:2<=n,m<=8 题解: 很明显需要状压.但是怎么压不知道,压什么不知道. 然后从条件下手. 条件1要求黑色在一起白色在一起,记录轮廓线很容易做到. 条件2要求不能 ...

  5. Openjudge-2815-城堡问题

    对于这道题目来说的话,我们的思路是这样的,我们首先把数据读进来,然后把color数组清零. 我们的思路是这样的的,给每一个房间设置一个对应的color数组,然后color数组里面填满不同的数字,每一种 ...

  6. Java之浅拷贝与深拷贝

    ----?浅拷贝 --- 概念 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.简单说,浅拷贝就是只复制所考虑的对象,而不复制它所引用的对象 --- 实现方 ...

  7. Centos7中yum安装jdk及配置环境变量

    系统版本 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) #安装之前先查看一下有无系统 ...

  8. 夯实Java:从面向对象说起

    作者:伯特出处:github.com/ruicbAndroid/LoulanPlan声明:本文出自伯特的<LoulanPlan>,转载务必注明作者及出处. 刚学习 Java 那会就接触了& ...

  9. POJ 3122 pie (二分法)

    Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have ...

  10. JustinMind

    看到公司老板新请来的兼职产品经理,在讲项目功能设计图是,用的是justinmind这个工具,觉得很好奇,默默记下,或许以后能用到.下面是搜的简单的介绍,只是为了记住这个工具名字,现并没有想要深入探究这 ...