[原創] 宮涵韻和大奶牛的手辦 - 搜索

宮涵蘊和大奶牛的手辦

1000ms

 

守序善良宮涵蘊嫌棄混亂邪惡大奶牛天天玩塑料小人不寫代碼,把大奶牛的手辦全扔了,結果把大奶牛氣死了,死後的大奶牛附身到了宮涵蘊身上,和宮涵蘊的靈魂輪流控制身體,奪回身體的唯一方案是在宮涵蘊的身體在宮涵韻意識的控制下下觸碰到被扔棄的手辦。

當前宮涵蘊在座標軸上,她的位置和手辦的位置保證是整數,她可達的所有點爲                                                    

 {(i,j) | 0 ≤ i, j ≤ n - 1, i j爲整數},

宮涵蘊和大奶牛的靈魂每秒交換一次身體控制權限,大奶牛控制身體的時候,每秒能任意往上下左右四個方向前進1,輪到宮涵蘊控制身體的時候,每秒只能往當前朝向的斜對角,即左前方和右前方前進√2 。地圖上會有障礙物,由於大奶牛是死人,所以當他控制身體時,既不會受到障礙物和腳印的限制,也不會留下腳印,而宮涵蘊不能跨越障礙物,同時每一步都會留下腳印,她也會拒絕往有腳印的地方行動。無論是誰控制身體,都不能越過邊界。

初始情況下均由大奶牛佔據身體。請判斷宮涵蘊最後能否奪回身體。

 

Input

第一行爲一個正整數T (T ≤ 15),表示測試用例的數量。

對於每個測試用例:

第一行爲空格隔開的兩個整數 n,m (1 ≤ n, m ≤ 50),分別爲範圍和障礙物的數量。

第二行爲空格隔開的四個整數x1,y1, x2, y2 (0 ≤ x1, y1, x2, y2 ≤ n - 1),爲宮涵蘊軀體所在的起點橫座標,縱座標以及被丟棄的玩具所在的橫座標,縱座標。

接下來 m 行,每行兩個數字,分別代表第 i 個障礙所在的橫座標和縱座標。

 

Output

如果有可能奪回身體,輸出Yes。

如果不能,輸出No。

 

SampleInput

1

4 3

3 2 1 3

1 1

3 1

3 3

 

SampleOutput

Yes

 

Note     

對於樣例,    大奶牛控制身體(3, 2) ->(2, 2)

宮涵蘊控制身體(2, 2) -> (1, 3)

當大奶牛控制身體從(3, 2)移動到(2, 2)時,輪到宮涵蘊控制身體,她只能前往(1, 3)或者(1, 1)。





恩原創題


開心


因爲大奶牛的移動不受任何限制也不會留下任何痕跡,所以題目就轉化成日字形的移動
int xx[10] = {-2, -2, -1, -1, 1, 1, 2, 2};
int yy[10] = {-1, 1, -2, 2, -2, 2, -1, 1};


#include <bits/stdc++.h>
using namespace std;

const int N = 55;

struct Node
{
    int x;
    int y;
};

int vis[N][N];
int bx, by;
int ex, ey;
int n, m;
int xx[10] = {-2, -2, -1, -1, 1, 1, 2, 2};
int yy[10] = {-1, 1, -2, 2, -2, 2, -1, 1};

bool check(Node node)
{
    int x = node.x;
    int y = node.y;

    if(x < 0 || x >= n) return false;
    if(y < 0 || y >= n) return false;
    if(vis[x][y]) return false;


    return true;
}

bool bfs()
{
    Node now;
    Node ne;
    queue<Node> Q;

    now.x = bx;
    now.y = by;
    vis[bx][by] ++;
    Q.push(now);
    while(!Q.empty()){
        now = Q.front();
        Q.pop();
        //printf("%d %d\n", now.x, now.y);
        if(now.x == ex && now.y == ey) return true;
        for(int i = 0; i < 8; i ++){
            ne.x = now.x + xx[i];
            ne.y = now.y + yy[i];
            if(check(ne)){
                vis[ne.x][ne.y] = 1;
                Q.push(ne);
            }
        }

    }

    return false;
}

int main(int argc, char const *argv[])
{
    int ncase;

    //freopen("1.in", "r", stdin);
    //freopen("1.ans", "w", stdout);

    scanf("%d", &ncase);
    while(ncase --){
        scanf("%d%d", &n, &m);
        memset(vis, 0, sizeof(vis));
        scanf("%d%d%d%d", &bx, &by, &ex, &ey);
        for(int i = 0; i < m; i ++){
            int x, y;

            scanf("%d%d", &x, &y);
            vis[x][y] = 1;
        }
        if(bfs()){
            printf("Yes\n");
        }
        else{
            printf("No\n");
        }
    }


    return 0;
}



發佈了77 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章