无聊登了一下condingame,通知说有本周谜题,正好刚撸完bfs,想尝试下。

题目链接:https://www.codingame.com/ide/17558874463b39b9ce6d420710807279bb1bd77e

题目大意:很有趣的题目,给你起始位置和终点位置,输出最短的路径,不过多了几个buff,垃圾球,开关,有害磁场

垃圾球:你可以移动(我没考虑这个东西,貌似用不到,因为后面说可以不动垃圾球)

开关:路过这个点,0/1状态变化一次

有害磁场:想通过磁场这个位置,必须把他关闭,方法是操作对应的开关

思路类似上一篇蓝桥杯迷宫还有poj3984

代码:

  1. #include<cstdio>
  2. #include <algorithm>
  3. #include<queue>
  4. #include<stack>
  5. #include<cstring>
  6. #define for(i, a, b) for(int i = a; i < b; i ++)//algorithm(要么不输入,输入必须在define前面,调换位置for的define会报错
  7.  
  8. using namespace std;
  9. struct Node{
  10. int r;
  11. int c;
  12. Node(int r, int c):r(r), c(c){}
  13. Node(){}
  14. }parent[][];
  15.  
  16. int dir[][] = {{, -}, {, }, {-, }, {, }};//上下左右
  17. int main()
  18. {
  19. // freopen("C:\\Users\\GerJCS岛\\Desktop\\t.txt", "r", stdin);
  20. int width;
  21. int height;
  22. int flag[][];
  23. int step[][];
  24. char str[][];
  25. memset(flag, , sizeof(flag));
  26. memset(parent, -, sizeof(parent));
  27. memset(step, -, sizeof(step));
  28. memset(str, '.', sizeof(str));
  29. scanf("%d%d", &width, &height);
  30.  
  31. getchar();
  32. for(i, , height + ){//>=1 <= height
  33. for(j, , width + )
  34. scanf("%c", &str[j][i]);
  35. getchar();
  36. }
  37. // for(i, 1, height + 1){//>=1 <= height
  38. // for(j, 1, width + 1)
  39. // printf("%c", str[j][i]);
  40. // printf("\n");
  41. // }
  42.  
  43. int startX;
  44. int startY;
  45. scanf("%d%d", &startX, &startY);
  46. int targetX;
  47. int targetY;
  48. scanf("%d%d", &targetX, &targetY);
  49.  
  50. int switchCount;
  51. scanf("%d", &switchCount);
  52. int switchX;
  53. int switchY;
  54. int blockX;
  55. int blockY;
  56. int initialState; // 1 if blocking, 0 otherwise
  57. for (i, , switchCount)
  58. scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState);
  59. //printf("%d %d %d %d\n", startX, startY, targetX, targetY);
  60.  
  61. queue<Node> Q;
  62. while(!Q.empty()) Q.pop();
  63. Q.push(Node(startX, startY));
  64.  
  65. // printf("%d %d\n", Q.front().r, Q.front().c);
  66.  
  67. flag[startX][startY] = ;
  68. step[startX][startY] = ;//不算初始
  69. int place;
  70. while(!Q.empty()){
  71. // printf("TEST\n");
  72. Node node = Q.front();
  73. Q.pop();//一开始忘了
  74. int r = node.r;
  75. int c = node.c;
  76. for(i, , ){
  77. int nowr = r + dir[i][];
  78. int nowc = c + dir[i][];
  79. // printf("%d%c%d\n", nowr, str[nowr][nowc], nowc);
  80. if(nowr >= && nowc >= && nowr <= width && nowc <= height
  81. && !flag[nowr][nowc] && str[nowr][nowc] == '.'){
  82. //printf("TEST\n");
  83. flag[nowr][nowc] = ;
  84. step[nowr][nowc] = step[r][c] + ;
  85. Q.push(Node(nowr, nowc));
  86. parent[nowr][nowc].r = r;
  87. parent[nowr][nowc].c = c;
  88. if(nowr == targetX && nowc == targetY){
  89. place = step[nowr][nowc];
  90. break;
  91. }
  92. }
  93. }
  94. }
  95.  
  96. stack<char> S;//不用清空嘛
  97. int r = width;
  98. int c = height;
  99. int r_r;
  100. while(){
  101. if(parent[r][c].r == - && parent[r][c].c == -)
  102. break;
  103.  
  104. if(parent[r][c].r < r)
  105. S.push('R');
  106. if(parent[r][c].r > r)
  107. S.push('L');
  108. if(parent[r][c].c < c)
  109. S.push('D');
  110. if(parent[r][c].c > c)
  111. S.push('U');
  112.  
  113. r_r = parent[r][c].r;
  114. c = parent[r][c].c;
  115. r = r_r;//好蠢QAQ~
  116. }
  117. r = targetX;
  118.  
  119. while(!S.empty()){
  120. char pc = S.top();
  121. S.pop();
  122. printf("%c", pc);
  123. }
  124. printf("\n");
  125. }
  126.  
  127. //PS:这codingame的xy是反过来的,好别扭QAQ~,
  128. //1,1 2,1
  129. //1,2 2,2
  130. //反手一想其实没什么影响,只是输出方向步伐,而不是坐标,就算是坐标也可以最后统一反向处理+增1操作
  131. //打算把所有xy都调过来输入,在减一操作的
  132. //想想还是算了,按题目来吧,不然不好调试,好反人类,,,
  133.  
  134. //提示总线错误(莫名其妙没了。。。。)
  135.  
  136. //写好后又是Standard Output Stream 什么都没有
  137.  
  138. /*
  139. 8 8
  140. ...#....
  141. ##.##.#.
  142. .#..###.
  143. .##.#...
  144. ..#.###.
  145. .##...#.
  146. ..###.#.
  147. ........
  148. 1 1
  149. 8 8
  150. 0
  151. */

