[刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600
比较忙比较累,只贴代码了。
题目:6-4 UVa439 - Knight Moves
//UVa439 - Knight Moves
//Accepted 0.000s
//#define _XIENAOBAN_
#include<iostream>
#include<cstring>
#include<queue>
#define M(po) Map[po.x][po.y]
using namespace std;
struct poi {
int x, y, weight;
poi operator +(const poi &that) const {
return poi{ x + that.x, y + that.y, weight};
}
bool operator ==(const poi &that) const {
return (x == that.x) && (y == that.y);
}
} op, ed;
const poi dir[8] = { { 2,1 },{ -2,1 },{ 2,-1 },{ -2,-1 },{ 1,2 },{ -1,2 },{ 1,-2 },{ -1,-2 } };
bool Map[10][10];
char xstart, ystart, xend, yend;
int xs, ys, xe, ye;
int BFS(){
if (op == ed) return 0;
queue<poi> Q;
Q.push(op);
M(op) = true;
while (!Q.empty()) {
for (int i(0);i < 8;++i){
poi nxt(Q.front() + dir[i]);
if (nxt.x > 0 && nxt.y > 0 && nxt.x < 9 && nxt.y < 9 && !M(nxt)) {
++nxt.weight;
if (nxt == ed) return nxt.weight;
M(nxt) = true;
Q.push(nxt);
}
}
Q.pop();
}
return -1;
}
int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 129)
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
while (scanf("%c%c %c%c", &xstart, &ystart, &xend, ¥d) == 4) {
memset(Map, 0, sizeof(Map));
op.x = xstart - 96, op.y = ystart - 48, op.weight = 0;
ed.x = xend - 96, ed.y = yend - 48, ed.weight = 0;
printf("To get from %c%c to %c%c takes %d knight moves.\n", xstart, ystart, xend, yend, BFS());
while (getchar() != '\n');
}
return 0;
}
题目:6-5 UVa1600 - Patrol Robot
//UVa1600 - Patrol Robot
//Accepted 0.000s
//#define _XIENAOBAN_
#include<iostream>
#include<cstring>
#include<queue>
#define DONE 2333333
using namespace std;
struct step { int x, y, k, mov; };
int T, m, n, k;
int Map[24][24],Obst[24][24];
void judge(queue<step> &Q, step &now, int x, int y) {
if (Obst[x += now.x][y += now.y] == DONE) return;
int _k = (Obst[x][y] ? now.k + 1 : 0);
if (_k <= k) {
if (_k) {
if (Map[x][y] && Map[x][y] <= _k) return;
Map[x][y] = _k;
}
else Obst[x][y] = DONE;
Q.push(step{ x, y, _k, now.mov + 1 });
}
}
int main()
{
#ifdef _XIENAOBAN_
#define gets(T) gets_s(T, 129)
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &m, &n, &k);
for (int i(1);i <= m;++i) for (int j(1);j <= n;++j)
scanf("%d", &Obst[i][j]);
memset(Map, 0, sizeof(Map));
queue<step> Q;
Q.push(step{ 1,1,0,0 });
Obst[1][1] = DONE;
while (!Q.empty()) {
step &now(Q.front());
if (now.x == m && now.y == n) break;
if (now.x + 1 <= m) judge(Q, now, 1, 0);
if (now.y + 1 <= n) judge(Q, now, 0, 1);
if (now.x - 1 >= 1) judge(Q, now, -1, 0);
if (now.y - 1 >= 1) judge(Q, now, 0, -1);
Q.pop();
}
if (Q.empty()) printf("-1\n");
else printf("%d\n", Q.front().mov);
}
return 0;
}
[刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- [刷题]算法竞赛入门经典(第2版) 5-13/UVa822 - Queue and A
题意:模拟客服MM,一共有N种话题,每个客服MM支持处理其中的i个(i < N),处理的话题还有优先级.为了简化流程方便出题,设每个话题都是每隔m分钟来咨询一次.现知道每个话题前来咨询的时间.间 ...
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation
题意:模拟Petri网的执行.虽然没听说过Petri网,但是题目描述的很清晰. 代码:(Accepted,0.210s) //UVa804 - Petri Net Simulation //Accep ...
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
题意:二叉树代表使得平衡天平,修改最少值使之平衡. 代码:(Accepted,0.030s) //UVa12166 - Equilibrium Mobile //Accepted 0.030s //# ...
- [刷题]算法竞赛入门经典(第2版) 6-1/UVa673 6-2/UVa712 6-3/UVa536
这三题比较简单,只放代码了. 题目:6-1 UVa673 - Parentheses Balance //UVa673 - Parentheses Balance //Accepted 0.000s ...
- [刷题]算法竞赛入门经典(第2版) 5-16/UVa212 - Use of Hospital Facilities
题意:模拟患者做手术. 其条件为:医院有Nop个手术室.准备手术室要Mop分钟,另有Nre个恢复用的床.准备每张床要Mre分钟,早上Ts点整医院开张,从手术室手术完毕转移到回复床要Mtr分钟.现在医院 ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
- [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web
题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...
随机推荐
- react+webpack开发环境配置
react是目前非常热门的前端框架,提倡组件化开发.所谓的组件,简单理解,就是一个独立的页面部件(包括页面模版,样式,逻辑等),它是一个独立的整体. webpack,是一个模块打包工具,其主要功能,就 ...
- 用代码控制退出APP
+ (void)exitApplication { AppDelegate *app = [UIApplication sharedApplication].delegate; UIWindow *w ...
- 通过php动态传数据到highcharts
1:在平时工作中,在对数据进行展示的时候,是直接通过后台提供的接口来获取json串,用来展示.今天别人问怎么在本地演示一下请求的动态数据. 2:在本地搭建环境,我用的WampServer,下载地址:h ...
- WPF中的RichTextBox
原文链接:http://blog.csdn.net/wuzhengqing1/article/details/7010902 取出richTextBox里面的内容 第一种方法:将richTextBox ...
- 1101: 零起点学算法08——简单的输入和计算(a+b)
1101: 零起点学算法08--简单的输入和计算(a+b) Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lldSubmitt ...
- [Python] Spark平台下实现分布式AC自动机(一)
转载请注明出处:http://www.cnblogs.com/kirai/ 作者:Kirai 零.问题的提出 最近希望在分布式平台上实现一个AC自动机,但是如何在这样的分布式平台上表示这样的非线性数据 ...
- yii2 创建ActiveForm(表单)
表单的生成表单中的方法 ActiveForm::begin()方法 ActiveForm::end()方法 getClientOptions()方法 其它方法:errorSum ...
- golang kafka
golang kafka – hello world https://github.com/Shopify/sarama https://shopify.github.io/sarama/ con ...
- 读书笔记 effective c++ Item 51 实现new和delete的时候要遵守约定
Item 50中解释了在什么情况下你可能想实现自己版本的operator new和operator delete,但是没有解释当你实现的时候需要遵守的约定.遵守这些规则并不是很困难,但是它们其中有一些 ...
- 04(1) 基于上下文相关的GMM-HMM声学模型1
1.上下文对音素发音的语谱轨迹的影响 受到上下文的影响,同一个音素的发音语谱轨迹不同 为提高识别准确率,对音素建模时应将这种上下文影响考虑在内 2.基于上下文相关的音素建模 注意,非单音素建模中,每个 ...