[刷题]算法竞赛入门经典(第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 ...
随机推荐
- 牛顿迭代法求开根号。 a^1/2_______Xn+1=1/2*(Xn+a/Xn)
#include <stdio.h>#include <math.h>int main(void){ double a,x1=1.0,x2; printf("plea ...
- HTTP [TCP Retransmission] Continuation or non-HTTP traffic[Packet size limited during capture]
http://www.xianren.org/blog/net/wireshark-q.html 抓到的包数据中常见的错误..待细看,先记下. tcpdump 抓包后发现,出现大量标题字样显示,不利 ...
- MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...
下面是我update数据库时打印出来的异常: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...
- 【Flex】读取本地XML,然后XML数据转成JSON数据
干了一年H5,最近被要求写编辑器,Electron等级还不够,写不了,只有重新拿起as3,用flex,最近写到数据表编辑模块,有这部分功能,基本完成 . package utils { /** * 模 ...
- Java内存泄露实例
- D3.js-坐标轴生成测试SVG
Bottom top Bottom top // =0) return "translate("+margin+","+i*offset+")&q ...
- POI 3.8读取2003与2007格式EXCEL(xls、xlsx)
废话少说直接上代码,记得是poi3.8版本啊.方法入口唯一,自动判断格式,使用接口引用,自动选择执行方法. 方法入口: public static ArrayList<String[]> ...
- Web worker 与JS中异步编程的对比
0.从一道题说起 var t = true; setTimeout(function(){ t = false; }, 1000); while(t){ } alert('end'); 问,以上代码何 ...
- 【web】之 jquery上传插件的Plupload的使用
首先下载plupload->http://www.plupload.com 因为Plupload可配置参数比较多,所以这里讲解最常用的,结合jquery-ui展示的界面!如下: Plupload ...
- Hive 的简单使用及调优参考文档
Hive 的简单使用及调优参考文档 HIVE的使用 命令行界面 使用一下命令查看hive的命令行页面, hive --help --service cli 简化命令为hive –h 会输出下面的这 ...