您的位置:首页 > 其它

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

2015-06-05 18:10 302 查看
题目链接:http://codeforces.com/contest/540/problem/C

(最近 unhappy!)

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

代码:

[code]#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: