[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;
}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章