Codeforces 877D
来源:互联网 发布:淘宝华佗大药房怎么样 编辑:程序博客网 时间:2024/05/14 09:23
D. Olya and Energy Drinks
time limit per test 2 seconds
memory limit per test 256 megabytes
Olya loves energy drinks. She loves them so much that her roomis full of empty cans from energy drinks.
Formally, her room can be represented as a field ofn × m cells, each cell of which is empty or littered with cans.
Olya drank a lot of energy drink, so now she can runk meters per second. Each second shechooses one of the four directions (up, down, left or right) and runs from1 to k meters in this direction. Of course,she can only run through empty cells.
Now Olya needs to get from cell(x1, y1) to cell (x2, y2). How many seconds will it take her ifshe moves optimally?
It's guaranteed that cells(x1, y1) and (x2, y2) are empty. These cells can coincide.
Input
The first line contains three integersn,m and k (1 ≤ n, m, k ≤ 1000) —the sizes of the room and Olya's speed.
Thenn lines follow containing m characters each, the i-th of them contains on j-th position "#", if the cell (i, j) is littered withcans, and "."otherwise.
The last line contains four integersx1, y1, x2, y2 (1 ≤ x1, x2 ≤ n,1 ≤ y1, y2 ≤ m) — the coordinates of the first and the last cells.
Output
Print a single integer — the minimum time it will take Olya toget from (x1, y1) to (x2, y2).
If it's impossible to get from(x1, y1) to (x2, y2), print -1.
Examples
Input
3 4 4
....
###.
....
1 1 3 1
Output
3
Input
3 4 1
....
###.
....
1 1 3 1
Output
8
Input
2 2 1
.#
#.
1 1 2 2
Output
-1
Note
In the first sample Olya should run3 meters to the right in the firstsecond, 2meters down in the second second and 3 meters to the left in the thirdsecond.
In second sample Olya should run to the right for3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad.
【题意】
给出一张n*m的地图,给定起点和终点,一个人从起点出发,每一秒内能向一个方向走1~k中的任意步数,问从起点到终点最少需要花费的时间,如果不能到达,则输出-1。
【思路】
这道题跟简单的走迷宫唯一不一样的就是每一秒可以走多步,那么我们只要去枚举每个方向及在该方向上走的步数。
需要注意的是,为了防止TLE,我们仍需要用vis数组来标记,但是之前的是标记该位置是否经过。现在需要保存该位置能否从四个方向到达的情况,这里我们用二进制来表示这个状态。
其他细节见代码。
#include <cstdio>#include <queue>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 1005;const ll mod = 1e9+7;const int INF = 0x3f3f3f3f;const double eps = 1e-9;const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int n,m,k;int sx,sy,ex,ey;int vis[maxn][maxn];char mp[maxn][maxn];int dis[maxn][maxn];struct node{ int x,y;}now,nex;bool inbound(node a){ return a.x>=1&&a.x<=n&&a.y>=1&&a.y<=m;}void bfs(){ queue<node>q; now.x=sx; now.y=sy; q.push(now); vis[sx][sy]=(1<<4)-1; //起点只经过一次就好 while(q.size()) { now=q.front(); q.pop(); if(now.x==ex&&now.y==ey) return; //搜到终点 for(int i=0;i<4;i++) //枚举方向 for(int j=1;j<=k;j++) //枚举走的步数 { nex.x=now.x+dir[i][0]*j; nex.y=now.y+dir[i][1]*j; if(!inbound(nex)||mp[nex.x][nex.y]=='#') break; //该位置不合法 if(vis[nex.x][nex.y]&(1<<i)) break; //从这个方向已经走过该位置 int flag=0; if(!vis[nex.x][nex.y]) flag=1; //搜到没有走过的点 vis[nex.x][nex.y]|=(1<<i); //更新状态 if(flag) { dis[nex.x][nex.y]=dis[now.x][now.y]+1; q.push(nex); } } }}int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%s",mp[i]+1); } scanf("%d%d%d%d",&sx,&sy,&ex,&ey); bfs(); if(vis[ex][ey]) printf("%d\n",dis[ex][ey]); else puts("-1"); return 0;}
- Codeforces 877D
- codeforces 877D
- Codeforces 877 D Olya and Energy Drinks
- CodeForces 617D CodeForces 617D
- CodeForces 101D
- CodeForces 103D
- CodeForces 222D Olympiad
- codeforces 242d
- CodeForces 111D
- 【codeforces】3D
- CodeForces 127D Password
- codeforces 257D. Sum
- Codeforces 292D
- Codeforces 275D
- Codeforces 126D
- Codeforces 337d
- Codeforces D. Points
- codeforces 96D Volleyball
- 1031. 查验身份证(15)--PAT乙级
- 大大大
- 入阵曲 (组合数 余数统计)
- 沉浸式(非纯色)
- how to compile kinfu_remake with cuda 9.0 opencv2.4.13.4
- Codeforces 877D
- Deep Neural Networks for YouTube Recommendations
- java-JDBC-ResultSet详解(java数据库操作)
- JS中时间戳转日期格式(YYYY-MM-dd HH:mm:ss)
- css中给div加边框
- 计算机网络第四次作业
- python圣斗士修炼(六):查看python帮助和学会数据类型转换
- 报名 | “阳奉阴违”的半监督学习算法
- html之jQuery .end()妙用