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

解法:广搜

#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. HDU1070:Milk

    Milk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  2. hdu1081 DP类最大子段和(二维压缩+前缀和数组/树状数组计数)

    题意:给出一个 n * n 的数字矩阵,问最大子矩阵和是多少. 由于和最长子段和问题类似,一开始想到的就是 DP ,一开始我准备用两个循环进行 DP ,对于每一个 (i,j) ,考察(i - 1,j) ...

  3. 使用点聚 weboffice 以及vsto、 web service 实现word 的自动化文档处理

    开发环境的搭建: 1.visual studio 2010 2. 点聚web office 开发步骤 1. 创建word vsto 项目 比较简单 1. 添加任务窗格 页面如下: 代码如下: 1. 使 ...

  4. oracle 查某一列有重复值的记录

    -- 查找重复记录select names,num from test where rowid != (select max(rowid)                  from test b   ...

  5. pow 的使用和常见问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/menxu_work/article/details/24540045 1.安装: $ curl ge ...

  6. c++中的流

    streambuf类为缓冲区提供内存,并提供了用于填充缓冲区,访问缓冲区,刷新新缓冲区和管理缓冲区内存的类方法. ios_base类表示流的一般特征,如是否可读,是二进制还是文本流等. ios类基于i ...

  7. MySQL中UNSIGNED和ZEROFILL的介绍

    UNSIGNED: 将数字类型无符号化,这与C和C++这些程序语言的unsigned含义相同. INT的类型范围-2 147 483 648~2 147 483 647 INT UNSIGNED范围0 ...

  8. windows 安装操作系统时切换分区表格式

    在出现分区管理界面时,按下shift+F10呼出命令行,输入diskpart 后尝试如下命令将磁盘分区表手动转换到MBR. list disk ---- 显示当前磁盘列表 select disk x ...

  9. Linux 下V4l2摄像头采集图片,实现yuyv转RGB,RGB转BMP,RGB伸缩,jpeglib 库实现压缩RGB到内存中,JPEG经UDP发送功(转)

    ./configure CC=arm-linux-gnueabihf-gcc LD=arm-linux-gnueabihf-ld --host=arm-linux --prefix=/usr/loca ...

  10. bzoj1042硬币购物

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1042 dp预处理+容斥原理. 先预处理求出无限制的各面值的组成方案数 f (完全背包). 求s ...