The Martian Challenge 2017 E. Secret Passage
来源:互联网 发布:网络加速app 编辑:程序博客网 时间:2024/06/06 16:27
题目链接
题解:正常的迷宫走法,用bfs,dfs超时。对捷径前bfs,加上捷径后bfs 两个路径对比差距是否在5以内
代码:
#include <bits/stdc++.h>using namespace std;const int MAX_N = 510;const int MAX_M = 510;const int INF = 0x3f3f3f3f;typedef pair<int, int> P;char str[MAX_N];char maze[MAX_N][MAX_M + 1];int N, M, K;int sx = 0, sy = 0; //起点的位置int gx , gy; //终点的位置int d[MAX_N][MAX_M];//储存起点到某一点的距离int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; //表明每次x和y方向的位移void bfs(){queue<P> que;for (int i = 0; i < N; i++)for (int j = 0; j < M; j++)d[i][j] = INF;//初始化所有点的距离为INFque.push(P(sx, sy));d[sx][sy] = 0;//从起点出发将距离设为0,并放入队列首端while (que.size()) //题目保证有路到终点,所以不用担心死循环{P p = que.front(); que.pop();//弹出队首元素int i;for (i = 0; i < 4; i++){int nx = p.first + dx[i];int ny = p.second + dy[i];//移动后的坐标//判断可移动且没到过if (0 <= nx&&nx < N&& 0 <= ny&&ny < M&&maze[nx][ny] != 1&&d[nx][ny] == INF)//之前到过的话不用考虑,因为距离在队列中递增,肯定不会获得更好的解{que.push(P(nx, ny));//可以移动则设定距离为之前加一,放入队列d[nx][ny] = d[p.first][p.second] + 1;if(nx==gx && ny==gy) break; }}if(i!=4) break;}}int main(){cin>>N>>M>>K;gx =N-1 , gy = M- 1; //终点的位置 for(int i = 0; i < N ; i ++) { scanf("%s",str); for(int j = 0; j < strlen(str);j ++) { maze[i][j]=(str[j]=='x'?1:0); } }bfs();int imin1 = d[gx][gy];int a1,b1; for(int i = 0; i < K ;i ++) { scanf("%d %d",&a1,&b1); maze[a1][b1] = 0; } bfsint imin2 = d[gx][gy]; if(imin1 < imin2+5) printf("NO\n"); else printf("YES\n");return 0;}
0 0
- The Martian Challenge 2017 E. Secret Passage
- 贪心——The Martian Challenge 2017 #G. Pick Your Team
- The passage of time
- The Passage of Time
- The first English passage
- passage
- Codeforces 216E - Martian Luck
- Codeforces 149 E. Martian Strings
- codeforces 149E Martian Strings
- The Secret
- CF 149E Martian Strings(KMP)
- CF 149E Martian Strings(KMP)
- CF 77E Martian Food题解
- June Challenge 2017 | Chef and the Feast
- July Challenge 2017 | Whats in the Name
- Codeforces Round #106 (Div. 2) 149/E E. Martian Strings
- 【CF 235E】Number Challenge
- The Secret[秘密,读后感]
- 提交(Commit)
- Oracle 12c 新特性之 temp undo
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(十四)谈谈写博客的原因和项目优化
- 什么是面向对象思想
- 自己编写JZ2440 bootloader分析
- The Martian Challenge 2017 E. Secret Passage
- 记一个归并排序模板
- ceshi
- 500. Keyboard Row
- #webview简介与遇到的问题及解决方案##
- 51Nod 1011 最大公约数GCD
- Android MediaRecorder 实现录制无声视频 禁止录入声音
- 图像处理之_轮廓匹配
- dojo中的dojo/dom-class