連連看-BFS

連連看
Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

“連連看”相信很多人都玩過。沒玩過也沒關係,下面我給大家介紹一下遊戲規則:在一個棋盤中,放了很多的棋子。如果某兩個相同的棋子,可以通過一條線連起來(這條線不能經過其它棋子),而且線的轉折次數不超過兩次,那麼這兩個棋子就可以在棋盤上消去。不好意思,由於我以前沒有玩過連連看,諮詢了同學的意見,連線不能從外面繞過去的,但事實上這是錯的。現在已經釀成大禍,就只能將錯就錯了,連線不能從外圍繞過。 
玩家鼠標先後點擊兩塊棋子,試圖將他們消去,然後遊戲的後臺判斷這兩個方格能不能消去。現在你的任務就是寫這個後臺程序。 
  

Input

輸入數據有多組。每組數據的第一行有兩個正整數n,m(0<n<=1000,0<m<1000),分別表示棋盤的行數與列數。在接下來的n行中,每行有m個非負整數描述棋盤的方格分佈。0表示這個位置沒有棋子,正整數表示棋子的類型。接下來的一行是一個正整數q(0<q<50),表示下面有q次詢問。在接下來的q行裏,每行有四個正整數x1,y1,x2,y2,表示詢問第x1行y1列的棋子與第x2行y2列的棋子能不能消去。n=0,m=0時,輸入結束。 
注意:詢問之間無先後關係,都是針對當前狀態的! 
  

Output

每一組輸入數據對應一行輸出。如果能消去則輸出"YES",不能則輸出"NO"。 
  

Sample Input

3 4 1 2 3 4 0 0 0 0 4 3 2 1 4 1 1 3 4 1 1 2 4 1 1 3 3 2 1 2 4 3 4 0 1 4 3 0 2 4 1 0 0 0 0 2 1 1 2 4 1 3 2 3 0 0
  

Sample Output

YES NO NO NO NO YES
  

當我看到此題的時候,從起點搜索到終點,因爲這裏並沒有要求最短路徑。所以我首先想到的是用DFS,並且用定義了一個結構體向量用來保存走過的位置,每次搜到終點之後就迭代一次vector,如果轉折小於兩次就返回不再搜索。否則即使能走到終點,只要轉折次數大於2也會輸出NO。當我提交之後,判題系統提示我Memory Limited Exceeded:超內存錯誤。這是因爲我用的vector保存的結構體佔用了大量內存導致內存超限。如下我是提交的內存超限代碼:

#include <string>
#include <vector>
#include <iostream>
using namespace std;

int n, m;
int a[1000][1000];
int vis[1000][1000];
int x1, y1, x2, y2;
int startA, endA;
int result;
struct T
{
    int x;
    int y;
};
vector<T> v;
vector<T>::iterator it;

void DFS(int x, int y)
{
    if (x <= 0 || y <= 0 || x > n || y > m || vis[x][y]) {
        return ;
    }
    struct T a;
    a.x = x;
    a.y = y;
    v.push_back(a);
    
    if (x == x2 && y == y2) {
        int sum  = 0;
        int tempX, tempY;
        it = v.begin();
        struct T temp = *it;
        tempX = temp.x;
        tempY = temp.y;
        for (it = v.begin(); it != v.end(); it++) {
            temp = *it;
            if (temp.x != tempX && temp.y != tempY) {
                sum++;
                tempX = temp.x;
                tempY = temp.y;
            }
        }
        if (sum <= 2) {
            result = sum;
            return;
        }
        
    }
    vis[x][y] = 1;
    DFS(x, y - 1);
    DFS(x - 1, y);
    DFS(x, y + 1);
    DFS(x + 1, y);
    vis[x][y] = 0;
}


