BNU 26582 Gregory the Grasshopper【简单BFS】
来源:互联网 发布:c语言读取多个字符串 编辑:程序博客网 时间:2024/05/16 11:45
链接:
D. 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, and LC. 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 of GR,GC are the coordinates of the square that Gregory is standing on, and LR, LC are 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
code:
/**题意:类似于骑士周游列国 给你棋盘大小和起点终点, 求到达终点的最小步数,如果不能到达则输出impossible 算法:BFS注意:输入时不要忘了 != EOF 。。。。贡献两次 TLE ToT */ #include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<iostream>using namespace std;const int maxn = 110;const int dir[8][2] = { 1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1, -2,1, -1,2 };struct Node{int step;int x,y;};int flag;int step;int r,c,gr,gc,lr,lc;int vis[maxn][maxn];void bfs(){memset(vis, 0, sizeof(vis));vis[gr][gc] = 1;queue<Node> q;while(!q.empty()) q.pop();Node now, next;now.x = gr; now.y = gc; now.step = 0;q.push(now);while(!q.empty()){now = q.front(); q.pop();for(int i = 0; i < 8; i++){next.x = now.x+dir[i][0];next.y = now.y+dir[i][1];if(next.x >= 1 && next.x <= r && next.y >= 1 && next.y <= c && !vis[next.x][next.y]) { vis[next.x][next.y] = 1; next.step = now.step+1; q.push(next); if(next.x == lr && next.y == lc) { flag = 1; step = next.step; return; } }}}return;}int main(){while(scanf("%d%d%d%d%d%d", &r,&c,&gr,&gc,&lr,&lc) != EOF){flag = 0;step = 0;if(gr == lr && gc == lc) {printf("0\n"); continue;}bfs();if(flag) printf("%d\n", step);else printf("impossible\n");}return 0;}
- BNU 26582 Gregory the Grasshopper【简单BFS】
- BNU 26582Gregory the Grasshopper 搜索水题
- Gregory the Grasshopper BFS搜索
- Gregory the Grasshopper(BFS搜索)
- CTU 2012 Gregory the Grasshopper (BFS)
- [codeforces] A. Grasshopper And the String 简单模拟
- bnu 14327 The Water Bowls[bfs,状态压缩]
- BNU 1642 简单的图论问题? 最短路+BFS
- Grasshopper And the String
- Grasshopper And the String
- The Ass and the Grasshopper
- 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
- 爬虫相关
- 37-iframe自适应高度
- 苹果公司调整应用排名算法:加入用户评级
- 答Rich──關於特徵值與特徵向量的物理意義
- C# 代码实现的万年历
- BNU 26582 Gregory the Grasshopper【简单BFS】
- 多线程编程一:带有信号量及计数器的多线程线程池
- Spring3.0 注解
- Line计划明年在美国或日本上市 估值或超44亿美元
- 特徵向量是甚麽物,恁麽來?
- 从一个程序员笑话看软件开发管理
- Windows套接字(Socket)例子(源码,实例)
- STM32F103定时器输出PWM波控制直流电机
- poj2777(延迟更新+线段树)