去注释代码:

  1. #include<cstdio>
  2. #include <algorithm>
  3. #include<queue>
  4. #include<stack>
  5. #include<cstring>
  6. #define for(i, a, b) for(int i = a; i < b; i ++)
  7.  
  8. using namespace std;
  9. struct Node{
  10. int r;
  11. int c;
  12. Node(int r, int c):r(r), c(c){}
  13. Node(){}
  14. }parent[][];
  15.  
  16. int dir[][] = {{, -}, {, }, {-, }, {, }};//上下左右
  17. int main()
  18. {
  19. freopen("C:\\Users\\GerJCS岛\\Desktop\\t.txt", "r", stdin);//这句话不注释会buss error
  20. int width;
  21. int height;
  22. int flag[][];
  23. int step[][];
  24. char str[][];
  25. memset(flag, , sizeof(flag));
  26. memset(parent, -, sizeof(parent));
  27. memset(step, -, sizeof(step));
  28. memset(str, '.', sizeof(str));
  29. scanf("%d%d", &width, &height);
  30.  
  31. getchar();
  32. for(i, , height + ){
  33. for(j, , width + )
  34. scanf("%c", &str[j][i]);
  35. getchar();
  36. }
  37. int startX;
  38. int startY;
  39. scanf("%d%d", &startX, &startY);
  40. int targetX;
  41. int targetY;
  42. scanf("%d%d", &targetX, &targetY);
  43.  
  44. int switchCount;
  45. scanf("%d", &switchCount);
  46. int switchX;
  47. int switchY;
  48. int blockX;
  49. int blockY;
  50. int initialState; // 1 if blocking, 0 otherwise
  51. for (i, , switchCount)
  52. scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState);
  53.  
  54. queue<Node> Q;
  55. while(!Q.empty()) Q.pop();
  56. Q.push(Node(startX, startY));
  57.  
  58. flag[startX][startY] = ;
  59. step[startX][startY] = ;//不算初始
  60. int place;
  61. while(!Q.empty()){
  62. Node node = Q.front();
  63. Q.pop();//一开始忘了
  64. int r = node.r;
  65. int c = node.c;
  66. for(i, , ){
  67. int nowr = r + dir[i][];
  68. int nowc = c + dir[i][];
  69. if(nowr >= && nowc >= && nowr <= width && nowc <= height
  70. && !flag[nowr][nowc] && str[nowr][nowc] == '.'){
  71. flag[nowr][nowc] = ;
  72. step[nowr][nowc] = step[r][c] + ;
  73. Q.push(Node(nowr, nowc));
  74. parent[nowr][nowc].r = r;
  75. parent[nowr][nowc].c = c;
  76. if(nowr == targetX && nowc == targetY){
  77. place = step[nowr][nowc];
  78. break;
  79. }
  80. }
  81. }
  82. }
  83.  
  84. stack<char> S;//不用清空嘛
  85. int r = width;
  86. int c = height;
  87. int r_r;
  88. while(){
  89. if(parent[r][c].r == - && parent[r][c].c == -)
  90. break;
  91.  
  92. if(parent[r][c].r < r)
  93. S.push('R');
  94. if(parent[r][c].r > r)
  95. S.push('L');
  96. if(parent[r][c].c < c)
  97. S.push('D');
  98. if(parent[r][c].c > c)
  99. S.push('U');
  100.  
  101. r_r = parent[r][c].r;
  102. c = parent[r][c].c;
  103. r = r_r;//好蠢QAQ~
  104. }
  105. r = targetX;
  106.  
  107. while(!S.empty()){
  108. char pc = S.top();
  109. S.pop();
  110. printf("%c", pc);
  111. }
  112. printf("\n");
  113. }
  114.  
  115. /*
  116. 第一组数据
  117. 8 8
  118. ...#....
  119. ##.##.#.
  120. .#..###.
  121. .##.#...
  122. ..#.###.
  123. .##...#.
  124. ..###.#.
  125. ........
  126. 1 1 8 8 0
  127. */

