BFS例题 ZOJ——1091 Knight Moves
来源:互联网 发布:vba数组可否使用clear 编辑:程序博客网 时间:2024/05/21 10:13
例题:ZOJ——1091
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<stdlib.h>#include<iostream>#include<queue>#include<vector>using namespace std;#define maxn 1010char aa[10],bb[10];int fx[8][2]={1,2,2,1,2,-1,1,-2,-1,-2,-2,1,-2,-1,-1,2};struct stu{ int x,y,step;};int vis[10][10];void bfs(int sx,int sy,int ex,int ey){ queue<stu>q; //定义结构体类型的队列 while(!q.empty()) q.pop(); //清空队列 vis[sx][sy]=1; //处理起始点 stu cur; //定义结构体类型的变量,并赋初值 cur.x=sx,cur.y=sy,cur.step=0; q.push(cur); //将cur压入队列 while(!q.empty()) { cur=q.front(); //每次取队首元素并抛弃 q.pop(); if(cur.x==ex&&cur.y==ey) //如果满足条件则输出 { printf("To get from %s to %s takes %d knight moves.\n",aa,bb,cur.step); return; } for(int i=0;i<8;i++) //找到可以扩展的点,将其压入队列 { int xx=cur.x+fx[i][0]; int yy=cur.y+fx[i][1]; if(xx>=0&&xx<8&&yy>=0&&yy<8&&!vis[xx][yy]) { vis[xx][yy]=1; stu temp; temp.x=xx; temp.y=yy; temp.step=cur.step+1; q.push(temp); } } }}int main(){ while(scanf("%s%s",aa,bb)!=EOF) { getchar(); //将a1,b2转换成(0,0),(1,1) int sx=aa[0]-'a'; int sy=aa[1]-'0'-1; int ex=bb[0]-'a'; int ey=bb[1]-'0'-1; memset(vis,0,sizeof(vis)); bfs(sx,sy,ex,ey); } return 0;}
0 0
- BFS例题 ZOJ——1091 Knight Moves
- ZOJ 1091 Knight Moves (BFS)
- ZOJ 1091 Knight Moves 【BFS】
- zoj 1091 knight moves (bfs)
- ZOJ-1091 Knight Moves(bfs)
- BFS——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
- ZOJ 1091 POJ 2935 Knight Moves BFS水题
- OS X 10.9 Mavericks下如何安装Command Line Tools(命令行工具)
- 安装Android时 sdk manager解决方案
- vector内存管理
- 装饰模式
- java基础—几种for循环编程思想
- BFS例题 ZOJ——1091 Knight Moves
- 命令行wsimport 生成webservice客户端代码
- STM32编程相关笔记。
- 用Idea配置Tomcat及建立web项目
- 【Hadoop权威指南】初识Hadoop(第一天)
- 如何在waqs中调用soap web service
- java 如何跳出指定的for循环
- Apache
- LeetCode---(42)Trapping Rain Water