zju 1091
// Traveling Knight Problem
#include "stdafx.h"
#include <string>
#include <string.h>
#include<iostream>
#include <queue>
using namespace std;
int a[8][8];//棋盘
int MAP[8][2] = { { 2, 1 }, { 2, -1 }, { -2, 1 }, { -2, -1 }, { 1, 2 }, { -1, 2 }, { -1, -2 }, { 1, -2 } };//8个方向
typedef struct
{
int x;
int y;
int moves;
} NodeStru;
NodeStru Start, End,temp;
void bfs(queue<NodeStru>knight)
{
int x, y;
while (!knight.empty())
{
temp = knight.front();
knight.pop();
if (temp.x == End.x&&temp.y == End.y)
break;
for (int i = 0; i < 8; i++)
{
x = temp.x + MAP[i][0];
y = temp.y + MAP[i][1];
if (x >= 0 && x <8 && y >= 0 && y <8 &&!a[x][y])
{
Start.x = x, Start.y = y;
Start.moves = temp.moves + 1;
knight.push(Start);
a[x][y] = 1;//走过了
}
}
}
}
int main()
{
queue<NodeStru>knight;
string s1, s2;
//char s1[2], s2[2];
while (cin>>s1>>s2)
{ if(s1==s2)// (strcmp(s1,s2) == 0)
{
cout << "To get from " << s1 << " to "<<s2 << " takes 0 knight moves." << endl;
}
else
{
memset(a, 0, sizeof(a));
Start.x = s1[0] - 'a';
Start.y = s1[1] - 49;
a[Start.x][Start.y] = 1;
Start.moves = 0;
End.x = s2[0] - 'a';
End.y = s2[1] - 49;
///////////////////////////////////////////////
//入列
knight.push(Start);
bfs(knight);
cout << "To get from " << s1 << " to " << s2 << " takes "<<temp.moves<<" knight moves." << endl; } while (!knight.empty())
{
knight.pop();
}//记得清空栈!
} }
基本是老师的代码。。我就加了四句。。。但过程也是很不容易的,因为对visual studio太不熟悉了,为什么那个#include "stdafx.h"一定要加在第一句呢,还有cin>>char;
最后ctrl + z 会出现错误呢? 还有strcmp 为什么在string 时会出错?
zju 1091的更多相关文章
- Raspberry Pi 3 FAQ --- connect automatically to 'mirrors.zju.edu.cn' when downloading and how to accelerate download
modify the software source: The software source is a place where several free application for linux ...
- zju 1002
// zju 1002 // #include "stdafx.h" #include <string> #include <iostream> using ...
- [swustoj 1091] 土豪我们做朋友吧
土豪我们做朋友吧(1091) 问题描述: 人都有缺钱的时候,缺钱的时候要是有个朋友肯帮助你,那将是一件非常幸福的事情.有N个人(编号为1到N),一开始他们互相都不认识,后来发生了M件事情,事情分为2个 ...
- ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph
ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...
- 人生第一场组队赛---2014.8 zju monthly
暑期集训中段就组了队,不过一直没机会打比赛 昨天kitkat突然发现了zju要搞月赛,我想了一下题目对于我这种渣实在是有点难,于是想到干脆打一次组队赛吧,跟队友商量了一下也同意了 12点---17点 ...
- [Swust OJ 1091]--土豪我们做朋友吧(并查集,最值维护)
题目链接:http://acm.swust.edu.cn/problem/1091/ Time limit(ms): 1000 Memory limit(kb): 32768 人都有缺钱的时候,缺 ...
- ural 1091. Tmutarakan Exams(容斥原理)
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
- 51Nod 1091 线段的重叠(贪心+区间相关,板子题)
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- PAT 乙级 1091 N-自守数 (15 分)
1091 N-自守数 (15 分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3×922=25392,而 25392 的末尾两位正好是 ...
随机推荐
- application/x-www-form-urlencoded
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型. 下边是说明: application/x-www-form-urlen ...
- 安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案
安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径“C:\app\用户名\product\11.2 ...
- c语言中的位移位操作
先要了解一下C语言里所有的位运算都是指二进制数的位运算.即使输入的是十进制的数,在内存中也是存储为二进制形式. “<<”用法: 格式是:a<<m,a和m必须是整型表达式,要求m ...
- 在ubuntu上配置apue的运行环境
http://www.apuebook.com/code3e.html 在上面的网站下载代码包,解压得到源码 sudo apt-get install libbsd-dev 安装这个支持,在解压包的m ...
- hdu 4828 Grids 卡特兰数+逆元
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Problem D ...
- flex布局注意点:
1.父元素display:flex之后成为伸缩容器,子元素(除了position:absolute或fixed)无论是display:block或者display:inline,都成为了伸缩项目.2. ...
- 16 shell调试技术
trap 命令 基本格式: trap command sig1 sig2 ... sigN 有3种信号可以捕获: EXIT : 从函数中退出, 或整个脚本执行完毕 ERR: 当一 ...
- Oracle Regexp_substr
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','2 ...
- Git开源项目工作流程图
- hdu 1573 X问题
数论题,本想用中国剩余定理,可是取模的数之间不一定互质,用不了,看到网上有篇文章写得很好的:数论——中国剩余定理(互质与非互质),主要是采用合并方程的思想: 大致理解并参考他的代码后便去试试hdu上这 ...