PS:抱怨一下这个从1开始和xy反转这是反人类。。。

我第一次提示bus error莫名其妙就调没了,可能是第一次设置str为int类型

但是现在问题是跑1/30点的时候,平台的Standard Output Stream 什么都没有QAQ~,我输入题目1/30这组数据

  1. ...#....
  2. ##.##.#.
  3. .#..###.
  4. .##.#...
  5. ..#.###.
  6. .##...#.
  7. ..###.#.
  8. ........

在本地是可以正确输出结果的QAQ~             (黑框输出的是:RRDDRDDDRRDDRR)

一会去撸铁,先放着。。

*********************更新**********************

真神奇,这道题只承认最后的cout里面的东西貌似。cout就好了,可是最后ans在本地也是对的,Standard Output却是个 0?的乱码

  1. char ans[];
  2. int i = ;
  3. while(!S.empty()){
  4. ans[i ++] = S.top();
  5. S.pop();
  6. }
  7. // cout << "RRDDRDDDRRDDRRL" << endl;
  8. cout << ans << endl;//本地可以,为啥Standard有提示怪怪的东西。。> 0?
  9. // printf("\n");

最后这么改了下还是不行。。。

问了下作者原来是地图搞错了,https://www.codingame.com/forum/t/community-puzzle-bender-episode-4/84756/21(头像为山崎县人,ID为GerJCS的是我)

改下就好了,过了1/30数据,原来是从(0,0)开始的

