BNU 26582Gregory the Grasshopper 搜索水题
来源:互联网 发布:消防防毒面具品牌知乎 编辑:程序博客网 时间:2024/06/05 15:39
Gregory the Grasshopper
Gregory is a grasshopper. His favourite food are clover leafs — he can simply never have enough of them. Whenever he spots such a leaf, he wants to eat it as quickly as possible. Gregory is also lazy, so he wants to move to the leaf with minimal effort. Your task is to help him to find the shortest way to a clover leaf.
For simplicity, we will assume that Gregory lives on a rectangular grid consisting of unit squares. As a grasshopper, he prefers to move by jumping (or, more exactly, hopping) from one square to the other. Each hop takes him to a square that is in the adjacent row or column in one direction, and two columns or rows away in the other direction. So, his hops resemble the moves of a knight on a chessboard.
Input
The input consists of several test cases, each of them specified by six integer numbers on one line:R, C, GR,GC, LR, andLC. R and C specify the size of the grid in unit squares, 1 ≤ R,C ≤ 100. Gregory cannot hop outside a rectangle of this size, because it would be too dangerous. The values ofGR,GCare the coordinates of the square that Gregory is standing on, andLR, LCare the coordinates of the square with the delicious clover leaf. (1 ≤GR, LR≤ R; 1 ≤ GC, LC≤ C)
Output
For each test case, print one integer number — the minimal number of hops that Gregory needs to reach the square with his beloved delicacy. If it is not possible to reach that square at all, print the word “impossible” instead.
Sample Input
10 10 10 10 1 12 2 1 1 1 28 8 1 1 1 2
Sample Output
6impossible3
Source
http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=26582
/*D题 Gregory the Grasshopperhttp://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1405#problem/15734题意 一只青蛙可以跳到8个方向 和马走日的方向一样 输入map大小以及起点 终点问从起点到终点所需要的最小步骤思路: 简化版的马走日 还省去了蹩脚条件直接BFS+队列*/#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> using namespace std; int step[10][2]={0,0,0,0,2,-1,2,1,1,2,-1,2,-2,-1,-2,1,1,-2,-1,-2},n,m; int p[5][2]={0,0,1,0,0,1,-1,0,0,-1}; int vis[200][200],ex,ey,flag,sx,sy,mm; struct haha { int x; int y; int step; //friend bool operator <(haha a,haha b) //{ // return a.step>b.step; //} }q,temp; void BFS() { int i,x,y,x1,y1; queue<haha>que; q.x=sx;q.y=sy;q.step=0; vis[sx][sy]=1; que.push(q); while(!que.empty()) { temp=que.front(); que.pop(); if(temp.x==ex&&temp.y==ey) { mm=temp.step;flag=1; return; } for(i=2;i<10;i++) { x=temp.x+step[i][0]; y=temp.y+step[i][1]; x1=temp.x+p[i/2][0]; y1=temp.y+p[i/2][1]; if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]) { vis[x][y]=1; q.x=x; q.y=y; q.step=temp.step+1; que.push(q); } } } } int main(){ while(scanf("%d %d %d %d %d %d",&n,&m,&sx,&sy,&ex,&ey)!=EOF) { flag=0; memset(vis,0,sizeof(vis)); BFS(); if(flag) printf("%d\n",mm); else printf("impossible\n"); } return 0;}
- BNU 26582Gregory the Grasshopper 搜索水题
- BNU 26582 Gregory the Grasshopper【简单BFS】
- Gregory the Grasshopper BFS搜索
- Gregory the Grasshopper(BFS搜索)
- CTU 2012 Gregory the Grasshopper (BFS)
- Grasshopper And the String
- Grasshopper And the String
- CodeForces 733 A.Grasshopper And the String(水~)
- The Ass and the Grasshopper
- BNU - Truchet Tiling-搜索
- codeforces733A.Grasshopper And the String
- A. Grasshopper And the String
- A. Grasshopper And the String
- NYOJ Grasshopper And the String
- BNU Avoid The Lakes
- BNU The Twin Towers
- BNU 33960 The table
- BNU-52305-Around the World(四川省赛H题)
- 继续学习-页面的HTML5改造
- CSS透明度大汇总
- 汉字字符串边界
- JQuery 显示隐藏切换
- 函数返回值—返回局部变量
- BNU 26582Gregory the Grasshopper 搜索水题
- CreateEvent;WaitForSingleObject(g_hEvent,INFINITE);;SetEvent(g_hEvent);
- 转载几个个人感兴趣的: 2012年Linux Journal读者选择奖结果公布
- HTML5高级编程之图形扭曲
- Understanding the parallelism of a Storm topology Page History
- ZXing核心库的编译
- MAC svn server 创建
- MySQL 日期查询
- 【shell】设置变量的默认值的几种写法