Codeforces_540_C

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
");
                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
");
    return 0;
}
原文地址:https://www.cnblogs.com/zhurb/p/5874929.html