首页 > 其他 > 详细

Codeforces_540_C

时间:2016-09-15 16:25:28      阅读:210      评论:0      收藏:0      [点我收藏+]

http://codeforces.com/problemset/problem/540/C

 

简单bfs,注意结束条件。

 

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

string a[505];
int endx,endy,n,m,dir[][2] = {-1,0,0,-1,1,0,0,1};
struct point{
    int x,y;
}start;
int main()
{
    cin >> n >> m;
    for(int i = 0;i < n;i++)    cin >> a[i];
    cin >> start.x >> start.y >> endx >> endy;
    start.x--;
    start.y--;
    endx--;
    endy--;
    queue<point> q;
    q.push(start);
    while(!q.empty())
    {
        point now = q.front();
        q.pop();

        for(int i = 0;i < 4;i++)
        {
            int xx = now.x+dir[i][0];
            int yy = now.y+dir[i][1];
            if(xx == endx && yy == endy && a[xx][yy] == X)
            {
                printf("YES\n");
                return 0;
            }
            if(xx < 0 || xx >= n || yy < 0 || yy >= m) continue;
            if(a[xx][yy] == X)    continue;
            point temp;
            temp.x = xx;
            temp.y = yy;
            q.push(temp);
            a[xx][yy] = X;
        }
    }
    printf("NO\n");
    return 0;
}

 

Codeforces_540_C

原文:http://www.cnblogs.com/zhurb/p/5874929.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!