离散化BFS,二维点的离散化

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 136   Accepted Submission(s) : 19
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

XiaoMing recently plays the World of Warcraft game, you know, World of the Warcraft map is very big and now XiaoMing falls into a large forest, assuming that the forest is a rectangle with N by M. there are only some trees in the forest that he cannot go through and he can't be out of the boundary of the forest. He would like to know that could he find the exit of the forest.

Input

The first line of input is T,( 1 <= T <= 50) the number of test cases. Each test case starts with three integers N,M,K(1<=N,M<=1000000,0<=K<=200) ,which means that the sizes of the maze and the number of trees. Then follow K lines, each line contains two integers Xi, Yi(0<=Xi< N,0<=Yi<M) denoting the position of each tree. The Last line consists of four integers Sx, Sy, Ex, Ey (0<=Sx, Ex<N, 0<=Sy, Ey<M) denoting the position of XiaoMing's starting place and the position of the exit.
Note: The starting place and the exit will not have trees there.

Output

For every test case, you should output "Case k: " first in a single line, where k indicates the case number and starts at 1. Then print "YES" if XiaoMing can reach the exit, or print "NO" if he cannot.

Sample Input

2
6 6 5
0 0
0 1
1 1
2 0
2 1
1 0 5 5
6 6 4
0 0
0 1
2 0
2 1
1 0 5 5

Sample Output

Case 1: NO
Case 2: YES
 
 
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1000
int mp[N][N];
struct Point
{
    int x, y;
    int nowx, nowy;
    int ises;
};
struct Node
{
    int x, y;
    Node() {}
    Node(int a, int b)
    {
        x = a;
        y = b;
    }
};
Point p[205];
int cmpx(Point a, Point b)
{
    return a.x < b.x;
}
int cmpy(Point a, Point b)
{
    return a.y < b.y;
}
int cnt = 1;
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n, m, k;
        int x, y;
        int sx, sy, ex, ey;
        scanf("%d%d%d", &n, &m, &k);
        memset(mp, 0, sizeof(mp));
        for (int i = 0; i < k; i++)
        {
            scanf("%d%d", &x, &y);
            p[i].x = x;
            p[i].y = y;
            p[i].ises = 0;
        }
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        p[k].ises = 1;
        p[k].x = sx;
        p[k++].y = sy;
        p[k].ises = 1;
        p[k].x = ex;
        p[k++].y = ey;

        p[k].ises = 2;//把边界也加入离散化
        p[k].x = n;
        p[k++].y = m;

        sort(p, p + k, cmpx);
        x = 0;
        if (p[0].x == 0)//把边界也加入离散化
            p[0].nowx = x;
        else
            p[0].nowx = ++x;

        for (int i = 1; i < k; i++)
        {
            if (p[i].x == p[i - 1].x)
            {
                p[i].nowx = x;
            }
            else if (p[i].x == p[i - 1].x + 1)
            {
                p[i].nowx = ++x;
            }
            else
            {
                p[i].nowx = x + 2;
                x += 2;
            }
        }
        sort(p, p + k, cmpy);
        y = 0;
        if (p[0].y == 0)
            p[0].nowy = y;
        else
            p[0].nowy = ++y;
        for (int i = 1; i < k; i++)
        {
            if (p[i].y == p[i - 1].y)
            {
                p[i].nowy = y;
            }
            else if (p[i].y == p[i - 1].y + 1)
            {
                p[i].nowy = ++y;
            }
            else
            {
                p[i].nowy = y + 2;
                y += 2;
            }
        }
        for (int i = 0; i < k; i++)
        {
            if (p[i].ises == 0)
                mp[p[i].nowx][p[i].nowy] = 1;
            else if (p[i].ises == 1)
            {
                if (p[i].x == sx && p[i].y == sy)
                {
                    sx = p[i].nowx;
                    sy = p[i].nowy;
                    mp[sx][sy] = 2;
                }
                else if (p[i].x == ex && p[i].y == ey)
                {
                    ex = p[i].nowx;
                    ey = p[i].nowy;
                    mp[ex][ey] = 2;
                }
            }
            else
            {
                n = p[i].nowx;
                m = p[i].nowy;
            }
        }
        bool vis[N][N] = zero;
        queue<Node> Q;
        Node node(sx, sy);
        Q.push(node);
        bool flag = false;
        while (!Q.empty())
        {
            node = Q.front();
            Q.pop();
            if (node.x == ex && node.y == ey)
            {
                flag = true;
                break;
            }
            for (int i = 0; i < 4; i++)
            {
                x = node.x + dir[i][0];
                y = node.y + dir[i][1];
                if (x < 0 || y < 0 || x >= n  || y >= m || mp[x][y] == 1 || vis[x][y])
                    continue;
                Node now(x, y);
                vis[x][y] = 1;
                Q.push(now);
            }
        }
        printf("Case %d: ", cnt++ );
        if (flag)
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值