题目链接

题意:

n*m的地图,'X'表示有裂痕的冰块,'.'表示完整的冰块,有裂痕的冰块再被踩一次就会碎掉,完整的冰块被踩一次会变成有裂痕的冰块,

现在告诉起点和终点,问从起点能否走到终点并且使终点的冰块碎掉。不能原地跳。起点和终点可能会在同一个位置。

解题思路:

在只走‘.’的情况下把终点的冰踩碎

 

输入n*m的矩阵

以及走的开始和终点位置

在开始点,上下左右找‘.’,有就走,并把改点设置为‘X’,走到终点时候,若终点是‘X’则成功。

其他情况都失败。

 

这个程序是在codeforces上面抄的

Java程序:

import java.io.PrintWriter;
import java.util.Scanner; public class C540 {
static void run0() throws Exception {
PrintWriter pw = new PrintWriter(System.out);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][] ch = new char[n][m];
for(int i=0;i<n;i++)
ch[i] = sc.next().toCharArray();
int fromX = sc.nextInt() - 1;
int fromY = sc.nextInt() -1;
int toX = sc.nextInt() -1;
int toY = sc.nextInt() -1;
ch[fromX][fromY]='.';
if(recur(ch,fromX,fromY,toX,toY)) pw.println("YES");
else pw.println("NO");
pw.close();
}
// 暴力破解,在四个方向做判断
public static boolean recur(char[][] ch,int curX,int curY,int tX,int tY){
// 越界失败
if(curX<0 || curY<0 ||curX>=ch.length||curY>=ch[0].length) return false;
// 走了回去,并且是在X的情况下,说明已经走了一次,或者 本来就是X
if(curX==tX &&curY==tY &&ch[tX][tY]=='X') return true;
// X 不可走
if(ch[curX][curY]=='X') return false;
// 是 点的 情况,可以走,走过设置为 X
         ch[curX][curY] = 'X';
return recur(ch,curX-1,curY,tX,tY)||
recur(ch,curX+1,curY,tX,tY)||
recur(ch,curX,curY-1,tX,tY)||
recur(ch,curX,curY+1,tX,tY);
} public static void main(String[] args) throws Exception{
run0();
}
}

上面的注释能很好的理解程序。

下面的程序和上面的差不多

也是复制别人的

import java.util.Scanner;

public class C540_1 {
static int n;
static int m;
static char[][] map;
static boolean[][] visited;
static int pi[];
static int pf[];
static int[] R = new int[] {1, 0, -1, 0};
static int[] C = new int[] {0, 1, 0, -1};
public static void main(String[] args){
Scanner sc = new Scanner(System.in); n=sc.nextInt();
m= sc.nextInt();
map = new char[n][m];
visited = new boolean[n][m];
for(int i=0;i<n;++i){
String s=sc.next();
for(int j=0;j<m;j++){
map[i][j] = s.charAt(j);
visited[i][j] = false;
}
}
pi = new int[]{sc.nextInt()-1,sc.nextInt()-1};
pf = new int[]{sc.nextInt()-1,sc.nextInt()-1};
if(dfs(pi[0],pi[1])) System.out.println("YES");
else System.out.println("NO");
}
static boolean dfs(int r,int c){
// 走过的点设置为 true
visited[r][c] = true;
// 改点走后设置为 X
map[r][c]='X';
for(int k=0;k<4;++k){
int rr = r+R[k];
int cc = c+C[k];
if(rr>=0 && rr<n &&cc>=0 &&cc< m){
if(rr==pf[0] &&cc==pf[1] &&map[rr][cc]=='X')
return true;
// !=X 没有被访问过 ,当前点可以走
else if(map[rr][cc]!='X' && visited[rr][cc] ==false &&dfs(rr,cc))
return true;
}
}
return false;
}
}

540C: Ice Cave的更多相关文章

  1. CF#301 C:Ice Cave(简单BFS)

    C:Ice Cave 有一个m*n的地图,里面包含'.'表示完整的冰块,'X'表示有裂痕的冰块,当游戏者到达完整的冰块时,这个位置的冰块会变成有裂痕的冰块,如果到达有裂痕的冰块时,游戏者会进入下一关 ...

  2. CodeForces 540C Ice Cave (BFS)

    http://codeforces.com/problemset/problem/540/C       Ice Cave Time Limit:2000MS     Memory Limit:262 ...

  3. CodeForces - 540C Ice Cave —— BFS

    题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...

  4. (简单广搜) Ice Cave -- codeforces -- 540C

    http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on so ...

  5. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

  6. Codeforces Round #301 (Div. 2) C. Ice Cave BFS

    C. Ice Cave Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/problem/C ...

  7. ice cave

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

  8. ICE CAVE(BFS搜索(模拟))

    Description You play a computer game. Your character stands on some level of a multilevel ice cave. ...

  9. CodeForces 540C Ice Cave (BFS)

    题意:给定 n * m的矩阵,让你并给定初始坐标和末坐标,你只能走'.',并且走过的'.'都会变成'X',然后问你能不能在末坐标是'X'的时候走进去. 析:这个题,在比赛时就是没做出来,其实是一个水题 ...

随机推荐

  1. LNMP下wordpress无法切换主题,只显示当前主题解决方法

    最近在lnmp下发现wordpress后台无法切换主题,只能显示当前主题,开始还以为是文件没传完,又重置了一遍,还是一样.百度得知,原来军哥的LNMP安装包默认关闭了scandir函数,为了安全考虑. ...

  2. 华硕电脑安装ubuntu出现问题及决方案

    问 题 一:华硕电脑安装ubuntu时无线网络禁用解决方案:打开终端(Ctrl+alt+t)运行命令sudo rmmod acer-wmi,然后开启无线,连接上后便可以上网(附上ubuntu论坛上讨论 ...

  3. 触发器(trigger)的作用???

    1.触发器,英文名trigger,可以简单的理解为: 就相当于是一个事件的触发装置,当满足了一定的事件触发条件后进行相应的操作 例如当复位set信号到来时,我们就让A<=B,这样一个系统就是一个 ...

  4. Python实现C4.5(信息增益率)

    Python实现C4.5(信息增益率) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>star ...

  5. Haskell 趣学指南 入门笔记(二)

    显示类型声明,Haskell是不用定义类型的原因,很像python 想要确定某个表达式的类型 *Main> :t 'a' 'a' :: Char *Main> :t True True : ...

  6. LintCode-Serialization and Deserialization Of Binary Tree

    Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a ...

  7. [转载]ubuntu的版本

    http://bbs.chinaunix.net/thread-2126589-2-1.html  希望他说的是对的 ubuntu 发布的linux里面有一个非常具有迷惑性的版本-desktop.因为 ...

  8. Asp.net将图片转为Base64编码

    protected void Page_Load(object sender, EventArgs e) { Image img = new Bitmap(Server.MapPath("/ ...

  9. iis express 启动多个网站

      iis express一次只能运行一个网站,  执行iisexpress 不加参数. 将执行配置文件中的第一个网站. iis express一次只能运行一个 应用程序池. 可以使用这个特点实现一次 ...

  10. Spring声明式事务配置管理方法(转)

    项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add libra ...