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

来源:互联网 发布:3d绘图软件 编辑:程序博客网 时间:2024/06/13 09:21

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

26 6 50 00 11 12 02 11 0 5 56 6 40 00 12 02 11 0 5 5

Sample Output

Case 1: NOCase 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-6typedef 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 1000int 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;}


0 0