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 的末尾两位正好是 ...
随机推荐
- [HDOJ5783]Divide the Sequence(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5783 题意:给n个数,要求划分成多个段,使得每一个段的任意前缀和都不小于0. 从后往前截取,这样不会影 ...
- CNV
CNV: 人类主要是二倍体.如果有些区域出现3个.4个拷贝,那就是扩增了,如果只出现1个拷贝,就是缺失.所以CNV分析是依靠特定位置的测序深度来估算的,先在染色体上划窗,然后看每个窗口的平均测序深度, ...
- hdu 4946 Area of Mushroom(凸包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4946 Area of Mushroom Time Limit: 2000/1000 MS (Java/Ot ...
- LINQ 如何实现 in 与 not in
T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Where CategoryID , ) T-SQL的NOT ...
- c++ 操作符 重载。
操作符如关系操作符,全局函数的话,必须第一个是class. 1.赋值(=),下标([ ]),调用(())和成员访问箭头(->)等操作符必须定义为成员,如果定义为非成员的话,程序在编译的时候,会发 ...
- [转载] 淘宝内部分享:怎么跳出MySQL的10个大坑(上)
原文: http://mp.weixin.qq.com/s?__biz=MzAxNjAzMTQyMA==&mid=209773318&idx=1&sn=e9600d3db80b ...
- Python学习(6)循环语句
目录 Python循环语句 - while循环语句 -- 无线循环 -- 循环使用else语句 -- 简单语句组 - for循环语句 -- 通过序列索引迭代 -- 循环使用else语句 - 循环嵌套 ...
- css 的小细节,小总结
CSS的一些零碎总结 1.CSS 伪元素用于向某些选择器设置特殊效果(用来当作一个东西的,跟一个元素差不多,但不是元素). ① :frist-line伪元素:用于向文本首行设置特殊样式,但是只能用于块 ...
- Java注解Annotation学习
学习注解Annotation的原理,这篇讲的不错:http://blog.csdn.net/lylwo317/article/details/52163304 先自定义一个运行时注解 @Target( ...
- 20160805_CentOS6_控制台切换
1. Ctrl + Alt + F1~F6 Ctrl + Alt + F1 是 图形界面(如果装了的话),后面的是 控制台界面 2. 3.