// 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的更多相关文章

  1. 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 ...

  2. zju 1002

    // zju 1002 // #include "stdafx.h" #include <string> #include <iostream> using ...

  3. [swustoj 1091] 土豪我们做朋友吧

    土豪我们做朋友吧(1091) 问题描述: 人都有缺钱的时候,缺钱的时候要是有个朋友肯帮助你,那将是一件非常幸福的事情.有N个人(编号为1到N),一开始他们互相都不认识,后来发生了M件事情,事情分为2个 ...

  4. 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 ...

  5. 人生第一场组队赛---2014.8 zju monthly

    暑期集训中段就组了队,不过一直没机会打比赛 昨天kitkat突然发现了zju要搞月赛,我想了一下题目对于我这种渣实在是有点难,于是想到干脆打一次组队赛吧,跟队友商量了一下也同意了 12点---17点  ...

  6. [Swust OJ 1091]--土豪我们做朋友吧(并查集,最值维护)

    题目链接:http://acm.swust.edu.cn/problem/1091/ Time limit(ms): 1000 Memory limit(kb): 32768   人都有缺钱的时候,缺 ...

  7. ural 1091. Tmutarakan Exams(容斥原理)

    1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...

  8. 51Nod 1091 线段的重叠(贪心+区间相关,板子题)

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

  9. PAT 乙级 1091 N-自守数 (15 分)

    1091 N-自守数 (15 分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3×92​2​​=25392,而 25392 的末尾两位正好是 ...

随机推荐

  1. 数据库批量修改表名,增加前缀(SQL server)

    exec sp_msforeachtable @command1=' declare @o sysname,@n sysname select @o=''?'' ,@n=stuff(@o,1,7,'' ...

  2. CodeForces 508C Anya and Ghosts

     Anya and Ghosts Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  3. excel在一个图表内,显示折线图和柱状图

      折线图和柱状图,在同一个图表中拆分显示   一个图,设置主坐标轴 另外一个图,设置次坐标轴     拆分,通过调整纵坐标的最小值和最大值来实现     关于图表的标题,选中图表,选择布局,然后图表 ...

  4. Datatypes In SQLite Version 3

    http://www.sqlite.org/datatype3.html http://stackoverflow.com/questions/7337882/what-is-the-differen ...

  5. [HDOJ1231]最大连续子序列

    混了好几个地方的博客,还是觉得博客园比较靠谱,于是决定在这里安家落户了.本人本科生一个,希望各位巨巨多多指教~ Hello World! 单独一个象征性的问候实在是太low了,还是决定来点实质性的.. ...

  6. 08.安装Oracle 10g和SQLServer2008(仅作学习使用VirtualBox虚拟机来安装节省电脑资源)

    1.虚拟机和宿主机共享文件夹. 2.右ctrl+F切换VirtualBox全屏 3.安装Oracle 10g 4.输入密码:root------------>下一步 5.勾选网络配置" ...

  7. C语言--指针问题_1

    #include <stdio.h> #include <string.h> main() { int *a,*b,*c; a=b=c=(int *)malloc(sizeof ...

  8. Java抓取网页数据(原网页+Javascript返回数据)

    有时候由于种种原因,我们需要采集某个网站的数据,但由于不同网站对数据的显示方式略有不同! 本文就用Java给大家演示如何抓取网站的数据:(1)抓取原网页数据:(2)抓取网页Javascript返回的数 ...

  9. JS自总结

    1.js获得当前元素   event.srcElement:   获取当前父元素   event.srcElement.parentElement   var rowIndex = e.parentE ...

  10. oracle 10g正则表达式 REGEXP_LIKE 用法

    ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBS ...