uva 11391 Blobs in the Board (DP)

II U C ONLINE C ON TEST2 008

Problem G: Blobs in the Board

Input: standard input

Output: standard output

You are given a board consists of R rows and C columns. There are N blobs in the board. Each cell of the board might or might not contain a blob. A cell can have at most 8 adjacent cells. A blob can jump over any adjacent blobs to the next empty cell. Thus a blob can jump straight to two squares at any of the 8 directions if the destination cell is empty and the cell between the source cell and the destination cell contains a blob. Blobs cannot jump outside of the board. After the jump the blob in the middle cell of the jump will be removed. The goal is to make jumps carefully so that after N-1 jumps only one blob remains. Your task is to count how many ways the goal can be achieved.

Input

Input consists of several test cases. The first line of the input file contains a single integer T indicating the number of test cases. Then T test cases follow. Each test case starts with three positive integers R, C and N. Each of the next N lines contain two integers x, y where i-th line contains the row position x and column position y of the i-th blob. No two blobs share the same cell.

Output

For each set of input produce one line of output “Case x: n”, where x indicates the test case number (starting from 1) and n the number of ways the goal can be achieved.

Constraints

- R, C ≤ 4

- 1 ≤ x ≤ R

- 1 ≤ y ≤ C

- 1 ≤ i ≤ N

Sample Input

Output for Sample Input

3

1 2 1

1 1

3 3 8

2 3

2 1

1 2

3 3

3 2

3 1

1 1

1 3

3 3 3

3 1

2 2

1 2


#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long

const int maxn = 65538;
const int dl[8] = {-1 , -1 , 1 , 1 , 0 , 1 , -1 , 0};
const int dr[8] = {-1 , 1 , 1 , -1 , 1 , 0 , 0 , -1};
ll dp[5][5][16][maxn];
int R , C , N;
struct point{
	int x , y;
	point(int a = 0 , int b = 0){
		x = a , y = b;
	}
}p[maxn];

void initial(){
	for(int i = 0;i < 5;i++){
		for(int j = 0;j < 5;j++){
			for(int k = 0;k < 16;k++){
				for(int l = 0;l < maxn;l++){
					dp[i][j][k][l] = -1;
				}
			}
		}
	}
	for(int i = 1;i <= 4;i++){
		for(int j = 1;j <= 4;j++){
			p[1<<(j+(i-1)*4-1)] = point(i , j);
		}
	}
}

ll DP(int sta , int n){
	if(dp[R][C][n][sta] != -1) return dp[R][C][n][sta];
	vector<int> v;
	for(int i = 0;i < 16;i++){
		if(!!(sta & (1<<i))){
			v.push_back(1<<i);
		}
	}
	if(v.size() == 1) return dp[R][C][n][sta] = 1;
	if(n == 0) return dp[R][C][n][sta] = 0;
	int ans = 0;
	for(int i = 0;i < v.size();i++){
		for(int k = 0;k < 8;k++){
			int tx = p[v[i]].x+dr[k];
			int ty = p[v[i]].y+dl[k];
			if(tx >= 1 && tx <= R && ty >= 1 && ty <= C){
				if(!(sta & 1<<(ty+(tx-1)*4-1))){
					ans += DP(sta-v[i]+(1<<(ty+(tx-1)*4-1)) , n-1);
				}else{
					tx = tx+dr[k];
					ty = ty+dl[k];
					if(tx >= 1 && tx <= R && ty >= 1 && ty <= C && !(sta & 1<<(ty+(tx-1)*4-1))){
						ans += DP(sta-v[i]+(1<<(ty+(tx-1)*4-1))-(1<<(ty-dl[k]+(tx-dr[k]-1)*4-1)) , n-1);
					}
				}
			}
		}
	}
	return dp[R][C][n][sta] = ans;
}
	

void computing(){
	scanf("%d%d%d" , &R ,&C , &N);
	int sta = 0 ,x ,y;
	for(int i = 0;i < N;i++){
		scanf("%d%d" , &x, &y);
		sta += (1<<(y+(x-1)*4-1));
	}
	printf("%d\n" , DP(sta , N-1));
}

int main(){
	initial();
	int t;
	cin >> t;
	for(int i = 1;i <= t;i++){
		printf("Case %d: ", i);
		computing();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值