UVA439-水题
题意:一只棋盘上的马,从一个点到另外一个点要走多少步
解法:广搜
#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-水题的更多相关文章
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 112[ ...
- [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 ...
- gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,
1195: 相信我这是水题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 821 Solved: 219 Description GDUT中有个风云人 ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
- 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题
B - 大还是小? Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Description 输入两个实数,判断第一个数大 ...
- ACM水题
ACM小白...非常费劲儿的学习中,我觉得目前我能做出来的都可以划分在水题的范围中...不断做,不断总结,随时更新 POJ: 1004 Financial Management 求平均值 杭电OJ: ...
- CF451C Predict Outcome of the Game 水题
Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
随机推荐
- JAVA爬取百度贴吧图片
package com.wang.xiaowei.utils; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.code ...
- (5)可变、不可变和hash函数
分类情况 与列表相似,列表用[],元组是()表示 内存角度看列表与数字的变与不变 列表 >>>l = [1,2,3,4] >>>id(l) 4392665160 & ...
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
通过调用静态工厂方法创建 bean: 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节. 要声明 ...
- ZH奶酪:最简单的Django安装方法(Windows 7)
前提是你已经在机器上安装了python~并且你的机器能够上网~ 1.进入:https://bootstrap.pypa.io/get-pip.py,ctrl+s保存网页(其实是.py文件)到某一目录: ...
- Linux 'XXXXXX' "is not in the sudoers file. This incident will be reported" 解决方法
添加方法如下: 1.进入root模式su - 注意:su和-之间有空格输入当前用户的密码 2.添加写权限chmod u+w /etc/sudoers 3.将自己加入到sudoers中 gedit / ...
- 使用php的curl根据关键词爬取百度搜索结果页
http://bbs.csdn.net/wap/topics/390938327 正则表达式匹配Html标签 查找所有的TD区域(最短):<td\s*.*>\s*.*<\/td& ...
- c++中的流
streambuf类为缓冲区提供内存,并提供了用于填充缓冲区,访问缓冲区,刷新新缓冲区和管理缓冲区内存的类方法. ios_base类表示流的一般特征,如是否可读,是二进制还是文本流等. ios类基于i ...
- RabbitMQ负载均衡方案之LVS
负载均衡的方案有很多,适合RabbitMQ使用的处理HAProxy之外还有LVS.LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件 ...
- 【转】每天一个linux命令(30): chown命令
原文网址:http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者 ...
- java 中的 hashcode
在Java的Object类中有一个方法: public native int hashCode(); 根据这个方法的声明可知,该方法返回一个int类型的数值,并且是本地方法,因此在Object类中并没 ...