D广搜
<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广搜的更多相关文章
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5652(二分+广搜)
题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...
- nyoj 613 免费馅饼 广搜
免费馅饼 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- 双向广搜 codevs 3060 抓住那头奶牛
codevs 3060 抓住那头奶牛 USACO 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 农夫约翰被告知一头逃跑奶牛 ...
- 双向广搜+hash+康托展开 codevs 1225 八数码难题
codevs 1225 八数码难题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Yours和zero在研究A*启 ...
- UVa12726 one Friend at a Time (位 广搜)
题目链接:UVa12726 是个PDF,不好复制进来. 大意:有个人要追个妹子,想加妹子QQ,但是不知道谁规定的,玩QQ的人要加好友必须先要有至少k个共同好友.共有N个人玩QQ,编号为1到N,1是男主 ...
随机推荐
- saltstack入门个人笔记
offical website reference1 reference2 install apt-get install python-software-properties apt install ...
- INFORMATION_SCHEMA InnoDB 表
INFORMATION_SCHEMA InnoDB Tables 本节提供InnoDB INFORMATION_SCHEMA表的表定义. 有关相关信息和示例,请参见"InnoDB INFOR ...
- 02-Mysql中的运算符
Mysql中运算符 1.算术运算符运算符 作用+ 加法- 减法* 乘法/,DIV 除法,返回商%,MOD 除法,返回余数 mysql root@localhost: ...
- ffmpeg mp4 mp3 wav flac webm aac ac3 ogg格式转换
版权声明:本文为博主原创文章,未经允许不得转载. ffmpeg是Linux中转换音频视频文件的常用工具. mp4 to mp3: ffmpeg -i $ID.mp4 -acodec libmp3lam ...
- 一篇文章掌握nightwatch自动化测试
nightwatch.js是一个web-ui自动化测试框架,被vue-cli深度整合进来.如果一个项目是基于vue-cli搭建的,基本可以做到开箱即用. 但是我们不可能一直都使用vue-cli.因为它 ...
- 【HDU 6000】Wash(贪心)
Problem Description Mr.Panda is about to engage in his favourite activity doing laundry! He's brough ...
- Uva 11212 编辑书稿(迭代加深搜索)
题意: 给定N个数的序列, 希望将它排列成1~N, 可以用剪切.粘贴来完成任务, 每次可以剪切一段连续的自然段, 粘贴时按照顺序粘贴. #include <bits/stdc++.h> # ...
- [Go]程序实体
Go语言中的程序实体包括变量.常量.函数.结构体.接口 1.常见声明变量的方式 package main import ( "flag" "fmt" ) fun ...
- 人生第一场CTF的解题报告(部分)
解题报告 濮元杰部分: 王者归来: 120 场景 小王入职了一段时间,最近有点无聊.Web安全项目不多,白天看着其他项目组的同事忙得热火朝天,小王有点坐不住了,这也许是新人都会有的想法,乐于助人.想到 ...
- BZOJ 3039: 玉蟾宫【dp】
Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子,每个格子里写着'R'或者' ...