[刷题]算法竞赛入门经典(第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 ...
随机推荐
- node.js 模板 ejs 转
node.js 模板引ejs. 搜了一把推荐用ejs的最多. 速度比对:http://www.cnblogs.com/fengmk2/archive/2011/04/28/2031971.html e ...
- 《InsideUE4》UObject(五)类型系统信息收集
在一起!在一起! 引言 前文中我们阐述了类型系统构建的第一个阶段:生成.UHT分析源码的宏标记并生成了包含程序元信息的代码,继而编译进程序,在程序启动的时候,开始启动类型系统的后续构建阶段.而本文我们 ...
- 开始学习yii2第一天
今天在朋友圈看到一条转发,内容是根据招聘网站的要求,列举了需要一个php工程师具体需要哪些技能 框架要求是yii2 出现的最多 已经出来工作了快半个月了,感觉工资还是少的可怜,而且我也好想去张江, 所 ...
- 创建keystone的catalog时提示:‘Internal Server Error (HTTP 500)’
在生成keystone的catalog时: [root@controller ~]# openstack service create --name keystone --description &q ...
- Java日期格式化方法
首先获取当前系统时间的方法有两种:第一种可以用currentTimeMillis()方法获取,它其实产生的是一个当前的毫秒数,这个毫秒是自1970年1月1日0时起至现在的毫秒数,类型是long 型,可 ...
- 老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具
老李分享:使用 Python 的 Socket 模块开发 UDP 扫描工具 poptest是业内唯一的测试开发工程师培训机构,测试开发工程师主要是为测试服务开发测试工具,在工作中要求你做网络级别的安全 ...
- windows下安装zabbix_agent
Server端在linux系统上,server端版本为2.2.6,是以前就装好的已经跑了很久的稳定版.目前的需求是要将新业务的服务器添加到该监控队列.而这些服务器是windows系统. 第一次下载了最 ...
- canvas画布
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 用Xamarin + VS 编写Android程序体验及其与Android Studio的比较
昨天看了微软2016Build大会,Xamarin免费了.恩,5亿美刀的家伙,哈哈,我也要体验一下..... 1. 首先在Xamarin官网下载安向导:https://www.xamarin.com/ ...
- JavaWeb总结(四)—JSP深入解析
一.JSP域对象 1.JSP属性范围(域对象范围) JSP提供了四个域对象,分别是pageContext.request.session.application. pageContext: 属性范围仅 ...