题目链接

题意:

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. 调整maven配置文件

    maven的配置文件位置:maven安装位置\conf\settings.xml. 这次调整maven的配置文件主要解决三个问题: 调整本地依赖库位置 设置代理 添加远程资源库镜像节点 调整本地依赖库 ...

  2. ExtJS MVC学习手记

    开始学习ExtJS的MVC了.这篇文章仅是用来做一个目录,为自己这个阶段的学习内容做个索引. 手记涉及的文章: EXTJS MVC结构(译自ExtJS4.0文档中的<MVC Architectu ...

  3. WCF note1

    Summary of WCF Client to use service use ChannelFactory to create proxy to use service. Client code ...

  4. mysql 任意连接

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDE ...

  5. Qt的QTabelWidget

    QTableWidget的用法总结  http://blog.csdn.net/zb872676223/article/details/39959061 [Qt]在QTableWidget中添加QCh ...

  6. [shell实例]——用脚本实现向多台服务器批量复制文件(nmap、scp)

    练习环境: (1)所有服务器将防火墙和selinux关闭 (2)所有服务器的root密码设置为aixocm (3)所有服务器都为10.0.100.*网段,并保证能够和其它主机通信 (4)所有服务器确保 ...

  7. c语言学习strcopy

    自己写了一个字符串复制函数: #include<stdio.h> #include<assert.h> char *mystrcpy(char *des,char *ser) ...

  8. Shell采集系统cpu 内存 磁盘 网络信息

    cpu信息采集 cpu使用率 采集算法 通过/proc/stat文件采集并计算CPU总使用率或者单个核使用率.以cpu0为例,算法如下: 1. cat /proc/stat | grep ‘cpu0’ ...

  9. sql中having的使用

    where 和having有什么区别? where 是group by之前进行筛选,having是group by 之后进行统计的筛选,一般having会和group by一起使用,结合聚合函数,统计 ...

  10. LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...