博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带限制的广搜 codeforces
阅读量:6598 次
发布时间:2019-06-24

本文共 5150 字,大约阅读时间需要 17 分钟。

You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth.

Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.

Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.

The second line contains two integers r, c (1 ≤ r ≤ n, 1 ≤ c ≤ m) — index of the row and index of the column that define the starting cell.

The third line contains two integers x, y (0 ≤ x, y ≤ 109) — the maximum allowed number of movements to the left and to the right respectively.

The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.

It is guaranteed, that the starting cell contains no obstacles.

Output

Print exactly one integer — the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.

Examples
Input
Copy
4 5 3 2 1 2 ..... .***. ...** *....
Output
Copy
10
Input
Copy
4 4 2 2 0 1 .... ..*. .... ....
Output
Copy
7
Note

Cells, reachable in the corresponding example, are marked with '+'.

First example:

+++.. +***. +++** *+++.

Second example:

.++. .+*. .++. .++. 题意 : 给你一个n*m 的矩阵,再给你一个起点,你可以往上下左右四个方向走,但同时又有一个往左右的步数限制,询问所有可能到达的点数有多少个? 思路分析 :   比赛场上直接写了一个 bfs ,pp 了,自信以为A了,今晚又可以涨分,结果....   其实有一点贪心的想法,就是可以上下走的时候一定要先上下走,最后再左右走,用一个双端队列即可 代码示例:
int n, m;int sx, sy;int sl, sr;char mp[2005][2005];struct node{    int x, y;    int l, r;};deque
que;bool vis[2005][2005];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};void bfs(){ que.push_front({sx, sy, sl, sr}); int ans = 0; //vis[sx][sy] = 1; while(!que.empty()){ node v = que.front(); que.pop_front(); if (vis[v.x][v.y]) continue; vis[v.x][v.y] = true; ans++; for(int i = 0; i < 4; i++){ int fx = v.x+dir[i][0]; int fy = v.y+dir[i][1]; if (fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == '*'||vis[fx][fy]) continue; if (i == 2) { if (v.r > 0) que.push_back({fx, fy, v.l, v.r-1}); } else if (i == 3) { if (v.l > 0) que.push_back({fx, fy, v.l-1, v.r}); } else que.push_front({fx, fy, v.l, v.r}); } } printf("%d\n", ans); }int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); cin >> n >> m; cin >> sx >> sy; cin >> sl >> sr; for(int i = 1; i <= n; i++){ scanf("%s", mp[i]+1); } bfs(); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(mp[i][j] == '*') printf("*"); else if (vis[i][j]) printf("+"); else printf("."); } printf("\n"); } return 0;}

 2 . 用类似最短路的写法,每次左右走的时候步数+1,上下走的时候步数不变

代码示例:

const int inf = 0x3f3f3f3f;int n, m;int sx, sy, sl, sr;char mp[2005][2005];struct node{    int x, y;    int l, r;    int sum;    bool operator< (const node &v)const{        return sum < v.sum;    }};priority_queue
que;bool vis[2005][2005];int bu[2005][2005];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};void bfs() { que.push({sx, sy, sl, sr, sl+sr}); memset(vis, false, sizeof(vis)); memset(bu, inf, sizeof(bu)); int ans = 0; bu[sx][sy] = 0; while(!que.empty()){ node v = que.top(); que.pop(); if (!vis[v.x][v.y]) ans++; vis[v.x][v.y] = 1; for(int i = 0; i < 4; i++){ int fx = dir[i][0]+v.x; int fy = dir[i][1]+v.y; if (fx < 1 || fx > n || fy < 1 || fy > m || mp[fx][fy] == '*') continue; if (bu[v.x][v.y] >= bu[fx][fy]) continue; bu[fx][fy] = bu[v.x][v.y]; if ((i == 2) || (i == 3)) bu[fx][fy]++; if (i == 2) { if (v.r) que.push({fx, fy, v.l, v.r-1, v.l+v.r-1}); } else if (i == 3) { if (v.l) que.push({fx, fy, v.l-1, v.r, v.l+v.r-1}); } else que.push({fx, fy, v.l, v.r, v.l+v.r}); } } printf("%d\n", ans);}int main() { cin >> n >> m; cin >> sx >> sy; cin >> sl >> sr; for(int i = 1; i <= n; i++) scanf("%s", mp[i]+1); bfs(); return 0;}

 

 

转载于:https://www.cnblogs.com/ccut-ry/p/9792782.html

你可能感兴趣的文章
server application unavailable
查看>>
浅谈尾递归的优化方式
查看>>
jacob操作注意事项
查看>>
【第8章】 如何使用类和对象
查看>>
关于valgrind的 “Conditional jump or move depends on uninitialised value(s)”
查看>>
Linux 底行模式常用命令
查看>>
Spring表达式注入方法
查看>>
父元素倾斜,子元素消除影响
查看>>
封锁阳光大学
查看>>
【作业1】:撰写技术博客,通过互联网手段了解自身专业,不能停留在一知半解。...
查看>>
Python 面向对象2
查看>>
码农如何写好一封邮件/2
查看>>
支付宝屏蔽微信 微信电商在“危”“机”之间
查看>>
halcon之NCC匹配
查看>>
【第34题】2019年OCP认证12C题库062考试最新考试原题-34
查看>>
开源solr搜索服务器配置
查看>>
htmlunit官网简易教程(翻译)
查看>>
解决linux系统CentOS下调整home和根分区大小
查看>>
gulp初试
查看>>
关于overflow-x: hidden隐藏滚动条失效的解决方案
查看>>