题意:一只棋盘上的马,从一个点到另外一个点要走多少步

解法:广搜

#include<stdio.h>
#include<iostream>
#include <strstream>
#include<string>
#include<memory.h>
#include<math.h>
#include<sstream>
using namespace std;
const int MAXR = 8;
const int MAXC = 'h' - 'a' + 1;
struct Node
{
int r;
int c;
int total;
};
const Node dir[] = { { -1, 2 }, { 1, 2 }, { -2, 1 }, { 2, 1 }, { -2, -1 }, { 2,
-1 }, { -1, -2 }, { 1, -2 } };
struct Queue
{
Node a[MAXR * MAXC];
int total;
int position;
Queue()
{
total = 0;
position = 0;
}
;
Node offerNode()
{
Node node = a[position++];
return node;
}
void pushNode(Node node)
{
a[total++] = node;
}
bool empty()
{
return position == total;
}
};
int bfs(Queue queue, Node e, int used[][MAXC + 1])
{
Node t;
while (!queue.empty())
{
t = queue.offerNode();
if (t.c == e.c && t.r == e.r)
{
return t.total;
}
int nt = t.total + 1;
for (int i = 0; i < 8; i++)
{
int er = t.r + dir[i].r;
int ec = t.c + dir[i].c;
if (er < 1 || ec < 1 || er > MAXR || ec > MAXC || used[er][ec] == 1)
{
continue;
}
Node node;
node.c = ec;
node.r = er;
node.total = nt;
queue.pushNode(node);
used[er][ec] = 1;
}
}
return 0;
}
int main()
{
//freopen("d:\\1.txt", "r", stdin);
char sc, sr;
char ec, er;
while (cin >> sc >> sr >> ec >> er)
{
int total = 0;
if (sc == ec && sr == er)
{
cout << "To get from " << sc << sr << " to " << ec << er
<< " takes " << total << " knight moves." << endl;
continue;
}
Queue queue;
Node s, e;
s.r = sr - '0';
s.c = sc - 'a' + 1;
e.r = er - '0';
e.c = ec - 'a' + 1;
s.total = 0;
queue.pushNode(s);
int used[MAXR + 1][MAXC + 1];
memset(used, 0, sizeof(used));
used[s.r][s.c] = 1;
total = bfs(queue, e, used);
cout << "To get from " << sc << sr << " to " << ec << er << " takes "
<< total << " knight moves." << endl;
}
}

  

UVA439-水题的更多相关文章

  1. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

  3. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...

  4. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  5. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  6. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  7. 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题

    B - 大还是小? Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Description 输入两个实数,判断第一个数大 ...

  8. ACM水题

    ACM小白...非常费劲儿的学习中,我觉得目前我能做出来的都可以划分在水题的范围中...不断做,不断总结,随时更新 POJ: 1004 Financial Management 求平均值 杭电OJ: ...

  9. CF451C Predict Outcome of the Game 水题

    Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...

  10. CF451B Sort the Array 水题

    Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...

随机推荐

  1. 如何用移动硬盘安装win7 系统

    身边没有U盘和光盘,就只有一个移动硬盘.移动硬盘安装系统是怎么进行的.在这里小毛孩来给大家上一课. 前期准备: 1.移动硬盘. 2.win7 32位的操作系统(*.iso). 3.有系统且可开机的电脑 ...

  2. idea_快捷键default&eclipse

    关键字: SpringMybatisplusRedisApplicationTests github关键字(springboot拦截器完整项目): implements WebMvcConfigure ...

  3. Oracle删除归档文件

    归档文件过大,会导致数据库出现异常,无法登陆. 1.D盘下新建一个delete_arch.txt文件 connect target / run { DELETE ARCHIVELOG ALL COMP ...

  4. 系列文章--从零开始学习ASP.NET MVC 1.0

    从零开始学习ASP.NET MVC 1.0 (一) 开天辟地入门篇 从零开始学习 ASP.NET MVC 1.0 (二) 识别URL的Routing组件 从零开始学习 ASP.NET MVC 1.0 ...

  5. 嵌入式Linux软件工程师面试题一

    题一,同步和异步有啥区别? 题二,TCP与UDP有啥区别? 题三,进程和线程有啥区别? 题一答案: 同步(Sync):所有的操作都做完,才返回给用户.这样用户在线等待的时间太长,给用户一种卡死了的感觉 ...

  6. mibox open ports

    root@dredd:/data/data/berserker.android.apps.sshdroid/home # netstat -lnpActive Internet connections ...

  7. cookie、localStorage、sessionStorage 的生命周期

    生命周期 存储 生命周期 cookie 没有设置 expires 选项时,cookie 的生命周期仅限于当前会话中,关闭浏览器意味着这次会话的结束,所以会话 cookie 仅存在于浏览器打开状态之下. ...

  8. springboot 知识点

    ---恢复内容开始--- 1springBoot项目引入方式, 1,继承自父 project (需要没有付项目才能用,一般我们的项目都会有 父 项目 所以 这种方式不推荐 ,记住有这种方式 就可以了) ...

  9. Windows下 YOLOv3配置教程(YOLOv3项VS2013平台迁移的方法)

    https://blog.csdn.net/maweifei/article/details/81150489

  10. MySQL性能指标及计算方法(go)

    绝大多数MySQL性能指标可以通过以下两种方式获取: (1)mysqladmin 使用mysqladmin extended-status命令获得的MySQL的性能指标,默认为累计值.如果想了解当前状 ...