(简单广搜) Ice Cave -- codeforces -- 540C
http://codeforces.com/problemset/problem/540/C
You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.
Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).
You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?
The first line contains two integers, n and m (1 ≤ n, m ≤ 500) — the number of rows and columns in the cave description.
Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).
The next line contains two integers, r1 and c1 (1 ≤ r1 ≤ n, 1 ≤ c1 ≤ m) — your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.
The next line contains two integers r2 and c2 (1 ≤ r2 ≤ n, 1 ≤ c2 ≤ m) — the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
If you can reach the destination, print 'YES', otherwise print 'NO'.
4 6
X...XX
...XX.
.X..X.
......
1 6
2 2
YES
5 4
.X..
...X
X.X.
....
.XX.
5 3
1 1
NO
4 7
..X.XX.
.XX..X.
X...X..
X......
2 2
1 6
YES
In the first sample test one possible path is:
After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std; #define N 550 char G[N][N];
int a[N][N], n, m;
int dir[][] = {{-,},{,-},{,},{,}}; struct node
{
int x, y;
}Start, End; int BFS()
{
int i;
node p, q; queue<node>Q;
Q.push(Start); ///a[Start.x][Start.y]--; 由于题意理解错误, 死在了这里 while(Q.size())
{
p = Q.front(), Q.pop(); for(i=; i<; i++)
{
q.x = p.x + dir[i][];
q.y = p.y + dir[i][]; if(q.x==End.x && q.y==End.y && a[q.x][q.y]==) return ; if(q.x>= && q.x<n && q.y>= && q.y<m && a[q.x][q.y]==)
{
a[q.x][q.y]--;
Q.push(q);
}
}
} return ;
} int main()
{
int i, j;
scanf("%d%d", &n, &m); memset(G, , sizeof(G));
memset(a, , sizeof(a)); for(i=; i<n; i++)
{
scanf("%s", G[i]);
for(j=; j<m; j++)
{
if(G[i][j] == 'X')
a[i][j] = ;
if(G[i][j] == '.')
a[i][j] = ;
}
} scanf("%d%d", &Start.x, &Start.y);
scanf("%d%d", &End.x, &End.y); Start.x--, Start.y--;
End.x--, End.y--; int ans = BFS(); if(ans)
printf("YES\n");
else
printf("NO\n"); return ;
}
(简单广搜) Ice Cave -- codeforces -- 540C的更多相关文章
- POJ 3126 Prime Path 简单广搜(BFS)
题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...
- POJ1376简单广搜
题意: 给你一个n*m的矩阵,然后给你机器人的起点和终点,还有起点的方向,然后每次机器人有两种操作,左右旋转90度,或者是朝着原来的方向走1,2或者3步,机器人再走的过程中不能碰到格子,也 ...
- hdu 2612(Find a way)(简单广搜)
Find a way Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Su ...
- 简单广搜,迷宫问题(POJ3984)
题目链接:http://poj.org/problem?id=3984 解题报告: 1.设置node结构体,成员pre记录该点的前驱. 2.递归输出: void print(int i) { ) { ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- CodeForces 540C Ice Cave (BFS)
http://codeforces.com/problemset/problem/540/C Ice Cave Time Limit:2000MS Memory Limit:262 ...
- CodeForces - 540C Ice Cave —— BFS
题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...
- 『ice 离散化广搜』
ice(USACO) Description Bessie 在一个冰封的湖面上游泳,湖面可以表示为二维的平面,坐标范围是-1,000,000,000..1,000,000,000. 湖面上的N(1 & ...
随机推荐
- 在电脑上查看小米手机连接wifi时保存的密码
手机连接的wifi,想另一个手机也连上,密码忘了. 先备份手机的资料,然后将手机连上电脑,在SD卡上MIUI-backup-allbackup文件夹中, 找到刚才备份的文件夹 点进去找到WLAN设置. ...
- (O)编写可维护的代码示例(原创)
图片轮播: /*广告图片数组*/ var imgs=[ {"i":0,"img":"images/index/banner_01.jpg"} ...
- shell脚本返回字符串
shell脚本的return只能返回数值类型,可是我们很多时候想返回字符串 #!/bin/sh function getStr () { String="very good" ec ...
- Java JMX 监管
Java JMX 监管 JSR 规范系列目录(https://www.cnblogs.com/binarylei/p/10348178.html) JMX(Java Management Extens ...
- 让UI设计师崩溃的瞬间,你经历过哪些?
隔行如隔山,这句话人人耳熟能详,但其实隔行并不可怕,大家各谋其事,各尽其职,倒也互不打扰,真正可怕的是,是内行还要受外行指点江山,而最难的部分,便是那沟通.流畅的沟通,和声细语,是有如时雨之化者:无效 ...
- python 函数调用顺序
def foo(): print ('in the foo') bar() def bar(): print ('in the bar') foo() 1.foo函数进入内存 2.bar函数进入内存 ...
- 消息中间件及WebSphere MQ入门(转载)
消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需要知道彼此的位置.或在继续执行前不需要等待 ...
- [ASP.NET]使用Oracle.ManagedDataAccess的OracleParameter参数化和OracleDataAdapter模糊查询
今天写个查询员工的信息的demo遇到了2个问题 问题1.使用Oracle.ManagedDataAccess的OracleParameter参数化 OracleParameter 的使用(参数名要以: ...
- 进入快速通道的委托(深入理解c#)
1.方法组:所有的名称相同的重载方法合在一起就成为一个方法组. 2.协变性和逆变性: 协变性指的是——泛型类型参数可以从一个派生类隐式转化为基类. 逆变性指的是——泛型类型参数可以从一个基类隐式转化为 ...
- 如何将mysql卸载干净
一.在控制面板中卸载mysql软件 二.卸载过后删除C:\Program Files (x86)\MySQL该目录下剩余了所有文件,把mysql文件夹也删了 三.windows+R运行“regedit ...