zoukankan      html  css  js  c++  java
  • 6993: Dominoes(纯bfs)

    题目描述
    Orz likes to play dominoes. Now giving an n*m chessboard and k dominoes whose size are 1*2, Orz finds that there is exactly one grid empty, so that he can move dominoes without overlapping them. An initial situation is given, he wants to know how many final situation could be achieved, except the initial situation. Note every domino is different, as they have their own serial number. Since the answer may be very large, please output the answer modulo 1000000009. 

    输入
    There will be multiple test cases. For each test case:
    The first line contains three integers: n, m, k(n ≤ 9, m ≤ 10000).
    The following k lines, each line contains four integers: a  b  c  d, indicating that this domino occupies (a, b) and (c, d). 
    The input guarantees that the domino does not overlap, and there is exactly one empty grid.

    输出
    For each test cases, output the answer modulo 1000000009.

    样例输入
    5 5 12
    1 1 2 1
    1 2 2 2
    1 3 2 3
    1 4 1 5
    2 4 2 5
    3 4 3 5
    3 1 3 2
    4 1 4 2
    5 1 5 2
    4 3 5 3
    4 4 5 4
    4 5 5 5
    样例输出
    8

    题意:类似华容道,问空格能在多少个位置出现

    多米诺牌是1*2大小的,它可以横着放,也可以竖着放。

    题目给出了k个多米诺牌的两个格子的坐标。

    牌横着放的时候两个格子的横坐标相等,记为1;竖着放的时候两个格子的纵坐标相等,记为2。

    空格可以上下左右四个方向移动:

    如果空格能向上向下移动,那么,空格上下的牌必须是竖着放的;

    如果空格能向左向右移动,那么,空格左右的牌必须是横着放的。

    首先判断空格上下左右的牌是如何放置的,再判断空格能否移动。

    标记空格走过的位置,空格出现的位置的状态是唯一的。

    纯bfs,一层一层的搜。

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<cstdio>
    using namespace std;
    int MAP[15][10005],ans,vis[15][10005],n,m;
    int dir[4][2]= {-1,0,1,0,0,-1,0,1};
    struct node
    {
        int x,y;
    };
    queue<node>p;
    int judge(int x,int y)
    {
        if(x>=1&&x<=n&&y>=1&&y<=m)
            return 1;
        else
            return 0;
    }
    void bfs_(int tx,int ty)
    {
        node q;
        q.x=tx,q.y=ty;
        p.push(q);//将空格的位置入队列
        vis[tx][ty]=1;
        int sx,sy;
        while(!p.empty())
        {
            node temp;
            temp=p.front();
            p.pop();
            vis[temp.x][temp.y]=1;
            node L;
            sx = temp.x;
            sy = temp.y;
            if(MAP[sx-1][sy]==2&&judge(sx-2,sy)&&vis[sx-2][sy]==0)//空格上面的牌是竖着放的,牌只能向下移动
            {                                                     //(sx-2,sy)是移动之后空格的坐标
                ans++;
                L.x=sx-2,L.y=sy;
                vis[sx-2][sy]=1;
                p.push(L);
            }
            if(MAP[sx+1][sy]==2&&judge(sx+2,sy)&&vis[sx+2][sy]==0)//空格下面的牌是竖着放的,牌只能向上移动
            {
                ans++;
                L.x=sx+2,L.y=sy;
                vis[sx+2][sy]=1;
                p.push(L);
            }
            if(MAP[sx][sy-1]==1&&judge(sx,sy-2)&&vis[sx][sy-2]==0)//空格左边的牌是横着放的,牌只能向右移动
            {
                ans++;
                L.x=sx,L.y=sy-2;
                vis[sx][sy-2]=1;
                p.push(L);
            }
            if(MAP[sx][sy+1]==1&&judge(sx,sy+2)&&vis[sx][sy+2]==0)//空格右边的牌是横着放的,牌只能向左移动
            {
                ans++;
                L.x=sx,L.y=sy+2;
                vis[sx][sy+2]=1;
                p.push(L);
            }
        }
    
    }
    int main()
    {
        int k,tx,ty;
        while(~scanf("%d%d%d",&n,&m,&k))
        {
            int x1,y1,x2,y2;
            ans=0;
            memset(MAP,0,sizeof(MAP));
            memset(vis,0,sizeof(vis));
            for(int i=1; i<=k; i++)
            {
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                if(x1 == x2)
                    MAP[x1][y1]=1,MAP[x2][y2]=1;
                else
                    MAP[x1][y1]=2,MAP[x2][y2]=2;
            }
            int flag = 0;
            for(int i=1; i<=n; i++) //找到空格的位置
            {
                for(int j=1; j<=m; j++)
                {
                    if(MAP[i][j] == 0)
                    {
                        tx=i,ty=j;
                        flag = 1;
                        break;
                    }
                }
                if(flag)
                    break;
            }
            bfs_(tx,ty);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    第一阶段用户模板和用户场景
    团队开发冲刺第十二天
    第十周总结
    团队开发冲刺第十一天
    团队开发冲刺第十天(实现页面展示评论数与点赞数)
    团队开发冲刺第九天(实现收藏,点赞,关注功能)
    团队开发冲刺第八天(实现评论功能)
    软件用户场景分析
    第九周总结
    团队开发冲刺第六天(新闻详情页的展示)
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/10735135.html
Copyright © 2011-2022 走看看