代码:

  1. #include<cstdio>
  2. #include <algorithm>
  3. #include<queue>
  4. #include<iostream>
  5. #include<stack>
  6. #include<cstring>
  7. #define for(i, a, b) for(int i = a; i < b; i ++)
  8.  
  9. using namespace std;
  10. struct Node{
  11. int r;
  12. int c;
  13. Node(int r, int c):r(r), c(c){}
  14. Node(){}
  15. }parent[][];
  16.  
  17. int dir[][] = {{, -}, {, }, {-, }, {, }};//上下左右
  18. char ans[];
  19. int main()
  20. {
  21. // freopen("C:\\Users\\GerJCS岛\\Desktop\\t.txt", "r", stdin);//这句话不注释会buss error
  22. int width;
  23. int height;
  24. int flag[][];
  25. int step[][];
  26. char str[][];
  27. memset(flag, , sizeof(flag));
  28. memset(parent, -, sizeof(parent));
  29. memset(step, -, sizeof(step));
  30. memset(str, '.', sizeof(str));
  31. scanf("%d%d", &width, &height);
  32.  
  33. getchar();
  34. for(i, , height ){
  35. for(j, , width )
  36. scanf("%c", &str[j][i]);
  37. getchar();
  38. }
  39.  
  40. // for(i, 0, height ){
  41. // for(j, 0, width)
  42. // printf("%c", str[j][i]);
  43. // printf("\n");
  44. // }
  45. int startX;
  46. int startY;
  47. scanf("%d%d", &startX, &startY);
  48. int targetX;
  49. int targetY;
  50. scanf("%d%d", &targetX, &targetY);
  51.  
  52. int switchCount;
  53. scanf("%d", &switchCount);
  54. int switchX;
  55. int switchY;
  56. int blockX;
  57. int blockY;
  58. int initialState; // 1 if blocking, 0 otherwise
  59. for (i, , switchCount)
  60. scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState);
  61.  
  62. // printf("%d %d %d %d\n", startX, startY, targetX, targetY);
  63.  
  64. queue<Node> Q;
  65. while(!Q.empty()) Q.pop();
  66. Q.push(Node(startX, startY));
  67.  
  68. flag[startX][startY] = ;
  69. step[startX][startY] = ;//不算初始
  70. int place;
  71. while(!Q.empty()){
  72. Node node = Q.front();
  73. Q.pop();//一开始忘了
  74. int r = node.r;
  75. int c = node.c;
  76. for(i, , ){
  77. int nowr = r + dir[i][];
  78. int nowc = c + dir[i][];
  79. if(nowr >= && nowc >= && nowr < width && nowc < height
  80. && !flag[nowr][nowc] && str[nowr][nowc] == '.'){
  81. flag[nowr][nowc] = ;
  82. step[nowr][nowc] = step[r][c] + ;
  83. Q.push(Node(nowr, nowc));
  84. parent[nowr][nowc].r = r;
  85. parent[nowr][nowc].c = c;
  86. if(nowr == targetX && nowc == targetY){
  87. place = step[nowr][nowc];
  88. break;
  89. }
  90. }
  91. }
  92. }
  93.  
  94. stack<char> S;//不用清空嘛
  95. int r = targetX;
  96. int c = targetY;
  97. int r_r;
  98. while(){
  99. if(parent[r][c].r == - && parent[r][c].c == -)
  100. break;
  101.  
  102. if(parent[r][c].r < r)
  103. S.push('R');
  104. if(parent[r][c].r > r)
  105. S.push('L');
  106. if(parent[r][c].c < c)
  107. S.push('D');
  108. if(parent[r][c].c > c)
  109. S.push('U');
  110.  
  111. r_r = parent[r][c].r;
  112. c = parent[r][c].c;
  113. r = r_r;//好蠢QAQ~
  114. }
  115. // r = targetX;
  116.  
  117. // memset(ans, '', sizeof(ans));
  118. int i = ;
  119. while(!S.empty()){
  120. ans[i++ ] = S.top();
  121. // printf("%c", ans[i ++]);
  122. // cout << ans[i ++];//这两句都不行,不能单独输出,必须输出整个字符串,ans
  123. S.pop();
  124. // printf("%c", pc);
  125. }
  126. cout <<ans<< endl;
  127. // ans[i] = '\0';
  128. // cout << "RRDDRDDDRRDDRRL" << endl;
  129. // cout << ans << endl;//本地可以,为啥Standard有提示怪怪的东西。。> 0?
  130. // printf("\n");
  131. }
  132.  
  133. /*
  134. 第一组数据
  135. 8 8
  136. ...#....
  137. ##.##.#.
  138. .#..###.
  139. .##.#...
  140. ..#.###.
  141. .##...#.
  142. ..###.#.
  143. ........
  144. 1 1 8 8 0
  145.  
  146. 10 10
  147. ##########
  148. #...#....#
  149. ###.##.#.#
  150. #.#..###.#
  151. #.##.#...#
  152. #..#.###.#
  153. #.##...#.#
  154. #..###.#.#
  155. #........#
  156. ##########
  157. 1 1
  158. 8 8
  159. 0
  160.  
  161. */

