zoj 1091 Knight Moves
来源:互联网 发布:数据人生完本 编辑:程序博客网 时间:2024/05/06 21:43
本题采用了广度优先算法(BFS)。
第一次做。
广搜的一般结构:
定义一个队列;
起始点入队;
while(队列非空)
{
队头结点出列;
若为所求目标状态,跳出;
否则,开始查找,将其扩出的所有子节点,全部入队;
}
若循环中找到目标,输出结果;
否则,输出无解;
广搜的主要特点:
1. 每次队头元素出列时,扩展其全部子节点,并用队列记录下来;
2. 搜索过程没有回溯,是一种牺牲空间换取时间的方法。
本题参考代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; //可走的八个方向int mark[8][8];struct node{int x,y,step;};int main(){char a,b,c,d;node n,p;queue <node> q;memset(mark,0,sizeof(mark));while(scanf("%c%c %c%c",&a,&b,&c,&d)!=EOF){int sx=a-'a';int sy=b-'1';int ex=c-'a';int ey=d-'1';n.x=sx;n.y=sy;n.step=0;q.push(n);mark[sx][sy]=1;while(!q.empty()){n=q.front();q.pop();if(n.x==ex&&n.y==ey)break;else{for(int i=0;i<8;i++){int tx=n.x+dir[i][0];int ty=n.y+dir[i][1];if(tx>=0&&tx<8&&ty>=0&&ty<8&&!mark[tx][ty]){p.x=tx;p.y=ty;p.step=n.step+1;q.push(p);mark[tx][ty]=1;}}}}printf("To get from %c%c to %c%c ",a,b,c,d);printf("takes %d knight moves.\n",n.step);}return 0;}
0 0
- zoj 1091 Knight Moves
- zoj 1091 Knight Moves
- ZOJ-1091 Knight moves
- zoj 1091 Knight Moves
- zoj 1091 Knight Moves
- ZOJ 1091 Knight Moves
- zoj 1091 Knight Moves
- zoj 1091 Knight Moves
- ZOJ 1091 Knight Moves
- zoj 1091 Knight Moves
- ZOJ-1091-Knight Moves
- ZOJ 1091 Knight Moves (BFS)
- ZOJ 1091 Knight Moves 【BFS】
- zoj 1091 knight moves (bfs)
- ZOJ-1091 Knight Moves(bfs)
- ZOJ Problem Set - 1091 Knight Moves
- zoj - 1091 - Knight Moves(直接查找法)
- zoj - 1091 - Knight Moves(广度优先法)
- 确保nginx安全,请注意这10点
- 字符编码
- HDU 5340 Three Palindromes (manacher算法)
- ZOJ 题目1610 Count the Colors(线段树求颜色及有多少段)
- Linux I2C
- zoj 1091 Knight Moves
- hi3516a之faac
- 如何在Oracle官网上找到以前版本的JDK?
- 产品经理的开始
- 创建.pch文件
- Linux C内存泄露检测工具
- MyISAM InnoDB 区别
- hdu1010
- redis入门