[刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600
来源:互联网 发布:windows自带的录音机 编辑:程序博客网 时间:2024/06/09 17:05
比较忙比较累,只贴代码了。
题目: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 2333333using 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;}
0 0
- [刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600
- 算法竞赛入门经典 第二版 习题6-5 巡逻机器人 Patrol Robot uva1600
- [刷题]算法竞赛入门经典(第2版) 5-6/UVa1595 - Symmetry
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
- 《算法竞赛入门经典(第2版)》
- [刷题]算法竞赛入门经典(第2版) 6-6/UVa12166 - Equilibrium Mobile
- [刷题]算法竞赛入门经典(第2版) 6-11/UVa10410 6-13/UVa215
- [刷题]算法竞赛入门经典(第2版) 6-7/UVa804 - Petri Net Simulation
- [刷题]算法竞赛入门经典(第2版) 6-8/UVa806 - Spatial Structures
- [刷题]算法竞赛入门经典(第2版) 6-9/UVa127 - "Accordian" Patience
- [刷题]算法竞赛入门经典(第2版) 6-14UVa12118
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
- [刷题]算法竞赛入门经典(第2版) 5-4/UVa10763 - Foreign Exchange
- 算法竞赛入门经典(第2版)习题5-6 对称轴(Symmetry) Uva1595
- [刷题]算法竞赛入门经典(第2版) 5-5/UVa10391 - Compound Words
- [刷题]算法竞赛入门经典(第2版) 4-4/UVa253 - Cube painting
- [刷题]算法竞赛入门经典(第2版) 5-2/UVa1594 - Ducci Sequence
- [刷题]算法竞赛入门经典(第2版) 4-2/UVa201 - Squares
- c++ list, vector, map, set 区别与用法比较
- 顺序表的静态存储
- 流程控制语句:循环语句-while
- volatile变量
- 20条书写 CSS 代码的建议
- [刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600
- sql索引应用
- Android之Activity的细枝末节(转载)
- java(20)------SimpleDateFormat日期格式转换详解
- 关于结构体字节对齐的一些感悟
- LaTex笔记(一)
- eclipse+scala+java+maven 整合实践
- 空指针异常解析
- Java - Collection——HashMap