codingame的更多相关文章

  1. c++17 代码你能看懂吗?

    ------------------------------------------------------------------------------ #include <vector&g ...

  2. F#周报2019年第1期

    新闻 介绍versionsof.net InfoQ正在寻找F#社区的声音 使用F#开发端对端的实际应用 UnoPlatform上的F# Elmish 视频及幻灯片 事件溯源DIY02--事件,事件存储 ...

  3. Unity3D与C#网站收藏

    siki学院(目前学习ing) http://www.sikiedu.com/ 雨松MOMO研究院 http://www.xuanyusong.com/ 知乎:Unity 开发教程相关回答(初步了解下 ...

  4. Awesome Go精选的Go框架,库和软件的精选清单.A curated list of awesome Go frameworks, libraries and software

    Awesome Go      financial support to Awesome Go A curated list of awesome Go frameworks, libraries a ...

  5. coding game, 边打游戏边学编程,是一种怎么样的体验?

    前言 hello,大家好,我是bigsai,好久不见,甚是想念! 在日常生活中,很多人喜欢玩游戏,因为游戏中有着对抗博弈.控制的喜悦,用灵魂指法完成一波靓丽的操作. 但实际上,你的按键都是对应代码中一 ...

  6. 学习java知道这五个网站就够了

    "这个国家的每个人都应该学习编程计算机,因为它教你如何思考." 当乔布斯几年前这么说时,他再次被证明是一个真正的有远见的人. 好吧,这很难反驳!如今,编程比以往任何时候都更加蓬勃发 ...

随机推荐

  1. 详解 方法的覆盖 —— toString() 与 equals()的覆盖

    在学习本篇博文前,建议先学习完本人的博文--<详解 继承(上)-- 工具的抽象与分层> 在本人之前的博文中曾讲过"基类"的知识,那么,本篇博文中的主题--Object类 ...

  2. 【题解】P1291 百事世界杯之旅 - 期望dp

    P1291 [SHOI2002]百事世界杯之旅 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 "--在 \ ...

  3. XSS语义分析的阶段性总结(一)

    本文作者:Kale 前言 由于X3Scan的研发已经有些进展了,所以对这一阶段的工作做一下总结!对于X3Scan的定位,我更加倾向于主动+被动的结合.主动的方面主要体现在可以主动抓取页面链接并发起请求 ...

  4. Java集合:ArrayList (JDK1.8 源码解读)

    ArrayList ArrayList几乎是每个java开发者最常用也是最熟悉的集合,看到ArrayList这个名字就知道,它必然是以数组方式实现的集合 关注点 说一下ArrayList的几个特点,也 ...

  5. Content-Type 四种常见的 POST 提交数据方式

    参考于: https://blog.csdn.net/tycoon1988/article/details/40080691(了解) 和: https://www.gy0929.com/wz/1420 ...

  6. thinkphp5.0 配置文件加载路径说明

    在thinphp5.0框架里,js,css等配置文件都是加载在/public/static的目录下,所以要引用这些文件,路径必须是要写好的,代码如图: return [ // 默认模块名 'defau ...

  7. mac OS 配置 svn服务器端

    在Windows环境下 一般使用Tortoise SVN来搭建svn环境 操作系统 mac OS High Sierra 10.13.6 在Mac环境下 由于Mac自带了svn的服务器端和客户端功能 ...

  8. Intellij-IDEA-maven+springMVC+mybatis整合

    2019独角兽企业重金招聘Python工程师标准>>> GitHub地址 https://github.com/Ethel731/WebProjectDemo 前言 之前都是在已经建 ...

  9. mysql查询语句中like 的用法

    1.常见用法: (1)搭配%使用 %代表一个或多个字符的通配符,譬如查询字段name中以大开头的数据: (2)搭配_使用 _代表仅仅一个字符的通配符,把上面那条查询语句中的%改为_,会发现只能查询出一 ...

  10. nodejs操作MySQL,mysql连接池及事务的使用

    https://blog.csdn.net/jasnet_u/article/details/88605168