第九届山东省ACM大学生程序设计Dominoes +bfs

链接: https://www.nowcoder.com/acm/contest/123/H
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

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.
示例1

输入

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
#define happy

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define P pair<int,int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int n,m,k;
const int N=1e5+10;
int vis[15][N],mp[15][N];

struct node{
    int x,y;
};

bool check(int x,int y){
    if(x>=1&&y>=1&&x<=n&&y<=m)return true;
    return false;
}

int bfs(int x,int y){
    int ans=0;
    queue<node>q;
    q.push({x,y});
    vis[x][y]=1;
    while(!q.empty()){
        node t=q.front();
        q.pop();
        int xx=t.x;int yy=t.y;
        if(mp[xx-1][yy]==2&&check(xx-2,yy)&&vis[xx-2][yy]==0){
            ans++;
            vis[xx-2][yy]=1;
            q.push({xx-2,yy});
        }
        if(mp[xx+1][yy]==2&&check(xx+2,yy)&&vis[xx+2][yy]==0){
            ans++;
            vis[xx+2][yy]=1;
            q.push({xx+2,yy});
        }
        if(mp[xx][yy-1]==1&&check(xx,yy-2)&&vis[xx][yy-2]==0){
            ans++;
            vis[xx][yy-2]=1;
            q.push({xx,yy-2});
        }
        if(mp[xx][yy+1]==1&&check(xx,yy+2)&&vis[xx][yy+2]==0){
            ans++;
            vis[xx][yy+2]=1;
            q.push({xx,yy+2});
        }
    }
    return ans;
}

int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    while(~scanf("%d%d%d",&n,&m,&k)){
        memset(vis,0,sizeof(vis));
        memset(mp,0,sizeof(mp));
        rep(i,0,k-1){
            int x=rd(),y=rd(),p=rd(),q=rd();
            if(x==p)
                mp[x][y]=mp[p][q]=1;
            else mp[x][y]=mp[p][q]=2;
        }
        int ans=0;
        rep(i,1,n)rep(j,1,m)if(mp[i][j]==0){
            ans=bfs(i,j);
            break;
        }
        printf("%d\n",ans);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值