codingame
无聊登了一下condingame,通知说有本周谜题,正好刚撸完bfs,想尝试下。
题目链接:https://www.codingame.com/ide/17558874463b39b9ce6d420710807279bb1bd77e
题目大意:很有趣的题目,给你起始位置和终点位置,输出最短的路径,不过多了几个buff,垃圾球,开关,有害磁场
垃圾球:你可以移动(我没考虑这个东西,貌似用不到,因为后面说可以不动垃圾球)
开关:路过这个点,0/1状态变化一次
有害磁场:想通过磁场这个位置,必须把他关闭,方法是操作对应的开关
思路类似上一篇蓝桥杯迷宫还有poj3984
代码:
- #include<cstdio>
- #include <algorithm>
- #include<queue>
- #include<stack>
- #include<cstring>
- #define for(i, a, b) for(int i = a; i < b; i ++)//algorithm(要么不输入,输入必须在define前面,调换位置for的define会报错
- using namespace std;
- struct Node{
- int r;
- int c;
- Node(int r, int c):r(r), c(c){}
- Node(){}
- }parent[][];
- int dir[][] = {{, -}, {, }, {-, }, {, }};//上下左右
- int main()
- {
- // freopen("C:\\Users\\GerJCS岛\\Desktop\\t.txt", "r", stdin);
- int width;
- int height;
- int flag[][];
- int step[][];
- char str[][];
- memset(flag, , sizeof(flag));
- memset(parent, -, sizeof(parent));
- memset(step, -, sizeof(step));
- memset(str, '.', sizeof(str));
- scanf("%d%d", &width, &height);
- getchar();
- for(i, , height + ){//>=1 <= height
- for(j, , width + )
- scanf("%c", &str[j][i]);
- getchar();
- }
- // for(i, 1, height + 1){//>=1 <= height
- // for(j, 1, width + 1)
- // printf("%c", str[j][i]);
- // printf("\n");
- // }
- int startX;
- int startY;
- scanf("%d%d", &startX, &startY);
- int targetX;
- int targetY;
- scanf("%d%d", &targetX, &targetY);
- int switchCount;
- scanf("%d", &switchCount);
- int switchX;
- int switchY;
- int blockX;
- int blockY;
- int initialState; // 1 if blocking, 0 otherwise
- for (i, , switchCount)
- scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState);
- //printf("%d %d %d %d\n", startX, startY, targetX, targetY);
- queue<Node> Q;
- while(!Q.empty()) Q.pop();
- Q.push(Node(startX, startY));
- // printf("%d %d\n", Q.front().r, Q.front().c);
- flag[startX][startY] = ;
- step[startX][startY] = ;//不算初始
- int place;
- while(!Q.empty()){
- // printf("TEST\n");
- Node node = Q.front();
- Q.pop();//一开始忘了
- int r = node.r;
- int c = node.c;
- for(i, , ){
- int nowr = r + dir[i][];
- int nowc = c + dir[i][];
- // printf("%d%c%d\n", nowr, str[nowr][nowc], nowc);
- if(nowr >= && nowc >= && nowr <= width && nowc <= height
- && !flag[nowr][nowc] && str[nowr][nowc] == '.'){
- //printf("TEST\n");
- flag[nowr][nowc] = ;
- step[nowr][nowc] = step[r][c] + ;
- Q.push(Node(nowr, nowc));
- parent[nowr][nowc].r = r;
- parent[nowr][nowc].c = c;
- if(nowr == targetX && nowc == targetY){
- place = step[nowr][nowc];
- break;
- }
- }
- }
- }
- stack<char> S;//不用清空嘛
- int r = width;
- int c = height;
- int r_r;
- while(){
- if(parent[r][c].r == - && parent[r][c].c == -)
- break;
- if(parent[r][c].r < r)
- S.push('R');
- if(parent[r][c].r > r)
- S.push('L');
- if(parent[r][c].c < c)
- S.push('D');
- if(parent[r][c].c > c)
- S.push('U');
- r_r = parent[r][c].r;
- c = parent[r][c].c;
- r = r_r;//好蠢QAQ~
- }
- r = targetX;
- while(!S.empty()){
- char pc = S.top();
- S.pop();
- printf("%c", pc);
- }
- printf("\n");
- }
- //PS:这codingame的xy是反过来的,好别扭QAQ~,
- //1,1 2,1
- //1,2 2,2
- //反手一想其实没什么影响,只是输出方向步伐,而不是坐标,就算是坐标也可以最后统一反向处理+增1操作
- //打算把所有xy都调过来输入,在减一操作的
- //想想还是算了,按题目来吧,不然不好调试,好反人类,,,
- //提示总线错误(莫名其妙没了。。。。)
- //写好后又是Standard Output Stream 什么都没有
- /*
- 8 8
- ...#....
- ##.##.#.
- .#..###.
- .##.#...
- ..#.###.
- .##...#.
- ..###.#.
- ........
- 1 1
- 8 8
- 0
- */
去注释代码:
- #include<cstdio>
- #include <algorithm>
- #include<queue>
- #include<stack>
- #include<cstring>
- #define for(i, a, b) for(int i = a; i < b; i ++)
- using namespace std;
- struct Node{
- int r;
- int c;
- Node(int r, int c):r(r), c(c){}
- Node(){}
- }parent[][];
- int dir[][] = {{, -}, {, }, {-, }, {, }};//上下左右
- int main()
- {
- freopen("C:\\Users\\GerJCS岛\\Desktop\\t.txt", "r", stdin);//这句话不注释会buss error
- int width;
- int height;
- int flag[][];
- int step[][];
- char str[][];
- memset(flag, , sizeof(flag));
- memset(parent, -, sizeof(parent));
- memset(step, -, sizeof(step));
- memset(str, '.', sizeof(str));
- scanf("%d%d", &width, &height);
- getchar();
- for(i, , height + ){
- for(j, , width + )
- scanf("%c", &str[j][i]);
- getchar();
- }
- int startX;
- int startY;
- scanf("%d%d", &startX, &startY);
- int targetX;
- int targetY;
- scanf("%d%d", &targetX, &targetY);
- int switchCount;
- scanf("%d", &switchCount);
- int switchX;
- int switchY;
- int blockX;
- int blockY;
- int initialState; // 1 if blocking, 0 otherwise
- for (i, , switchCount)
- scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState);
- queue<Node> Q;
- while(!Q.empty()) Q.pop();
- Q.push(Node(startX, startY));
- flag[startX][startY] = ;
- step[startX][startY] = ;//不算初始
- int place;
- while(!Q.empty()){
- Node node = Q.front();
- Q.pop();//一开始忘了
- int r = node.r;
- int c = node.c;
- for(i, , ){
- int nowr = r + dir[i][];
- int nowc = c + dir[i][];
- if(nowr >= && nowc >= && nowr <= width && nowc <= height
- && !flag[nowr][nowc] && str[nowr][nowc] == '.'){
- flag[nowr][nowc] = ;
- step[nowr][nowc] = step[r][c] + ;
- Q.push(Node(nowr, nowc));
- parent[nowr][nowc].r = r;
- parent[nowr][nowc].c = c;
- if(nowr == targetX && nowc == targetY){
- place = step[nowr][nowc];
- break;
- }
- }
- }
- }
- stack<char> S;//不用清空嘛
- int r = width;
- int c = height;
- int r_r;
- while(){
- if(parent[r][c].r == - && parent[r][c].c == -)
- break;
- if(parent[r][c].r < r)
- S.push('R');
- if(parent[r][c].r > r)
- S.push('L');
- if(parent[r][c].c < c)
- S.push('D');
- if(parent[r][c].c > c)
- S.push('U');
- r_r = parent[r][c].r;
- c = parent[r][c].c;
- r = r_r;//好蠢QAQ~
- }
- r = targetX;
- while(!S.empty()){
- char pc = S.top();
- S.pop();
- printf("%c", pc);
- }
- printf("\n");
- }
- /*
- 第一组数据
- 8 8
- ...#....
- ##.##.#.
- .#..###.
- .##.#...
- ..#.###.
- .##...#.
- ..###.#.
- ........
- 1 1 8 8 0
- */
PS:抱怨一下这个从1开始和xy反转这是反人类。。。
我第一次提示bus error莫名其妙就调没了,可能是第一次设置str为int类型
但是现在问题是跑1/30点的时候,平台的Standard Output Stream 什么都没有QAQ~,我输入题目1/30这组数据
- ...#....
- ##.##.#.
- .#..###.
- .##.#...
- ..#.###.
- .##...#.
- ..###.#.
- ........
在本地是可以正确输出结果的QAQ~ (黑框输出的是:RRDDRDDDRRDDRR)
一会去撸铁,先放着。。
*********************更新**********************
真神奇,这道题只承认最后的cout里面的东西貌似。cout就好了,可是最后ans在本地也是对的,Standard Output却是个 0?的乱码
- char ans[];
- int i = ;
- while(!S.empty()){
- ans[i ++] = S.top();
- S.pop();
- }
- // cout << "RRDDRDDDRRDDRRL" << endl;
- cout << ans << endl;//本地可以,为啥Standard有提示怪怪的东西。。> 0?
- // printf("\n");
最后这么改了下还是不行。。。
问了下作者原来是地图搞错了,https://www.codingame.com/forum/t/community-puzzle-bender-episode-4/84756/21(头像为山崎县人,ID为GerJCS的是我)
改下就好了,过了1/30数据,原来是从(0,0)开始的
代码:
- #include<cstdio>
- #include <algorithm>
- #include<queue>
- #include<iostream>
- #include<stack>
- #include<cstring>
- #define for(i, a, b) for(int i = a; i < b; i ++)
- using namespace std;
- struct Node{
- int r;
- int c;
- Node(int r, int c):r(r), c(c){}
- Node(){}
- }parent[][];
- int dir[][] = {{, -}, {, }, {-, }, {, }};//上下左右
- char ans[];
- int main()
- {
- // freopen("C:\\Users\\GerJCS岛\\Desktop\\t.txt", "r", stdin);//这句话不注释会buss error
- int width;
- int height;
- int flag[][];
- int step[][];
- char str[][];
- memset(flag, , sizeof(flag));
- memset(parent, -, sizeof(parent));
- memset(step, -, sizeof(step));
- memset(str, '.', sizeof(str));
- scanf("%d%d", &width, &height);
- getchar();
- for(i, , height ){
- for(j, , width )
- scanf("%c", &str[j][i]);
- getchar();
- }
- // for(i, 0, height ){
- // for(j, 0, width)
- // printf("%c", str[j][i]);
- // printf("\n");
- // }
- int startX;
- int startY;
- scanf("%d%d", &startX, &startY);
- int targetX;
- int targetY;
- scanf("%d%d", &targetX, &targetY);
- int switchCount;
- scanf("%d", &switchCount);
- int switchX;
- int switchY;
- int blockX;
- int blockY;
- int initialState; // 1 if blocking, 0 otherwise
- for (i, , switchCount)
- scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState);
- // printf("%d %d %d %d\n", startX, startY, targetX, targetY);
- queue<Node> Q;
- while(!Q.empty()) Q.pop();
- Q.push(Node(startX, startY));
- flag[startX][startY] = ;
- step[startX][startY] = ;//不算初始
- int place;
- while(!Q.empty()){
- Node node = Q.front();
- Q.pop();//一开始忘了
- int r = node.r;
- int c = node.c;
- for(i, , ){
- int nowr = r + dir[i][];
- int nowc = c + dir[i][];
- if(nowr >= && nowc >= && nowr < width && nowc < height
- && !flag[nowr][nowc] && str[nowr][nowc] == '.'){
- flag[nowr][nowc] = ;
- step[nowr][nowc] = step[r][c] + ;
- Q.push(Node(nowr, nowc));
- parent[nowr][nowc].r = r;
- parent[nowr][nowc].c = c;
- if(nowr == targetX && nowc == targetY){
- place = step[nowr][nowc];
- break;
- }
- }
- }
- }
- stack<char> S;//不用清空嘛
- int r = targetX;
- int c = targetY;
- int r_r;
- while(){
- if(parent[r][c].r == - && parent[r][c].c == -)
- break;
- if(parent[r][c].r < r)
- S.push('R');
- if(parent[r][c].r > r)
- S.push('L');
- if(parent[r][c].c < c)
- S.push('D');
- if(parent[r][c].c > c)
- S.push('U');
- r_r = parent[r][c].r;
- c = parent[r][c].c;
- r = r_r;//好蠢QAQ~
- }
- // r = targetX;
- // memset(ans, '', sizeof(ans));
- int i = ;
- while(!S.empty()){
- ans[i++ ] = S.top();
- // printf("%c", ans[i ++]);
- // cout << ans[i ++];//这两句都不行,不能单独输出,必须输出整个字符串,ans
- S.pop();
- // printf("%c", pc);
- }
- cout <<ans<< endl;
- // ans[i] = '\0';
- // cout << "RRDDRDDDRRDDRRL" << endl;
- // cout << ans << endl;//本地可以,为啥Standard有提示怪怪的东西。。> 0?
- // printf("\n");
- }
- /*
- 第一组数据
- 8 8
- ...#....
- ##.##.#.
- .#..###.
- .##.#...
- ..#.###.
- .##...#.
- ..###.#.
- ........
- 1 1 8 8 0
- 10 10
- ##########
- #...#....#
- ###.##.#.#
- #.#..###.#
- #.##.#...#
- #..#.###.#
- #.##...#.#
- #..###.#.#
- #........#
- ##########
- 1 1
- 8 8
- 0
- */
codingame的更多相关文章
- c++17 代码你能看懂吗?
------------------------------------------------------------------------------ #include <vector&g ...
- F#周报2019年第1期
新闻 介绍versionsof.net InfoQ正在寻找F#社区的声音 使用F#开发端对端的实际应用 UnoPlatform上的F# Elmish 视频及幻灯片 事件溯源DIY02--事件,事件存储 ...
- Unity3D与C#网站收藏
siki学院(目前学习ing) http://www.sikiedu.com/ 雨松MOMO研究院 http://www.xuanyusong.com/ 知乎:Unity 开发教程相关回答(初步了解下 ...
- 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 ...
- coding game, 边打游戏边学编程,是一种怎么样的体验?
前言 hello,大家好,我是bigsai,好久不见,甚是想念! 在日常生活中,很多人喜欢玩游戏,因为游戏中有着对抗博弈.控制的喜悦,用灵魂指法完成一波靓丽的操作. 但实际上,你的按键都是对应代码中一 ...
- 学习java知道这五个网站就够了
"这个国家的每个人都应该学习编程计算机,因为它教你如何思考." 当乔布斯几年前这么说时,他再次被证明是一个真正的有远见的人. 好吧,这很难反驳!如今,编程比以往任何时候都更加蓬勃发 ...
随机推荐
- 详解 方法的覆盖 —— toString() 与 equals()的覆盖
在学习本篇博文前,建议先学习完本人的博文--<详解 继承(上)-- 工具的抽象与分层> 在本人之前的博文中曾讲过"基类"的知识,那么,本篇博文中的主题--Object类 ...
- 【题解】P1291 百事世界杯之旅 - 期望dp
P1291 [SHOI2002]百事世界杯之旅 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 "--在 \ ...
- XSS语义分析的阶段性总结(一)
本文作者:Kale 前言 由于X3Scan的研发已经有些进展了,所以对这一阶段的工作做一下总结!对于X3Scan的定位,我更加倾向于主动+被动的结合.主动的方面主要体现在可以主动抓取页面链接并发起请求 ...
- Java集合:ArrayList (JDK1.8 源码解读)
ArrayList ArrayList几乎是每个java开发者最常用也是最熟悉的集合,看到ArrayList这个名字就知道,它必然是以数组方式实现的集合 关注点 说一下ArrayList的几个特点,也 ...
- Content-Type 四种常见的 POST 提交数据方式
参考于: https://blog.csdn.net/tycoon1988/article/details/40080691(了解) 和: https://www.gy0929.com/wz/1420 ...
- thinkphp5.0 配置文件加载路径说明
在thinphp5.0框架里,js,css等配置文件都是加载在/public/static的目录下,所以要引用这些文件,路径必须是要写好的,代码如图: return [ // 默认模块名 'defau ...
- mac OS 配置 svn服务器端
在Windows环境下 一般使用Tortoise SVN来搭建svn环境 操作系统 mac OS High Sierra 10.13.6 在Mac环境下 由于Mac自带了svn的服务器端和客户端功能 ...
- Intellij-IDEA-maven+springMVC+mybatis整合
2019独角兽企业重金招聘Python工程师标准>>> GitHub地址 https://github.com/Ethel731/WebProjectDemo 前言 之前都是在已经建 ...
- mysql查询语句中like 的用法
1.常见用法: (1)搭配%使用 %代表一个或多个字符的通配符,譬如查询字段name中以大开头的数据: (2)搭配_使用 _代表仅仅一个字符的通配符,把上面那条查询语句中的%改为_,会发现只能查询出一 ...
- nodejs操作MySQL,mysql连接池及事务的使用
https://blog.csdn.net/jasnet_u/article/details/88605168