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 ...
随机推荐
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- NSObject之二
前面一章我们整理了NSObject类,这一章我们来看看NSObject协议的内容. NSObject协议提供了一组方法作为Objective-C对象的基础.其实我们对照一个NSObject类和NSOb ...
- 杭电 KazaQ's Socks
KazaQ wears socks everyday. At the beginning, he has n pairs of socks numbered from 1 to n in his cl ...
- dbt seed 以及base ephemeral使用
seed 可以方便的进行数据的导入,可以方便的进行不变数据(少量)以及测试数据的导入, base 设置为 ephemeral(暂态),这个同时也是官方最佳实践的建议 项目依赖的gitlab 数据可以参 ...
- 用户态监控网络接口up、down事件
网上搜索(https://blog.csdn.net/qq123386926/article/details/50695725)可以直接使用netlink现成的接口实现: #include <s ...
- [CLPR] 卷积还是相关? - Opencv之filter2D探究
I am doing something about convolving images in Python and for sake of speed I chose opencv 2.4.9. O ...
- dell support
部門營業時間 電話號碼訂單支援中小型企業 (員工不多於 500名 )00852-3416-0910 9:00 - 18:00 訂單編號: 810607806 訂單日期: 26/11/2014 客戶 ...
- nginx location正则写法(转载)
nginx location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # ...
- caffe跑densenet的错误:Message type "caffe.PoolingParameter" has no field named "ceil_mode".【转自CSDN】
最近看了densenet这篇论文,论文作者给了基于caffe的源码,自己在电脑上跑了下,但是出现了Message type “caffe.PoolingParameter” has no field ...
- You Arent Gonna Need It 理解
软件开发是一项无法驾驭全局的工作,其工作效率,工作精神都高度依赖需求反馈. 开发不存在需求的功能,实际上是除了要开发软件,还要构思需求,这样一正一反,严重消耗了脑力资源. 如果是自己开发软件,需求来自 ...