连连看-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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值