无聊登了一下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[25][25]; 15 16 int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};//上下左右 17 int main() 18 { 19 // freopen("C:\Users\GerJCS岛\Desktop\t.txt", "r", stdin); 20 int width; 21 int height; 22 int flag[25][25]; 23 int step[25][25]; 24 char str[25][25]; 25 memset(flag, 0, sizeof(flag)); 26 memset(parent, -1, sizeof(parent)); 27 memset(step, -1, sizeof(step)); 28 memset(str, '.', sizeof(str)); 29 scanf("%d%d", &width, &height); 30 31 getchar(); 32 for(i, 1, height + 1){//>=1 <= height 33 for(j, 1, width + 1) 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(" "); 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, 0, switchCount) 58 scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState); 59 //printf("%d %d %d %d ", 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 ", Q.front().r, Q.front().c); 66 67 flag[startX][startY] = 1; 68 step[startX][startY] = 0;//不算初始 69 int place; 70 while(!Q.empty()){ 71 // printf("TEST "); 72 Node node = Q.front(); 73 Q.pop();//一开始忘了 74 int r = node.r; 75 int c = node.c; 76 for(i, 0, 4){ 77 int nowr = r + dir[i][0]; 78 int nowc = c + dir[i][1]; 79 // printf("%d%c%d ", nowr, str[nowr][nowc], nowc); 80 if(nowr >= 1 && nowc >= 1 && nowr <= width && nowc <= height 81 && !flag[nowr][nowc] && str[nowr][nowc] == '.'){ 82 //printf("TEST "); 83 flag[nowr][nowc] = 1; 84 step[nowr][nowc] = step[r][c] + 1; 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(1){ 101 if(parent[r][c].r == -1 && parent[r][c].c == -1) 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(" "); 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[25][25]; 15 16 int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};//上下左右 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[25][25]; 23 int step[25][25]; 24 char str[25][25]; 25 memset(flag, 0, sizeof(flag)); 26 memset(parent, -1, sizeof(parent)); 27 memset(step, -1, sizeof(step)); 28 memset(str, '.', sizeof(str)); 29 scanf("%d%d", &width, &height); 30 31 getchar(); 32 for(i, 1, height + 1){ 33 for(j, 1, width + 1) 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, 0, 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] = 1; 59 step[startX][startY] = 0;//不算初始 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, 0, 4){ 67 int nowr = r + dir[i][0]; 68 int nowc = c + dir[i][1]; 69 if(nowr >= 1 && nowc >= 1 && nowr <= width && nowc <= height 70 && !flag[nowr][nowc] && str[nowr][nowc] == '.'){ 71 flag[nowr][nowc] = 1; 72 step[nowr][nowc] = step[r][c] + 1; 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(1){ 89 if(parent[r][c].r == -1 && parent[r][c].c == -1) 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(" "); 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 8 8 2 ...#.... 3 ##.##.#. 4 .#..###. 5 .##.#... 6 ..#.###. 7 .##...#. 8 ..###.#. 9 ........ 10 1 1 8 8 0
在本地是可以正确输出结果的QAQ~ (黑框输出的是:RRDDRDDDRRDDRR)
一会去撸铁,先放着。。
*********************更新**********************
真神奇,这道题只承认最后的cout里面的东西貌似。cout就好了,可是最后ans在本地也是对的,Standard Output却是个 0?的乱码
1 char ans[100]; 2 int i = 0; 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(" ");
最后这么改了下还是不行。。。
问了下作者原来是地图搞错了,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[21][21]; 16 17 int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};//上下左右 18 char ans[100]; 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[21][21]; 25 int step[21][21]; 26 char str[21][21]; 27 memset(flag, 0, sizeof(flag)); 28 memset(parent, -1, sizeof(parent)); 29 memset(step, -1, sizeof(step)); 30 memset(str, '.', sizeof(str)); 31 scanf("%d%d", &width, &height); 32 33 getchar(); 34 for(i, 0, height ){ 35 for(j, 0, width ) 36 scanf("%c", &str[j][i]); 37 getchar(); 38 } 39 40 41 // for(i, 0, height ){ 42 // for(j, 0, width) 43 // printf("%c", str[j][i]); 44 // printf(" "); 45 // } 46 int startX; 47 int startY; 48 scanf("%d%d", &startX, &startY); 49 int targetX; 50 int targetY; 51 scanf("%d%d", &targetX, &targetY); 52 53 int switchCount; 54 scanf("%d", &switchCount); 55 int switchX; 56 int switchY; 57 int blockX; 58 int blockY; 59 int initialState; // 1 if blocking, 0 otherwise 60 for (i, 0, switchCount) 61 scanf("%d%d%d%d%d", &switchX, &switchY, &blockX, &blockY, &initialState); 62 63 // printf("%d %d %d %d ", startX, startY, targetX, targetY); 64 65 queue<Node> Q; 66 while(!Q.empty()) Q.pop(); 67 Q.push(Node(startX, startY)); 68 69 flag[startX][startY] = 1; 70 step[startX][startY] = 0;//不算初始 71 int place; 72 while(!Q.empty()){ 73 Node node = Q.front(); 74 Q.pop();//一开始忘了 75 int r = node.r; 76 int c = node.c; 77 for(i, 0, 4){ 78 int nowr = r + dir[i][0]; 79 int nowc = c + dir[i][1]; 80 if(nowr >= 0 && nowc >= 0 && nowr < width && nowc < height 81 && !flag[nowr][nowc] && str[nowr][nowc] == '.'){ 82 flag[nowr][nowc] = 1; 83 step[nowr][nowc] = step[r][c] + 1; 84 Q.push(Node(nowr, nowc)); 85 parent[nowr][nowc].r = r; 86 parent[nowr][nowc].c = c; 87 if(nowr == targetX && nowc == targetY){ 88 place = step[nowr][nowc]; 89 break; 90 } 91 } 92 } 93 } 94 95 stack<char> S;//不用清空嘛 96 int r = targetX; 97 int c = targetY; 98 int r_r; 99 while(1){ 100 if(parent[r][c].r == -1 && parent[r][c].c == -1) 101 break; 102 103 if(parent[r][c].r < r) 104 S.push('R'); 105 if(parent[r][c].r > r) 106 S.push('L'); 107 if(parent[r][c].c < c) 108 S.push('D'); 109 if(parent[r][c].c > c) 110 S.push('U'); 111 112 r_r = parent[r][c].r; 113 c = parent[r][c].c; 114 r = r_r;//好蠢QAQ~ 115 } 116 // r = targetX; 117 118 // memset(ans, '', sizeof(ans)); 119 int i = 0; 120 while(!S.empty()){ 121 ans[i++ ] = S.top(); 122 // printf("%c", ans[i ++]); 123 // cout << ans[i ++];//这两句都不行,不能单独输出,必须输出整个字符串,ans 124 S.pop(); 125 // printf("%c", pc); 126 } 127 cout <<ans<< endl; 128 // ans[i] = ' '; 129 // cout << "RRDDRDDDRRDDRRL" << endl; 130 // cout << ans << endl;//本地可以,为啥Standard有提示怪怪的东西。。> 0? 131 // printf(" "); 132 } 133 134 /* 135 第一组数据 136 8 8 137 ...#.... 138 ##.##.#. 139 .#..###. 140 .##.#... 141 ..#.###. 142 .##...#. 143 ..###.#. 144 ........ 145 1 1 8 8 0 146 147 148 149 10 10 150 ########## 151 #...#....# 152 ###.##.#.# 153 #.#..###.# 154 #.##.#...# 155 #..#.###.# 156 #.##...#.# 157 #..###.#.# 158 #........# 159 ########## 160 1 1 161 8 8 162 0 163 164 */