Codeforces Round #301 C (Div. 2) 【dfs】

来源:互联网 发布:南京行知教育集团 编辑:程序博客网 时间:2024/05/04 07:06

题目链接:http://codeforces.com/contest/540/problem/C

(最近 unhappy!)

题意:初始时候在一个破碎的冰块上,每次只能走到临近的四个未破碎冰块上。未破碎的冰块被踩后变为破碎。且不能在冰块上停留。踩上破碎的冰块就会掉入此坐标。问 是否可以在掉进给定的坐标。

代码:

#include <stdio.h>#include <iostream>#include <math.h>#include <string.h>#include <string>#include <algorithm>#include <ctype.h>#include <time.h>#include <queue>#include <stdlib.h>using namespace std;int n, m, ok;char p[510][510];int sx, sy, ex, ey;int dir[4][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };int check(int x, int y){    if (x < 1 || x > n || y < 1 || y > m) return 0;    if (p[x][y] == 'X')    {        if (x == ex && y == ey )            ok = 1;        return 0;    }    return 1;}void dfs(int x,int y){    p[x][y] = 'X';    for (int i = 0; i < 4; i++)    {        int xx = x + dir[i][0];        int yy = y + dir[i][1];        if (check(xx, yy)) dfs(xx, yy);    }                                }int main(){    while (scanf("%d%d", &n, &m) != EOF)    {        for (int i = 1; i <= n; i++)            scanf("%s",p[i]+1);        //cout << endl;        //for (int i = 1; i <= n; i++)        //  puts(p[i]+1);        scanf("%d%d%d%d",&sx,&sy,&ex,&ey);        ok = 0;        dfs(sx,sy);        if (ok)            puts("YES");        else            puts("NO");    }    return 0;}
0 0
原创粉丝点击