zoj 1091 && hdu 1372 Knight Moves
来源:互联网 发布:ping ip加端口 编辑:程序博客网 时间:2024/04/26 16:15
题意:给定起点和终点,求最少需几步完成。(bfs)
总结:queue<node> q;在函数外,每次用都要记得清空。
#include<cstdio>#include<queue>#include<cstring>#include<iostream>using namespace std;int dx[]={-1,-2,-2,-1,1,2,2,1};int dy[]={-2,-1,1,2,2,1,-1,-2};bool vis[10][10];struct node{ int x,y; int step;}st,ed;queue<node> q;bool ok(node u){ if(u.x>=1 && u.x<=8 && u.y>=1 && u.y<=8) return true; return false;}int bfs(){ while(!q.empty()) q.pop(); st.step=0; q.push(st); node cur,next; while(!q.empty()) { cur = q.front(); if(cur.x==ed.x && cur.y == ed.y) return cur.step; q.pop(); for(int i=0;i<8;i++) { next.x = cur.x+dx[i]; next.y = cur.y+dy[i]; if(ok(next) && !vis[next.x][next.y]) { vis[next.x][next.y]=true; next.step = cur.step+1; q.push(next); } } }}int main(){ char a,c; int b,d; while(scanf("%c%d %c%d",&a,&b,&c,&d)!=EOF) { memset(vis,0,sizeof(vis)); st.x = a-'a'+1; st.y = b; ed.x = c-'a'+1; ed.y = d; //cout<<st.x<<' '<<st.y<<' '<<ed.x<<' '<<ed.y<<endl; int ans = bfs(); printf("To get from %c%d to %c%d takes %d knight moves.\n",a,b,c,d,ans); getchar(); } return 0;}
0 0
- zoj 1091 && hdu 1372 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
- HDU 1372 knight moves
- HDU 1372 Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 Knight Moves
- U盘安装redhat6.0和win7双系统
- c++中的类模板
- iOS 7Development Recipes(中文版)之第二章 Storyboard(3)
- 判断javascrit 类型 Object.prototype.toString方法的原理
- 语音识别系统之kaldi----在timit上的实验
- zoj 1091 && hdu 1372 Knight Moves
- C#中制作启动窗体的方法和问题
- E8刷机日志
- 静态链接库与动态链接库导出函数详解(本文系转载)
- auto_ptr常见使用方法
- 面试题之陈利人 单链表和之恋
- 退出ssh继续执行命令
- 添加一个能够自适应字数的UILabel对象到ScrollView
- cocos2d-x酱油笔记之获取系统时间