[2016 indeed笔试题] Tables and Pieces

Question
There is a 6×6 table. Place a number of pieces on the table to meet the following conditions:
There is to be either zero or one piece in each square of the table.
Each column in the table is to have exactly three pieces.
Each row in the table is to have exactly three pieces.
There may already be pieces in some of the squares of the table. When si,j is 'o', there is already a piece in the square at the jth column of the ith row. When that is not the case, si,j is '.' and there is no piece in that square. Find out the number of ways to place pieces in the empty squares, which satisfies the conditions. Two ways are considered different if they have at least one square which contains a piece in one way and doesn't contain a piece in the other way.


Constrains
si,j is 'o' or '.'


Input
Inputs are provided from standard inputs in the following form.


s1,1 … s1,6
:
s6,1 … s6,6


Output
Output the number of ways that pieces can be placed on the empty squares to satisfy the conditions.


Input Example #1
......
.o....
...o..
...o..
......
......


Output Example #1
32692


Input Example #2
oooo..
......
......
......
......
......


Output Example #2
0

题解:深搜枚举,2^36.大数据会超时


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
char s[7][7];
int rowNum[7],colNum[7],res;
void dfs(int r,int c) {
    if(c>=6) {
        if(rowNum[r]!=3) {
			return;
		}
        dfs(r+1,0);
    }
    if(r==6) {	
        for(int i=0;i<6;i++) {
            if(colNum[i]!=3) {
				return;
			}
        }
        res++;
        return;
    }
	if(6-(c+1)+rowNum[r]>=3)
        dfs(r,c+1);
    if(s[r][c]=='.'&&(colNum[c]+1)<=3) {
        rowNum[r]++;
        colNum[c]++;
        if(rowNum[r]==3) {
            dfs(r+1,0);
        }
        else dfs(r,c+1);
		rowNum[r]--;
        colNum[c]--;
    }
}
int main()
{
    bool flag=true;
	memset(rowNum,0,sizeof(rowNum));
	memset(colNum,0,sizeof(colNum));
    for(int i=0;i<6;i++) {
        for(int j=0;j<6;j++) {
            scanf("%c",&s[i][j]);
			if(s[i][j]=='o') {
				rowNum[i]++;
			    colNum[j]++;
			}
			if(colNum[j]>3) {
				flag=false;
			}
        }
        if(rowNum[i]>3) {
			flag=false;
		}
        getchar();
    }
    res=0;
    if(!flag) {
		printf("0\n");
        return 0;
    }
    dfs(0,0);
    printf("%d\n",res);
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Boolean algebra [�; ∪ , ∩ ,�]. 1) To prove that [�; ∪ , ∩ ,�] is a Boolean algebra, we need to show that it satisfies the following properties: i) Closure under complementation: For any element � in �, there exists an element � in � such that � ∪ � = � and � ∩ � = �. ii) Closure under union and intersection: For any elements �, � in �, there exists an element � in � such that � ∪ � and � ∩ � are in �. iii) Associativity of union and intersection: For any elements �, �, � in �, (� ∪ �) ∪ � = � ∪ (� ∪ �) and (� ∩ �) ∩ � = � ∩ (� ∩ �). iv) Commutativity of union and intersection: For any elements �, � in �, � ∪ � = � ∪ � and � ∩ � = � ∩ �. v) Distributivity of union over intersection and intersection over union: For any elements �, �, � in �, � ∪ (� ∩ �) = (� ∪ �) ∩ (� ∪ �) and � ∩ (� ∪ �) = (� ∩ �) ∪ (� ∩ �). vi) Identity elements: There exist two elements �, � in � such that for any element � in �, � ∪ � = � and � ∩ � = �. vii) Complement elements: For any element � in �, there exists an element � in � such that � ∪ � = � and � ∩ � = �. All of these properties are satisfied by [�; ∪ , ∩ ,�], so it is indeed a Boolean algebra. 2) Here are the operation tables for [�; ∪ , ∩ ,�]: ``` ∪ 0 1 2 _____ 0| 0 1 2 1| 1 1 2 2| 2 2 2 ∩ 0 1 2 _____ 0| 0 0 0 1| 0 1 1 2| 0 1 2 � 0 1 2 _____ | 2 1 0 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值