int main(int argc, const char * argv[]) {
    
    while (cin >> n >> m) {
        if (n == 0 && m == 0) {
            break;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> a[i][j];
            }
        }
        
        int q;
        cin >> q;
        for (int i = 0; i < q; i++) {
            cin >> x1 >> y1 >> x2 >> y2;
            startA = a[x1][y1];
            endA = a[x2][y2];
            if (startA != endA || startA == 0 || endA == 0) {
                cout << "NO" << endl;
                continue;
            }
            DFS(x1, y1);
            if (result <= 2 && result > 0) {
                cout << "YES" << endl;
            }else {
                cout << "NO" << endl;
            }
        }
    }
    return 0;
}

用DFS沒通過,我纔想着用BFS來解決此題,用隊列queue來解決內存佔用過多的問題,因爲需要經常pop,所以用queue不會出現內存超限的問題。

#include <queue>
#include <iostream>
#include <string.h>
using namespace std;

#define MAXN 1010
#define MAXM 1010

//定義一個二維數組保存方向
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, m;
int q;
int x1, y1;
int x2, y2;
//OK的作用:標記功能,當找到可行解的時候就把OK標記爲1
int OK = 0;
int a[MAXN][MAXM], vis[MAXN][MAXM];

struct positon
{
    int preX, preY; //(preX, preY)上一個點的座標
    int x, y;       //(x, y)當前點的座標
    int cnt;        //走到當前點轉折的次數
} now, eed;

int can(struct positon aa)
{
    /** 之前的想法是隻要不超出邊界,下一個不爲0並且下一個數和終點的數相等就可以往下走,但是這樣的想法是錯誤的,
     因爲其它地方也可能會有和終點一樣的數
    if (aa.x <= 0 || aa.x > n || aa.y <= 0 || aa.y > m || vis[aa.x][aa.y] || (a[aa.x][aa.y] > 0 && a[aa.x][aa.y] != a[x2][y2]) )
     {
     return 0;
     }
     return 1;
     */
    
    if (aa.x >= 1 && aa.x <= n && aa.y >= 1 && aa.y <= m
        && !vis[aa.x][aa.y] && (a[aa.x][aa.y] == 0 || (aa.x == x2 && aa.y == y2)))
        return 1;
    return 0;
    
}
void BFS()
{
    queue<positon> Q;
    now.x = x1;
    now.y = y1;
    now.preX = x1;
    now.preY = y1;
    now.cnt = 0;
    memset(vis, 0, sizeof(vis));
    Q.push(now);
    vis[now.x][now.y] = 1;
    while (!Q.empty())
    {
        now = Q.front();
        Q.pop();
        if (now.x == x2 && now.y == y2)
        {
            //找到終點,OK標記爲1,並返回
            OK = 1;
            return;
            //}
        }
        for (int i = 0; i < 4; i++)
        {
            eed.x = now.x + dir[i][0];
            eed.y = now.y + dir[i][1];
            eed.preX = now.x;
            eed.preY = now.y;
            eed.cnt = now.cnt;
            if (can(eed))
            {
                if (eed.x != now.preX && eed.y != now.preY)
                {
                    if (now.cnt == 2) continue;
                    eed.cnt += 1;
                }
                vis[eed.x][eed.y] = 1;
                
                Q.push(eed);
            }
            
        }
        
    }
}

int main(int argc, const char * argv[])
{
    
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
        {
            break;
        }
        
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> a[i][j];
            }
        }
        cin >> q;
        for (int i = 0; i < q; i++)
        {
            OK = 0;
            cin >> x1 >> y1 >> x2 >> y2;
            if (a[x1][y1] != a[x2][y2] || a[x1][y1] == 0 || a[x2][y2] == 0)
            {
                //如果起點和終點不相同,或者起點與終點任意一點爲0,就直接輸出NO
                cout << "NO" << endl;
                continue;
            }
            BFS();
            if (OK == 1)
            {
                cout << "YES" << endl;
            }
            else
            {
                cout << "NO" << endl;
            }
            
        }
    }
    return 0;
}


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