【链接】 我是链接,点我呀:)

【题意】

你可以往左最多x次,往右最多y次
问你从x,y出发最多能到达多少个格子
只能往上下左右四个方向走到没有障碍的格子

【题解】

假设我们从(r,c)出发想要到固定某个点(i,j)的最短距离
我们设x0为向左走动的次数,y0为向右走动的次数
显然(j-c)=y0-x0
即y0 = (j-c) + x0
这里j-c是一个常数
也就是说当我们让x0最小的时候到达每一个点的对应的y0也一定是最优的,因为和x0成正相关
因此我们只要求出来(r,c)到所有点所用的x0的最小值就好了
会发现没个点到达相邻的点的花费要么就是1(往左走),要么就是0(往其他方向走)
符合01bfs的特点
(01bfs:如果向左走的话,那么就把新得到的状态加入到末尾,否则加入到队头,因为花费为0的话,和之前的dis值相同,依然是当前所有的情况里最低花费
(所以可以用来继续更新最小值,因此是放在队头,这样的话能始终维持队列从头到尾的dis值依次递增的顺序
(因此能保证每次尝试用来更新的状态都是当前状况下最优的状态,不会造成错解或者漏解

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static class Pair{
int x,y,d;
public Pair(int x,int y,int d){
this.x = x;
this.y = y;
this.d = d;
}
} static int N = 2000;
static class Task{
int n,m,r,c,x,y;
String s[];
int dis[][];
int dx[]= {0,0,1,-1},dy[]= {1,-1,0,0};
Deque<Pair> queue; public void solve(InputReader in,PrintWriter out) {
queue = new LinkedList<Pair>();
dis = new int[N+10][N+10];
s = new String[N+10];
n = in.nextInt();m = in.nextInt();
r = in.nextInt();c = in.nextInt();
x = in.nextInt();y = in.nextInt();
for (int i = 1;i <= n;i++) {
s[i] = in.next();
StringBuilder sb = new StringBuilder(s[i]);
sb.insert(0, ' ');
s[i] = sb.toString();
}
for (int i = 1;i <= N;i++)
for (int j = 1;j <= N;j++)
dis[i][j] = -1; dis[r][c] = 0;
queue.offerFirst(new Pair(r,c,0));
while (!queue.isEmpty()) {
Pair temp = queue.pollFirst();
int x0 = temp.x,y0 = temp.y,d = temp.d;
for (int i = 0;i < 4;i++) {
int x1 = x0 + dx[i];int y1 = y0 + dy[i];
int td = d;
if (i==1) td++;
if (x1>=1 && x1<=n && y1>=1 && y1<=m && s[x1].charAt(y1)!='*') {
if (dis[x1][y1]==-1 ||dis[x1][y1]>td) {
dis[x1][y1] = td;
if (i==1) {
queue.offerLast(new Pair(x1,y1,td));
}else {
queue.offerFirst(new Pair(x1,y1,td));
}
}
}
}
}
int ans = 0;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++) {
if (dis[i][j]==-1) continue;
int x0 = dis[i][j];
int y0 = j-c + x0;
if (x0<=x && y0<=y) {
ans++;
}
}
out.println(ans);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 1063B】Labyrinth的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. E20171228-hm

    traverse  n. 穿过; 横贯,横切; 横木; [建] 横梁;               vt. 通过; 横越,横贯; [法] 否认,反驳; [木工] 横刨;

  2. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛【差分】

    s[i]为差分后的"i这头牛前有几头比它高",计算答案的时候加成前缀和,假设第一头最高减一下即可 用map记录一下被加过的区间,避免重复 #include<iostream& ...

  3. Ajax 知识点总结

    1.AJAX的优缺点都有什么? 最大的一点是页面无刷新,用户的体验非常好.使用异步方式与服务器通信,具有更加迅速的响应能力.可以把以前一些服务器负担的工作转嫁到客户端,利用客户端闲置的能力来处理,减轻 ...

  4. [Qt Creator 快速入门] 第3章 窗口部件

    从这一章开始正式接触Qt的窗口部件.在第2章曾看到 Qt Creator 提供的默认基类只有 QMainWindow.QWidget 和 QDialog 这3种.QMainWindow 是带有菜单栏和 ...

  5. 一个JavaScript贷款计算器

    通过本案例,将会学到: . 如何在文档中查找元素 . 如何通过表单input元素来获取用户的输入数据 . 如何通过文档元素来设置HTML内容 . 如何将数据存储在浏览器中 . 如何使用脚本发起HTTP ...

  6. 【hive】hive表很大的时候查询报错问题

    线上hive使用环境出现了一个奇怪的问题,跑一段时间就报如下错误: FAILED: SemanticException MetaException(message:Exception thrown w ...

  7. BZOJ1499: [NOI2005]瑰丽华尔兹(dp)

    Description 你跳过华尔兹吗?当音乐响起,当你随着旋律滑动舞步,是不是有一种漫步仙境的惬意?众所周知,跳华尔兹时,最重要的是有好的音乐.但是很少有几个人知道,世界上最伟大的钢琴家一生都漂泊在 ...

  8. 学习笔记 第五章 使用CSS美化网页文本

    第五章   使用CSS美化网页文本 学习重点 定义字体类型.大小.颜色等字体样式: 设计文本样式,如对齐.行高.间距等: 能够灵活设计美观.实用的网页正文版式. 5.1 字体样式 5.1.1 定义字体 ...

  9. Android常用依赖库搜集

    图片处理 CircleImageView Git地址:https://github.com/hdodenhof/CircleImageView 图片依赖库 glide Git地址:https://gi ...

  10. Python :用两个栈实现队列

    转自:http://blog.csdn.net/Lynette_bb/article/details/75092745 牛客网上的剑指 offer的在线编程: 题目描述 用两个栈来实现一个队列,完成队 ...